午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼

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

Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼

記錄一下...

自定義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(); //這是鏤空的關(guān)鍵 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,把一些基本屬性都支持進(jìn)去 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'/>

總結(jié)

到此這篇關(guān)于Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)鏤空字體內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 成人一区二区三区在线 | 欧美综合久久 | 久久久人人人 | 午夜精品久久久久久久久 | 日本欧美在线视频 | 人人看av | 白浆视频在线观看 | 国产精品视频久久久久久久 | 精品一二三四区 | 99自拍视频| 日批动态图 | 国产黄色精品 | 99久久久精品| 色在线视频 | 欧美精品一区二区三区四区五区 | 一区二区三区美女视频 | 国内精品免费视频 | 国产精品视频免费观看 | 亚洲欧美日本一区 | jlzzjlzz亚洲日本少妇 | 日韩三级视频在线播放 | а√天堂8资源在线官网 | 亚洲精品一二区 | 天堂av在线免费观看 | 99色网站| 欧美三级国产 | 69夜色精品国产69乱 | 欧美性videos| 天天综合国产 | 国产精品99久久久久久动医院 | 亚洲一二三区视频 | 欧美精品一二三 | 欧美在线免费观看 | 成年人在线视频免费观看 | 免费岛国av| 国产成人免费观看 | 中文字幕日韩三级 | 日韩高清在线一区 | 毛片大全在线观看 | 不卡国产视频 | 91黄色免费看 |