当前位置:   article > 正文

Android镂空字体,TextvView实现镂空字体效果_android textview字体镂空

android textview字体镂空

前言:

本来朋友问我镂空字体怎么实现,刚开始以为是简简单单调整背景色和透明度,后来发现是我想多了。记录一下

效果图:

 看了几个整体思路都是

  • 自定义HolloTextView继承自View,重写onDraw方法,绘制背景,使用PorterDuff.Mode.DST_OUT的画笔调用canvas.drawText方法绘制文字

其实有些背景可以用别的方法实现:

透明背景可以用

binding.btMySignOut.setTextColor(Color.alpha(100));等等

alpha范围是0-250

第一步:

1、创建HollowTextView
  1. public class HollowTextView extends AppCompatTextView {
  2. private Paint mTextPaint, mBackgroundPaint;
  3. private Bitmap mBackgroundBitmap, mTextBitmap;
  4. private Canvas mBackgroundCanvas, mTextCanvas;
  5. private RectF mBackgroundRect;
  6. private int mBackgroundColor;
  7. private float mCornerRadius;
  8. public HollowTextView(Context context) {
  9. this(context, null);
  10. }
  11. public HollowTextView(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. initAttrs(attrs, 0);
  14. initPaint();
  15. }
  16. public HollowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  17. super(context, attrs, defStyleAttr);
  18. initAttrs(attrs, defStyleAttr);
  19. initPaint();
  20. }
  21. private void initAttrs(AttributeSet attrs, int defStyleAttr) {
  22. if (attrs == null) {
  23. return;
  24. }
  25. TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HollowTextView, defStyleAttr, 0);
  26. mBackgroundColor = typedArray.getColor(R.styleable.HollowTextView_hollowTextView_background_color, Color.TRANSPARENT);
  27. mCornerRadius = typedArray.getDimension(R.styleable.HollowTextView_hollowTextView_corner_radius, 0);
  28. typedArray.recycle();
  29. }
  30. /***
  31. * 初始化画笔属性
  32. */
  33. private void initPaint() {
  34. //画文字的paint
  35. mTextPaint = new Paint();
  36. //这是镂空的关键
  37. mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  38. mTextPaint.setAntiAlias(true);
  39. mBackgroundPaint = new Paint();
  40. mBackgroundPaint.setColor(mBackgroundColor);
  41. mBackgroundPaint.setAntiAlias(true);
  42. }
  43. @Override
  44. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  45. super.onSizeChanged(w, h, oldw, oldh);
  46. mBackgroundBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
  47. mBackgroundCanvas = new Canvas(mBackgroundBitmap);
  48. mTextBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
  49. mTextCanvas = new Canvas(mTextBitmap);
  50. mBackgroundRect = new RectF(0, 0, getWidth(), getHeight());
  51. }
  52. @Override
  53. protected void onDraw(Canvas canvas) {
  54. //这里给super传入的是mTextCanvas,把一些基本属性都支持进去
  55. super.onDraw(mTextCanvas);
  56. drawBackground(mBackgroundCanvas);
  57. int sc;
  58. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  59. sc = canvas.saveLayer(0, 0, getMeasuredWidth(), getMeasuredHeight(), null);
  60. } else {
  61. sc = canvas.saveLayer(0, 0, getMeasuredWidth(), getMeasuredHeight(), null, Canvas.ALL_SAVE_FLAG);
  62. }
  63. canvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
  64. canvas.drawBitmap(mTextBitmap, 0, 0, mTextPaint);
  65. canvas.restoreToCount(sc);
  66. }
  67. private void drawBackground(Canvas canvas) {
  68. if (mCornerRadius > 0) {
  69. canvas.drawRoundRect(mBackgroundRect, mCornerRadius, mCornerRadius, mBackgroundPaint);
  70. } else {
  71. canvas.drawColor(mBackgroundColor);
  72. }
  73. }
  74. }

第二步:创建values下styleable的xml(attrs

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="HollowTextView">
  4. <attr name="hollowTextView_background_color" format="color|reference"/>
  5. <attr name="hollowTextView_corner_radius" format="dimension|reference"/>
  6. </declare-styleable>
  7. </resources>

 最后就是引用HollowTextView

  1. <com.test.aaa.activitys.HollowTextView
  2. android:id="@+id/hollowtext"
  3. android:layout_width="wrap_content"
  4. android:layout_height="50dp"
  5. android:gravity="center"
  6. android:text="镂空文本"
  7. android:textSize="30sp"
  8. android:layout_marginTop="230dp"
  9. android:layout_marginLeft="@dimen/dp_200"
  10. android:layout_marginRight="50dp"
  11. android:textStyle="bold"
  12. app:hollowTextView_background_color="#4CAF50"
  13. app:hollowTextView_corner_radius="5dp"/>

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

闽ICP备14008679号