先寫幾點注意事項
1、設置矩形或者文字的位置所設置的位置是其左下角的座標
2、自定義view中getWidth和 getMeasuredWidth()的區別
下面是寫自定義view必備的幾個操作
1、在value中建立attrs.xml文件
我們定義了字體,字體顏色,字體大小3個屬性,format是值該屬性的取值類型:一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。
<resources>
<attr name="title_text" format="string" />
<attr name="title_text_color" format="color" />
<attr name="title_text_size" format="dimension" />
<attr name="circle_size" format="dimension" />
<declare-styleable name="CustomView" >
<attr name="title_text"/>
<attr name="title_text_color"/>
<attr name="title_text_size"/>
<attr name="circle_size"/>
</declare-styleable>
</resources>
2、在佈局中聲明自定義view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns: tools="http://schemas.android.com/tools"
xmlns: custom="http://schemas.android.com/apk/res-auto"
android :orientation="vertical" android:layout_width= "match_parent"
android :layout_height="match_parent">
<getui.com.customview.CustomView
android :layout_width="wrap_content"
android :layout_height="wrap_content"
custom :circle_size="50dp"
custom :title_text="1356"
custom :title_text_color="#dd5566"
custom :title_text_size="13dp"/>
</LinearLayout>
其中custom一般寫自動獲取自定義view的地址
3、在view中獲取自定義樣式 並畫圖
private int mTitleTextColor; //文本顏色
private String mTitleText ;//文本內容
private int mTitleTextSize; //文本大小
private int mCircleSize; //圓形的半徑
private Paint mPaint ;
private Canvas canvas ;
private Rect mBound ;
public CustomView(Context context) {
this (context,null);
}
public CustomView(Context context, AttributeSet attrs) {
this (context,attrs,0 );
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super (context, attrs, defStyleAttr);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomView,defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0;i < n;i++){
int attr = a.getIndex(i);
switch (attr){
case R.styleable.CustomView_title_text:
mTitleText = a.getString(attr);
break;
case R.styleable.CustomView_title_text_color:
// 字體默認設置爲黑色
mTitleTextColor = a.getColor(attr, Color. BLACK);
break;
case R.styleable.CustomView_title_text_size:
// 默認設置爲16sp,TypeValue也可以把sp轉化爲px
mTitleTextSize = a.getDimensionPixelSize(attr, ( int) TypedValue. applyDimension(
TypedValue. COMPLEX_UNIT_SP, 16 , getResources().getDisplayMetrics()));
break;
case R.styleable.CustomView_circle_size:
// 默認設置爲50sp,TypeValue也可以把sp轉化爲px
mCircleSize = a.getDimensionPixelSize(attr,( int) TypedValue. applyDimension(
TypedValue. COMPLEX_UNIT_SP, 50 , getResources().getDisplayMetrics()));
break;
}
}
//android的垃圾回收機制是自動調用的
a.recycle();
mPaint = new Paint();
mPaint .setTextSize(mTitleTextSize);
// mPaint.setColor(mTitleTextColor);
mBound = new Rect();
//設置文字寬和高
mPaint .getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super .onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super .onDraw(canvas);
WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
mPaint .setColor(Color.YELLOW);
canvas.drawCircle(width/2 , mCircleSize+20, mCircleSize, mPaint);
mPaint .setColor(Color.GREEN);
Log.i("mTitleText", getWidth() / 2+ " "+mCircleSize +" "+" "+ mTitleText+" " +getMeasuredWidth()/2);
canvas.drawText(mTitleText , width/2 - mBound.width()/2,mCircleSize+ 20+ mBound.height()/ 2, mPaint);
}