最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年10月30日 09:34:57   作者:cachelittlepeople  
這篇文章主要介紹了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)文章

最新評論

江华| 安徽省| 旅游| 松溪县| 农安县| 贵定县| 濉溪县| 大足县| 新绛县| 金寨县| 体育| 桃园市| 台安县| 瑞昌市| 屏东市| 朝阳县| 句容市| 芦溪县| 铜鼓县| 庆云县| 富锦市| 象山县| 布尔津县| 诸城市| 于田县| 祁门县| 海淀区| 太谷县| 武穴市| 富阳市| 仁寿县| 新乡市| 石嘴山市| 元江| 佛冈县| 高密市| 元朗区| 苍溪县| 剑河县| 托克托县| 瑞丽市|