Android開發(fā)TextvView實(shí)現(xiàn)鏤空字體效果示例代碼
Android鏤空字體的實(shí)現(xiàn)效果圖,感興趣的朋友可以參考實(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: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實(shí)現(xiàn)鏤空字體效果示例代碼的文章就介紹到這了,更多相關(guān)Android實(shí)現(xiàn)鏤空字體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android自定義view實(shí)現(xiàn)圓環(huán)效果實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Android自定義view實(shí)現(xiàn)圓環(huán)效果,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-07-07
Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會丟失問題
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會丟失問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法
這篇文章主要為大家詳細(xì)介紹了3種Android隱藏頂部狀態(tài)欄及標(biāo)題欄的方法,還涉及一種隱藏Android 4.0平板底部狀態(tài)欄的方法,感興趣的小伙伴們可以參考一下2016-02-02
AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁滑動效果
這篇文章主要介紹了AndroidUI組件SlidingTabLayout實(shí)現(xiàn)ViewPager頁滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
Flutter學(xué)習(xí)之實(shí)現(xiàn)自定義themes詳解
一般情況下我們在flutter中搭建的app基本上都是用的是MaterialApp這種設(shè)計(jì)模式,MaterialApp中為我們接下來使用的按鈕,菜單等提供了統(tǒng)一的樣式,那么這種樣式能不能進(jìn)行修改或者自定義呢?答案是肯定的,一起來看看吧2023-03-03
android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動效果實(shí)例
這篇文章主要介紹了android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動效果實(shí)例,即相冊內(nèi)的圖片實(shí)現(xiàn)可左右滑動的效果,需要的朋友可以參考下2014-07-07

