栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android开发TextvView实现镂空字体效果示例代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android开发TextvView实现镂空字体效果示例代码

Android镂空字体的实现效果图,感兴趣的朋友可以参考实现代码。

效果图:

记录一下...

自定义TextView

public class HollowTextView extends AppCompatTextView {
 private Paint mTextPaint, mBackgroundPaint;
 private Bitmap mBackgroundBitmap,mTextBitmap;
 private Canvas mBackgroundCanvas,mTextCanvas;
 private RectF mBackgroundRect;
 private int mBackgroundColor;
 private float mCornerRadius;
 
 public HollowTextView(Context context) {
  this(context,null);
 }
 
 public HollowTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initAttrs(attrs,0);
  initPaint();
 }
 
 public HollowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  initAttrs(attrs,defStyleAttr);
  initPaint();
 }
 
 
 private void initAttrs(AttributeSet attrs,int defStyleAttr){
  if(attrs == null){
   return;
  }
  TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.HollowTextView, defStyleAttr, 0);
  mBackgroundColor = typedArray.getColor(R.styleable.HollowTextView_hollowTextView_background_color, Color.TRANSPARENT);
  mCornerRadius = typedArray.getDimension(R.styleable.HollowTextView_hollowTextView_corner_radius,0);
  typedArray.recycle();
 }
 
 
 private void initPaint() {
  //画文字的paint
  mTextPaint = new Paint();
  //这是镂空的关键
  mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
  mTextPaint.setAntiAlias(true);
  mBackgroundPaint = new Paint();
  mBackgroundPaint.setColor(mBackgroundColor);
  mBackgroundPaint.setAntiAlias(true);
 
 }
 
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  mBackgroundBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
  mBackgroundCanvas = new Canvas(mBackgroundBitmap);
  mTextBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_4444);
  mTextCanvas = new Canvas(mTextBitmap);
  mBackgroundRect = new RectF(0,0,getWidth(),getHeight());
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
  //这里给super传入的是mTextCanvas,把一些基本属性都支持进去
  super.onDraw(mTextCanvas);
  drawBackground(mBackgroundCanvas);
  int sc;
  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ){
   sc = canvas.saveLayer(0,0,getMeasuredWidth(),getMeasuredHeight(),null);
  }else {
   sc = canvas.saveLayer(0,0,getMeasuredWidth(),getMeasuredHeight(),null,Canvas.ALL_SAVE_FLAG);
  }
  canvas.drawBitmap(mBackgroundBitmap,0,0,null);
  canvas.drawBitmap(mTextBitmap, 0, 0, mTextPaint);
  canvas.restoreToCount(sc);
 }
 
 private void drawBackground(Canvas canvas){
  if(mCornerRadius > 0){
   canvas.drawRoundRect(mBackgroundRect,mCornerRadius,mCornerRadius, mBackgroundPaint);
  }else {
   canvas.drawColor(mBackgroundColor);
  }
 }

attr.xml文件


   
   
 

xml中使用

总结

到此这篇关于Android开发TextvView实现镂空字体效果示例代码的文章就介绍到这了,更多相关Android实现镂空字体内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153729.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号