android--------自訂控制項(二)

來源:互聯網
上載者:User

標籤:AC   oid   ted   顯示   strong   詳細   也會   canvas   包括   

 

前面簡單的講述了Android中自訂控制項的理論和流程圖,今天通過代碼來詳細的講解一下其中的方法

 

首先先建立一個類 CircularView 繼承於 View,之後實現構造方法(初始化步驟)

public class CircularView extends View {public CircularView(Context context) {        super(context);    }    public CircularView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CircularView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public CircularView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);    }

 

View的建構函式有四種重載,View建構函式的參數有多有少,

有四個參數的建構函式在API21的時候才添加上,暫不考慮。

有三個參數的建構函式中第三個參數是預設的Style,這裡的預設的Style是指它在當前Application或Activity所用的Theme中的預設Style,且只有在明確調用的時候才會生效

 //調用了三個參數的建構函式,明確指定第三個參數  this(context, attrs, com.android.internal.R.attr.imageButtonStyle);

 

由於三個參數的建構函式第三個參數一般不常用,所以也暫不考慮

//一般在直接New一個View的時候調用。public void CircularView(Context context) {}//一般在layout檔案中使用的時候會調用,關於它的所有屬性(包括自訂屬性)都會包含在attrs中傳遞進來。public void CircularView(Context context, AttributeSet attrs) {}

 

使用方式:(布局檔案中)

    <com.zhangqie.customcontrol.demo1.CircularView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:background="@color/colorAccent"        />

 

onMeasure方法 (測量View大小)

View的大小不僅由自身所決定,同時也會受到父控制項的影響,為了我們的控制項能更好的適應各種情況,一般會自己進行測量。

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int widthsize  MeasureSpec.getSize(widthMeasureSpec);      //取出寬度的確切數值    int widthmode  MeasureSpec.getMode(widthMeasureSpec);      //取出寬度的測量模式        int heightsize  MeasureSpec.getSize(heightMeasureSpec);    //取出高度的確切數值    int heightmode  MeasureSpec.getMode(heightMeasureSpec);    //取出高度的測量模式}

 

  

從上面可以看出 onMeasure 函數中有 widthMeasureSpec 和 heightMeasureSpec 這兩個 int 類型的參數, 毫無疑問他們是和寬高相關的, 但它們其實不是寬和高, 而是由寬、高和各自方向上對應的測量模式來合成的一個值:

 

測量模式一共有三種, 被定義在 Android 中的 View 類的一個內部類View.MeasureSpec中:

模式 位元值 描述
UNSPECIFIED 00 預設值,父控制項沒有給子view任何限制,子View可以設定為任意大小。
EXACTLY 01 表示父控制項已經確切的指定了子View的大小。
AT_MOST 10 表示子View具體大小沒有尺寸限制,但是存在上限,上限一般為父View大小。

 

 

onSizeChanged方法 確定View的大小,這個函數在視圖大小發生改變時調用

    /***     * 確定View的大小(這個函數在視圖大小發生改變時調用。)     *     * 寬度,高度,上一次寬度,上一次高度。     * 只需關注 寬度(w), 高度(h) 即可,這兩個參數就是View最終的大小。     * @param w     * @param h     * @param oldw     * @param oldh     */    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        Log.i(TAG,"onSizeChanged");    }

 

 

 

onLayout 方法 確定子View布局位置

 /****     * 布局-Layout過程用於設定視圖在螢幕中顯示的位置,onLayout一般只會在自訂ViewGroup中才會使用     *     * @param changed     * @param left     * @param top     * @param right     * @param bottom     */    @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {        super.onLayout(changed, left, top, right, bottom);        Log.i(TAG,"onLayout");    }

 

確定布局的函數是onLayout,它用於確定子View的位置,在自訂ViewGroup中會用到,他調用的是子View的layout函數。

在自訂ViewGroup中,onLayout一般是迴圈取出子View,然後經過計算得出各個子View位置的座標值,然後用以下函數設定子View位置。

 

onDraw 方法,繪製內容

 /***     * 繪製-draw過程主要用於利用前兩步得到的參數,將視圖顯示在螢幕上,到這裡也就完成了整個的視圖繪製工作     * @param canvas     */    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }

 

onDraw是實際繪製的部分,也就是我們真正關心的部分,使用的是Canvas繪圖。

 

android--------自訂控制項(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.