Android開發(fā)TextvView實現(xiàn)鏤空字體效果示例代碼
Android鏤空字體的實現(xiàn)效果圖,感興趣的朋友可以參考實現(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,把一些基本屬性都支持進去 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:id="@+id/hollowtext" 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實現(xiàn)鏤空字體效果示例代碼的文章就介紹到這了,更多相關(guān)Android實現(xiàn)鏤空字體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android本地數(shù)據(jù)存儲Room實踐和優(yōu)化技巧
本文詳細(xì)介紹了Android本地數(shù)據(jù)存儲框架Room的使用,包括基本概念、核心組件、最佳實踐、優(yōu)化技巧等,幫助開發(fā)者學(xué)習(xí)和掌握Room的使用方法,提升數(shù)據(jù)存儲效率和應(yīng)用性能2023-04-04Android判斷手機是否是小米MIUI系統(tǒng)的方法
這篇文章主要介紹了Android判斷手機是否是小米MIUI系統(tǒng)的方法的相關(guān)資料,需要的朋友可以參考下2016-02-02Android開發(fā)實現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)ListView和adapter配合顯示圖片和文字列表功能,涉及Android使用ListView結(jié)合adapter適配器實現(xiàn)圖文顯示功能相關(guān)的布局、解析、權(quán)限控制等操作技巧,需要的朋友可以參考下2019-04-04360瀏覽器文本框獲得焦點后被android軟鍵盤遮罩該怎么辦
最近接了個項目,項目需求是這樣的,站點上篩選按鈕點擊后彈出層(fixed),當(dāng)輸入框獲取焦點以后彈出系統(tǒng)自帶的軟鍵盤,在android上十款瀏覽器挨個測試比對,發(fā)現(xiàn)在360瀏覽器彈出鍵盤以后獲取焦點的文本框被軟鍵盤覆蓋了,下面分享我的解決辦法2015-12-12Android編程實現(xiàn)自動調(diào)整TextView字體大小以適應(yīng)文字長度的方法
這篇文章主要介紹了Android編程實現(xiàn)自動調(diào)整TextView字體大小以適應(yīng)文字長度的方法,涉及Android基于TextView類的繼承及Paint屬性操作實現(xiàn)字體大小自適應(yīng)的相關(guān)技巧,需要的朋友可以參考下2016-01-01Android 讀取文件內(nèi)容實現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10