1. 思路分析
自定義View步驟:
1>:values__attrs.xml猛频,自定義屬性眼刃;
2>:在第三個(gè)構(gòu)造方法中獲取自定義屬性蒋院;
3>:onMeasure:不是非必須的哼勇;
4>:onDraw:所有繪制的代碼都寫(xiě)在onDraw方法中炸庞;
步驟分析:
1>:在values__attrs中定義自定義屬性;
2>:自定義MyTextView翠勉;
3>:在activity_mytextview布局文件中使用妖啥;
效果圖如下:
1>:values__attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 自定義View的名字 -->
<declare-styleable name="MyTextView">
<!-- 以下是所用到的屬性-->
<!-- 文字-string -->
<attr name="novateText" format="string"/>
<!-- 文字大小-dimension -->
<attr name="novateTextSize" format="dimension"/>
<!-- 文字長(zhǎng)度-->
<attr name="novateMaxLength" format="integer"/>
<!-- 文字顏色-color-->
<attr name="novateColor" format="color"/>
<!-- 枚舉 -->
<attr name="novateInputType">
<enum name="number" value="1"/>
<enum name="text" value="2"/>
<enum name="password" value="3"/>
</attr>
</declare-styleable>
</resources>
2>:MyTextView
/**
* ================================================
* Email: 2185134304@qq.com
* Created by Novate 2018/12/28 10:52
* Version 1.0
* Params:
* Description: 自定義TextView
* ================================================
*/
public class MyTextView extends View {
// 文字
private String mText ;
// 文字默認(rèn)大小 15sp
private int mTextSize = 15 ;
// 文字默認(rèn)顏色
private int mTextColor = Color.BLACK ;
// 初始化畫(huà)筆
private Paint mPaint ;
/**
* 這種調(diào)用第1個(gè)構(gòu)造方法
* TextView tv = new TextView(this):
*/
public MyTextView(Context context) {
this(context,null);
}
/**
* 這種調(diào)用第2個(gè)構(gòu)造方法
* <com.novate.test.customview.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:novateText="北京Novate"
app:novateTextSize="20sp"
app:novateColor="#ff0000"
android:background="#00FF00"
/>
*/
public MyTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
/**
* 這個(gè)和第二個(gè)方法一樣,只是該布局文件中含有 style屬性
*/
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 獲取自定義屬性
TypedArray typedArray = context.obtainStyledAttributes(attrs , R.styleable.MyTextView) ;
// 獲取文字
mText = typedArray.getString(R.styleable.MyTextView_novateText) ;
// 獲取文字大小
mTextSize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_novateTextSize , sp2px(mTextSize)) ;
// 獲取文字顏色
typedArray.getColor(R.styleable.MyTextView_novateColor , mTextColor) ;
// 釋放資源
typedArray.recycle();
// 創(chuàng)建畫(huà)筆
mPaint = new Paint() ;
// 設(shè)置抗鋸齒对碌,讓文字比較清晰荆虱,同時(shí)文字也會(huì)變得圓滑
mPaint.setAntiAlias(true);
// 設(shè)置文字大小
mPaint.setTextSize(mTextSize);
// 設(shè)置畫(huà)筆顏色
mPaint.setColor(mTextColor);
}
/**
* 測(cè)量文字
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// TODO:方式1:如果在布局文件中設(shè)置的寬高都是固定值[比如100dp、200dp等],就用下邊方式直接獲取寬高
int width = MeasureSpec.getSize(widthMeasureSpec) ;
int height = MeasureSpec.getSize(heightMeasureSpec) ;
// TODO:方式2:如果調(diào)用者在布局文件給自定義MyTextView的寬高屬性設(shè)置wrap_content怀读,
// 就需要通過(guò)下邊的 widthMode和heightMode來(lái)獲取寬高
// 獲取寬高模式
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
// 如果在布局中設(shè)置寬高都是wrap_content[對(duì)應(yīng)AT_MOST]诉位,必須用mode計(jì)算
if (widthMode == MeasureSpec.AT_MOST){
// 文字寬度 與字體大小和長(zhǎng)度有關(guān)
Rect rect = new Rect() ;
// 獲取文本區(qū)域 param1__測(cè)量的文字 param2__從位置0開(kāi)始 param3__到文字長(zhǎng)度
mPaint.getTextBounds(mText , 0 , mText.length() , rect);
// 文字寬度 getPaddingLeft寬度 getPaddingRight高度 寫(xiě)這兩個(gè)原因是為了在布局文件中設(shè)置padding屬性起作用
width = rect.width() + getPaddingLeft() + getPaddingRight() ;
}
if (heightMode == MeasureSpec.AT_MOST){
Rect rect = new Rect() ;
mPaint.getTextBounds(mText , 0 , mText.length() , rect);
height = rect.height() + getPaddingTop() + getPaddingBottom() ;
}
// 給文字設(shè)置寬高
setMeasuredDimension(width , height);
}
/**
* 繪制文字
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom ;
int baseLine = getHeight()/2 + dy ;
int x = getPaddingLeft() ;
canvas.drawText(mText , x , baseLine , mPaint);
}
/**
* sp轉(zhuǎn)為px
*/
private int sp2px(int sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
}
}
3>:activity_mytextview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.novate.test.customview.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:novateText="北京Novate"
app:novateTextSize="20sp"
app:novateColor="#ff0000"
android:background="#00FF00"
/>
</LinearLayout>
4>:MyTextViewActivity
public class MyTextViewActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mytextview);
}
}