欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年10月30日 09:34:57   作者:cachelittlepeople  
這篇文章主要介紹了Android開發(fā)TextvView實現(xiàn)鏤空字體效果,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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)化技巧

    Android本地數(shù)據(jù)存儲Room實踐和優(yōu)化技巧

    本文詳細(xì)介紹了Android本地數(shù)據(jù)存儲框架Room的使用,包括基本概念、核心組件、最佳實踐、優(yōu)化技巧等,幫助開發(fā)者學(xué)習(xí)和掌握Room的使用方法,提升數(shù)據(jù)存儲效率和應(yīng)用性能
    2023-04-04
  • Android判斷手機是否是小米MIUI系統(tǒng)的方法

    Android判斷手機是否是小米MIUI系統(tǒng)的方法

    這篇文章主要介紹了Android判斷手機是否是小米MIUI系統(tǒng)的方法的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Android開發(fā)實現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例

    Android開發(fā)實現(xiàn)ListView和adapter配合顯示圖片和文字列表功能示例

    這篇文章主要介紹了Android開發(fā)實現(xiàn)ListView和adapter配合顯示圖片和文字列表功能,涉及Android使用ListView結(jié)合adapter適配器實現(xiàn)圖文顯示功能相關(guān)的布局、解析、權(quán)限控制等操作技巧,需要的朋友可以參考下
    2019-04-04
  • Android 超簡易Zxing框架 生成二維碼+掃碼功能

    Android 超簡易Zxing框架 生成二維碼+掃碼功能

    這篇文章主要介紹了Android 超簡易Zxing框架 生成二維碼+掃碼功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 360瀏覽器文本框獲得焦點后被android軟鍵盤遮罩該怎么辦

    360瀏覽器文本框獲得焦點后被android軟鍵盤遮罩該怎么辦

    最近接了個項目,項目需求是這樣的,站點上篩選按鈕點擊后彈出層(fixed),當(dāng)輸入框獲取焦點以后彈出系統(tǒng)自帶的軟鍵盤,在android上十款瀏覽器挨個測試比對,發(fā)現(xiàn)在360瀏覽器彈出鍵盤以后獲取焦點的文本框被軟鍵盤覆蓋了,下面分享我的解決辦法
    2015-12-12
  • Android Handler機制詳解原理

    Android Handler機制詳解原理

    Handler主要用于異步消息的處理:當(dāng)發(fā)出一個消息之后,首先進入一個消息隊列,發(fā)送消息的函數(shù)即刻返回,而另外一個部分在消息隊列中逐一將消息取出,然后對消息進行處理,也就是發(fā)送消息和接收消息不是同步的處理。 這種機制通常用來處理相對耗時比較長的操作
    2021-11-11
  • Android 對Map按key和value分別排序的實例

    Android 對Map按key和value分別排序的實例

    下面小編就為大家?guī)硪黄狝ndroid 對Map按key和value分別排序的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • Android如何獲取雙卡手機IMEI的方法示例

    Android如何獲取雙卡手機IMEI的方法示例

    這篇文章主要介紹了Android如何獲取雙卡手機IMEI的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • Android編程實現(xiàn)自動調(diào)整TextView字體大小以適應(yīng)文字長度的方法

    Android編程實現(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-01
  • Android 讀取文件內(nèi)容實現(xiàn)方法總結(jié)

    Android 讀取文件內(nèi)容實現(xiàn)方法總結(jié)

    這篇文章主要介紹了Android 讀取文件內(nèi)容實現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下
    2016-10-10

最新評論