自定義View

一.View結(jié)構(gòu)原理

創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)巴州,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575

    Android系統(tǒng)對視圖結(jié)構(gòu)的設(shè)計(jì)采用了組合模式,即View作為所有圖形的基類,ViewGoup對View進(jìn)行擴(kuò)展為視圖容器類。

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

    1.measure操作

        用于計(jì)算視圖的長寬,是final類型的。measure函數(shù)又會調(diào)用onMeasure().

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

        蓋onMeasure()來實(shí)現(xiàn)計(jì)算視圖大小的方式,并通過setMeasuredDimension(width,height)

        保存結(jié)果。

    2.layout操作

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

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

       (2)onLayout(),在View函數(shù)中什么都不做,它是為ViewGroup布局子視圖用的。

    3.draw操作

        draw操作利用前2步得到的參數(shù)將視圖顯示在屏幕上。子類也不應(yīng)該修改該方法,因?yàn)槠鋬?nèi)部

      定義了繪圖的基本操作:

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

      即調(diào)用onDraw()。ViewGroup不需要實(shí)現(xiàn)該函數(shù),因?yàn)槿萜魇恰皼]有內(nèi)容”的,它只需要告訴子    view繪制自己就行了,也就是下面的dispatchDraw()方法;(4)繪制子視圖,即

      dispatchDraw()函數(shù)。View不需要實(shí)現(xiàn)該方法,容器類必須實(shí)現(xiàn)。(5)如果需要(應(yīng)用程序調(diào)

      用了setVerticalFadingEdge之類),開始繪制漸變框;(6)繪制滾動(dòng)條;

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

二.View類的構(gòu)造方法

(1)繼承已有控件(要實(shí)現(xiàn)的控件與已有控件類似時(shí)采用該方法)

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

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

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

(1)在View類中定義。通過構(gòu)造函數(shù)引入的AttributeSet去查找XML布局中的屬性名稱,然后找到它對應(yīng)引用的資源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提供的標(biāo)準(zhǔn)屬性寫法一樣。

<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();

    }

}

標(biāo)題名稱:自定義View
文章位置:http://muchs.cn/article20/johsco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站排名、服務(wù)器托管、域名注冊、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)