当前位置:   article > 正文

android 自定义 TextView 点击字体镂空效果_android自定义view镂空图形

android自定义view镂空图形

网上好多字体镂空效果都是单行的,及没有点击效果的,所以,我没办法就自己写了一个TextView ,目的是自定义这个,点击的时候才是镂空效果,但是,平时的时候是常态,就像是selector一样,这是一个工具类,你们拿去直接用就行了

  1. public class ShowPalettePageTextView extends View {
  2. public static final int BACKGROUND_COLOR = MyApp.getAppColor(R.color.color_dialog_text_content);
  3. private static final int DEFAULT_RADIUS = MyApp.getAppDimen(R.dimen.default_radio_size);
  4. private TextPaint mTextPaint;
  5. private Paint mBackgroundPaint;
  6. private RectF mBackgroundRect;
  7. private boolean isPressed = false;
  8. private String mShowPageText;
  9. private Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
  10. public ShowPalettePageTextView(Context context) {
  11. this(context, null);
  12. }
  13. public ShowPalettePageTextView(Context context, AttributeSet attrs) {
  14. this(context, attrs, -1);
  15. }
  16. public ShowPalettePageTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  17. super(context, attrs, defStyleAttr);
  18. mTextPaint = new TextPaint();
  19. mTextPaint.setAntiAlias(true);
  20. mTextPaint.setColor(BACKGROUND_COLOR);
  21. mTextPaint.setTextSize(MyApp.getAppDimen(R.dimen.text_size));
  22. mBackgroundPaint = new Paint();
  23. mBackgroundPaint.setColor(BACKGROUND_COLOR);
  24. mBackgroundPaint.setAntiAlias(true);
  25. mBackgroundRect = new RectF();
  26. }
  27. @Override
  28. protected void onSizeChanged(int width, int height, int oldw, int oldh) {
  29. super.onSizeChanged(width, height, oldw, oldh);
  30. mBackgroundRect.set(0, 0, width, height);
  31. }
  32. public void setText(String showPageText) {
  33. this.mShowPageText = showPageText;
  34. invalidate();
  35. }
  36. @Override
  37. protected void onDraw(Canvas canvas) {
  38. drawBackground(canvas);
  39. drawText(canvas);
  40. }
  41. private void drawBackground(Canvas canvas) {
  42. if (isPressed) {
  43. mBackgroundPaint.setStyle(Paint.Style.FILL);
  44. } else {
  45. mBackgroundPaint.setStyle(Paint.Style.STROKE);
  46. }
  47. canvas.drawRoundRect(mBackgroundRect, DEFAULT_RADIUS, DEFAULT_RADIUS, mBackgroundPaint);
  48. }
  49. private void drawText(Canvas canvas) {
  50. if (mShowPageText == null) {
  51. return;
  52. }
  53. if (isPressed) {
  54. mTextPaint.setXfermode(xfermode);
  55. } else {
  56. mTextPaint.setXfermode(null);
  57. }
  58. float x = mBackgroundRect.width() / 2 - mTextPaint.measureText(mShowPageText) / 2;
  59. float y = mBackgroundRect.top + mBackgroundRect.height() / 2 + mTextPaint.getTextSize() / 3;
  60. canvas.drawText(mShowPageText, x, y, mTextPaint);
  61. }
  62. @SuppressLint("ClickableViewAccessibility")
  63. @Override
  64. public boolean onTouchEvent(MotionEvent event) {
  65. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  66. isPressed = true;
  67. invalidate();
  68. } else if (event.getAction() == MotionEvent.ACTION_UP) {
  69. isPressed = false;
  70. invalidate();
  71. }
  72. return super.onTouchEvent(event);
  73. }
  74. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/73672
推荐阅读
相关标签
  

闽ICP备14008679号