自定義View

一.View結構原理

    Android系統對視圖結構的設計採用了組合模式,即View作爲所有圖形的基類,ViewGoup對View進行擴展爲視圖容器類。

    View定義了繪圖的基本操作:measure(),layout(),draw()。其內部又分別包含了onMeasure(),onLayout(),onDraw()三個子方法。

    1.measure操作

        用於計算視圖的長寬,是final類型的。measure函數又會調用onMeasure().

        onMeasure(),視圖大小將最終在這裏確定。即measure是對onMeasure的包裝,子類可以覆

        蓋onMeasure()來實現計算視圖大小的方式,並通過setMeasuredDimension(width,height)

        保存結果。

    2.layout操作

        設置視圖在屏幕中顯示的位置,final類型,有以下2個基本操作

       (1)setFrame(l,t,r,b),參數即子視圖在父視圖中的具體位置,該函數用於保存這些參數。

       (2)onLayout(),在View函數中什麼都不做,它是爲ViewGroup佈局子視圖用的。

    3.draw操作

        draw操作利用前2步得到的參數將視圖顯示在屏幕上。子類也不應該修改該方法,因爲其內部

      定義了繪圖的基本操作:

        (1)繪製背景;(2)如果要視圖顯示漸變框,這裏會做一些準備工作;(3)繪製視圖本身

      即調用onDraw()。ViewGroup不需要實現該函數,因爲容器是“沒有內容”的,它只需要告訴子    view繪製自己就行了,也就是下面的dispatchDraw()方法;(4)繪製子視圖,即

      dispatchDraw()函數。View不需要實現該方法,容器類必須實現。(5)如果需要(應用程序調

      用了setVerticalFadingEdge之類),開始繪製漸變框;(6)繪製滾動條;

        從上面看出自定義View最少應覆蓋onMeasure()和onDraw()方法。



二.View類的構造方法

(1)繼承已有控件(要實現的控件與已有控件類似時採用該方法)

(2)繼承佈局文件(要組合控件時用),注意此時不用onDraw方法,它是通過inflater視圖然後         addView(view)

(3)繼承View類,使用GDI繪製出組件界面


三.自定義View增加屬性的2種方法:

(1)在View類中定義。通過構造函數引入的AttributeSet去查找XML佈局中的屬性名稱,然後找到它對應引用的資源ID去找值。

    

    <com.example.myp_w_picpathview2.MyView

        android:id="@+id/myView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" 

        Text="@string/hello_world"

        Src="@drawable/xh"/>

        自定義Text和Src屬性。

public class MyView extends View {


    public MyView(Context context, AttributeSet attrs) {

        super(context, attrs);

        int resourceId = 0;

        //獲取xml中自定義的Text和Src屬性

        int textId = attrs.getAttributeResourceValue(null, "Text",0);

        int srcId = attrs.getAttributeResourceValue(null, "Src", 0);

        mtext = context.getResources().getText(textId).toString();

        msrc = srcId;

    }

    

@Override

        protected void onDraw(Canvas canvas) {


        Paint paint = new Paint();

        paint.setColor(Color.RED);

        //獲取圖片

        InputStream is = getResources().openRawResource(msrc); 

        Bitmap mBitmap = BitmapFactory.decodeStream(is);


        int bh = mBitmap.getHeight();

        int bw = mBitmap.getWidth();


        canvas.drawBitmap(mBitmap, 0,0, paint);

        //canvas.drawCircle(40, 90, 15, paint);

        canvas.drawText(mtext, bw/2, 30, paint);

    }

}


(2)通過XML爲View註冊屬性。與Android提供的標準屬性寫法一樣。

<resources>

    <declare-styleable name="MyImageView">

        <attr name="Text" format="reference|string"/>

        <attr name="Oriental">

            <enum name="Horizontal" value="1"/>

            <enum name="Vertical" value="0"/>

        </attr>

        <attr name="Src" format="reference|integer"/>

    </declare-styleable>

</resources>


public class MyImageView extends LinearLayout {


    public MyImageView(Context context, AttributeSet attrs) {

        super(context, attrs);

        int resourceId = -1;


        TypedArray typedArray = context.obtainStyledAttributes(attrs,                                         R.styleable.MyImageView);

        ImageView iv = new ImageView(context);

        TextView tv = new TextView(context);


        int n = typedArray.getIndexCount();

        for (int i = 0; i < n; i ++) {

            int attr = typedArray.getIndex(i);


            switch (attr) {

                case R.styleable.MyImageView_Oriental:

                    resourceId = typedArray.getInt(R.styleable.MyImageView_Oriental, 0);

                    this.setOrientation(resourceId == 1 ? LinearLayout.HORIZONTAL :

                            LinearLayout.VERTICAL);

                    break;

                case R.styleable.MyImageView_Text:

                    resourceId = typedArray.getResourceId(R.styleable.MyImageView_Text, 0);

                    tv.setText(resourceId > 0 ?                                                                      typedArray.getResources().getText(resourceId) :

                            typedArray.getString(R.styleable.MyImageView_Text));

                    break;

                case R.styleable.MyImageView_Src:

                    resourceId = typedArray.getResourceId(R.styleable.MyImageView_Src, 0);

                    iv.setImageResource(resourceId > 0 ?

                         resourceId : R.drawable.ic_launcher);

                    break;

            }

        }


        addView(iv);

        addView(tv);

        typedArray.recycle();

    }

}


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