自定義控件分析2

上一篇簡單的使用了一下自定義控件的構造方法和ondraw

使用xml和代碼動態獲取兩種添加屬性的方法。

這一篇使用還是對上一篇使用的練習鞏固吧。

添加屬性使用xml註冊。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomText">
        <attr name="text" format="string"/>
        <attr name="textColor" format="string"/>
        <attr name="textSize" format="string"/>
    </declare-styleable>
</resources>

然後是繼承view使用

public class CustomText extends View{
    public CustomText(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }
    String text;
    int textColor;
    int textSize;
    Paint paint;
    Rect rect;
    public CustomText(Context context, AttributeSet attrs, int i) {
        super(context, attrs, i);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomText, i, 0);
        int n = typedArray.getIndexCount();
        for(int j = 0;j<n;j++){
            int attr = typedArray.getIndex(i);
            switch (attr){
                case R.styleable.CustomText_text:

                    text = typedArray.getString(attr);
                    break;
                case R.styleable.CustomText_textColor:

                    textColor = typedArray.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomText_textSize:

                    textSize = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, getResources().getDisplayMetrics()));
                    break;
            }
        }
        typedArray.recycle();

        paint = new Paint();
        //設置字體大小
        paint.setTextSize(textSize);
        //計算文字的寬度和高度
        rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //設置背景顏色以及位置
        paint.setColor(Color.YELLOW);
        //得到的是像素  爲400和200  可以由dp轉化來
       // http://blog.csdn.net/wangbofei/article/details/7795430  關於getMeasuredWidth() 和 getWidth()區別
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        canvas.drawRect(0, 0,measuredWidth ,measuredHeight , paint);
        //設置字體顏色以及位置
        paint.setColor(textColor);
        int width = getWidth();
        int height = getHeight();
        //文字的寬高
        int width1 = rect.width();
        int height1 = rect.height();
        canvas.drawText(text,width/2-width1/2,height/2-height1,paint);
    }
}



在xml裏面使用

 <com.example.administrator.customcontrols3.CustomText
       android:layout_width="200dp"
       android:layout_height="100dp"
       app:text="adsfa"
       app:textColor="#666666"
       app:textSize="18"
       />

當然還要在頭部定義(studio定義)

xmlns:app="http://schemas.android.com/apk/res-auto"
這一篇相對於上一篇要深入一點
http://blog.csdn.net/lmj623565791/article/details/24252901   依然是理解基礎上整理下

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