国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android開發TextvView實現鏤空字體效果示例代碼

瀏覽:85日期:2022-09-21 18:38:24

Android開發TextvView實現鏤空字體效果示例代碼

記錄一下...

自定義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文件

<declare-styleable name='HollowTextView'> <attr name='hollowTextView_background_color' format='color|reference'/> <attr name='hollowTextView_corner_radius' format='dimension|reference'/> </declare-styleable>

xml中使用

<com.cn.util.HollowTextView android: android:layout_width='60dp' android:layout_height='50dp' android:gravity='center' android:text='99+' android:textSize='30sp' android:textStyle='bold' app:hollowTextView_background_color='@color/white' app:hollowTextView_corner_radius='5dp' android:layout_centerInParent='true'/>

總結

到此這篇關于Android開發TextvView實現鏤空字體效果示例代碼的文章就介紹到這了,更多相關Android實現鏤空字體內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 国产99视频精品免费视频7 | 高清一级淫片a级中文字幕 高清一区二区 | 国产日产欧产精品精品推荐小说 | 欧美精品在线视频 | 中文字幕曰韩一区二区不卡 | 免费一级淫片aaa片毛片a级 | 另类视频综合 | 精品久久久久久久久久久 | 亚洲精品线在线观看 | 欧美亚洲国产视频 | 亚洲人成在线免费观看 | 欧美三级真做在线观看 | 欧美在线视频精品 | 国产精品欧美亚洲韩国日本不卡 | 亚洲精品14p | 中文字幕一区二区小泽玛利亚 | 国产伦精品一区二区三区 | 午夜嘿咻 | 国产一区二区三区在线看 | 在线看国产| 日韩精品午夜视频一区二区三区 | www.日本在线观看 | 精品日韩一区二区三区 | 欧美激情视频在线观看一区二区三区 | 久久久久毛片成人精品 | 91久久综合 | 日韩 欧美 国产 师生 制服 | 成年人看的免费视频 | 26uuu天天夜夜综合 | 日本韩国一级 | 不卡一级aaa全黄毛片 | 精品国产一区二区三区2021 | 国产综合精品久久亚洲 | 久草视频福利资源站 | 99热精品在线观看 | 日韩精品午夜视频一区二区三区 | 暖暖免费高清日本一区二区三区 | 国产区久久| 亚洲国产一成人久久精品 | 国内真实愉拍系列情侣 | 搞黄网站免费看 |