当前位置:   article > 正文

Android TextView文字镂空效果的两种实现_textview实现字体镂空

textview实现字体镂空

一图胜千言

文字镂空效果主要有两种实现方式:

1,自动义View,在canvas中绘制圆角矩形作为背景,然后绘制文字,通过PorterDuff.Mode.DST_OUT把背景擦除,实现镂空效果。如上图中的第一个。

2,自定义TextView,定义两Bitmap,分别在Bitmap上画背景和文字前景,然后通过PorterDuff.Mode.DST_OUT,把背景擦除,实现镂空效果。如上图中的第二个。

比较:两种方式原理上是一样的,但是第二种方式继承自TextView,可以直接使用TextView的属性,如字体大小、颜色、样式等等。而第一种则需要定义大量的属性。

下面分别介绍两种方式。

一,自动义View方式。

1,初始化

  1. public void init(Context context, AttributeSet attrs) {
  2. if (attrs != null) {
  3. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HollowView);
  4. // 获取要实现的内容
  5. textString = a.getString(R.styleable.HollowView_textString);
  6. // 获取背景色
  7. mBgColor = a.getColor(R.styleable.HollowView_bgColor, mBgColor);
  8. if (a.hasValue(R.styleable.HollowView_textSize)) {
  9. // 获取文字大小
  10. textSize = a.getDimension(R.styleable.HollowView_textSize, textSize);
  11. }
  12. // 获取圆角半径
  13. mRadius=a.getDimension(R.styleable.HollowView_radius,mRadius);
  14. a.recycle();
  15. }
  16. mPaint = new Paint();
  17. mPaint.setTextSize(textSize);
  18. mPaint.setAntiAlias(true);
  19. mPaint.setColor(mBgColor);
  20. Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
  21. // 计算绘制文字起始点的Y值
  22. drawY = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
  23. }

2,测量

这里用了默认的wrap_content测量方式,如果有需要可自行修改。

  1. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  2. // 获取文字宽度
  3. width = FontUtil.getTextWidth(textString, mPaint);
  4. // 获取文字高度
  5. height = FontUtil.getTextHeight(textString, mPaint);
  6. // 计算空间应占的宽度
  7. mWidth = width + getPaddingLeft() + getPaddingRight();
  8. // 计算空间应占的高度
  9. mHeight = height + getPaddingTop() + getPaddingBottom();
  10. // 背景的绘制范围
  11. rectF = new RectF(-mWidth / 2, -mHeight / 2, mWidth / 2, mHeight / 2);
  12. setMeasuredDimension(mWidth, mHeight);
  13. }

3,绘制

  1. protected void onDraw(Canvas canvas) {
  2. super.onDraw(canvas);
  3. // 移动原点到view中心
  4. canvas.translate(mWidth / 2, mHeight / 2);
  5. // 保存layer
  6. int layer = canvas.saveLayer(rectF, mPaint);
  7. // 绘制圆角矩形的背景
  8. canvas.drawRoundRect(rectF, mRadius, mRadius, mPaint);
  9. // 设置画笔PorterDuff.Mode.DST_OUT模式
  10. mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  11. // 绘制文字
  12. canvas.drawText(textString, -width / 2, drawY, mPaint);
  13. mPaint.setXfermode(null);
  14. // 还原图层
  15. canvas.restoreToCount(layer);
  16. }

这样就实现了图中第一个效果。

4,使用

  1. <per.wangsj.myview.view.textview.HollowView
  2. android:id="@+id/hollowView"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. app:textString="撒拉嘿!"
  6. app:bgColor="@color/color_1"
  7. app:textSize="24sp"
  8. app:radius="4dp"
  9. android:paddingLeft="8dp"
  10. android:paddingRight="8dp"
  11. android:paddingTop="4dp"
  12. android:paddingBottom="4dp"
  13. />

二,自动义TextView方式。

1,初始化属性,画笔等

  1. private void init(AttributeSet attrs) {
  2. TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HollowView);
  3. // 获取背景颜色属性
  4. bgColor = typedArray.getColor(R.styleable.HollowView_bgColor, bgColor);
  5. // 获取圆角半径属性
  6. mRadius = typedArray.getDimension(R.styleable.HollowView_radius, mRadius);
  7. // 获取文本
  8. textString = getText().toString();
  9. // 初始化画笔
  10. bgPaint = new Paint();
  11. bgPaint.setColor(bgColor);
  12. bgPaint.setAntiAlias(true);
  13. }

2,初始化两个Bitmap和canvas

通过canvas分别在bitmap上绘制文字,和背景。

  1. private void initCanvas() {
  2. // 显示文字的Bitmap
  3. textBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_4444);
  4. textCanvas = new Canvas(textBitmap);
  5. // 计算文字开始绘制位置的Y值
  6. Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
  7. float y = mHeight / 2 + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
  8. // 在textBitmap上画背景
  9. textCanvas.drawText(textString, getPaddingLeft(), y, getPaint());
  10. // 绘制背景的bitmap
  11. bgBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_4444);
  12. Canvas bgCanvas = new Canvas(bgBitmap);
  13. // 在bgBitmap上画背景
  14. bgCanvas.drawRoundRect(0, 0, mWidth, mHeight, mRadius, mRadius, bgPaint);
  15. }

3,绘制

  1. protected void onDraw(Canvas canvas) {
  2. // 保存图层
  3. int layer = canvas.saveLayer(0, 0, mWidth, mHeight, bgPaint);
  4. // 画背景
  5. canvas.drawBitmap(bgBitmap, 0, 0, bgPaint);
  6. // 设置画笔模式
  7. bgPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  8. // 画文字
  9. canvas.drawBitmap(textBitmap, 0, 0, bgPaint);
  10. bgPaint.setXfermode(null);
  11. // 还原图层
  12. canvas.restoreToCount(layer);
  13. }

代码注释已经比较清楚了,就不多做解释了。

可以看出,这种方式只需要添加背景色和圆角半径两个自定义属性。

4,使用

  1. <per.wangsj.myview.view.textview.HollowTextView
  2. android:id="@+id/hollowTextView"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:textSize="24sp"
  6. android:paddingLeft="8dp"
  7. android:paddingRight="8dp"
  8. app:bgColor="@color/color_1"
  9. app:radius="4dp"
  10. android:text="撒拉嘿!"/>

 

 

参考:https://www.colabug.com/4763345.html

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

闽ICP备14008679号