第五更 android 自定义view(一)

先写几点注意事项
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);
}

效果图

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章