色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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
相關文章:
主站蜘蛛池模板: 欧美日韩高清观看一区二区 | 久久国产精品一区二区三区 | 超清波多野结衣精品一区 | 亚洲精品一区二区三区中文字幕 | 洋老外米糕国产一区二区 | www.亚洲免费| 天天舔夜夜操 | 亚洲伊人成人 | 日韩精品在线看 | 怡红院视频在线观看 | 免费观看性欧美一级 | 日韩欧美中文字幕在线播放 | 成人亚州| 亚欧精品在线观看 | 极品美女一级毛片 | 久久精品片 | 久久成年片色大黄全免费网站 | 一本不卡 | 91视频18| 久热香蕉在线视频 | 国产精品揄拍一区二区 | 9久9久女女热精品视频免费观看 | 国产欧美综合精品一区二区 | 美女午夜影院 | 欧美日韩国产一区二区三区播放 | 国产三级日本三级美三级 | 欧美 亚洲 另类 自拍 在线 | 亚洲精品久久久久中文 | 欧美一级毛片图 | 91视频99 | 久久免费视频6 | 日韩欧美一区二区在线 | 精品免费久久 | 尤物tv已满18点击进入 | 成人亚洲欧美日韩中文字幕 | 欧美成人午夜免费完成 | 日本天堂在线视频 | 久久99精品一级毛片 | 一色屋成人免费精品网 | 免费成年人视频网站 | 日韩国产一区二区 |