android自定義View之從入門到放棄(一) 記錄學習

今天正式開始接觸自定義view
其中的重要方法體如下:
1.onMeasure() 對view進行測量
2.onDraw() 在canvas進行圖形的繪製
3.onLayout()確定圖形顯示的位置
4.onTouchEvent() 監聽觸摸事件回調
5.onSizeChanged() 組件大小改變回掉

然後動手做的一個簡單demo
直接上代碼:詳細註釋都在代碼中

public class HeroView extends View {
    public HeroView(Context context) {  //在代碼中創建自定義view
        super(context);
    }

    public HeroView(Context context, AttributeSet attrs) {//在xml中創建時調用,可自定義屬性  textColor,background,,
        super(context, attrs);
    }

    public HeroView(Context context, AttributeSet attrs, int defStyleAttr) {//在xml中創建調用,且自定了樣式  style,
        super(context, attrs, defStyleAttr);
    }
    //獲取viewd的測量模式和view的大小
    /**
     * EXACTLY :將layout_width 或 layout_height指定爲指定數值或者match_parent(佔父view的大小)    不重寫onMeasure()時 默認爲這種模式
     * AT_MOST :將layout_width 或 layout_height指定爲wrap_content
     * UNSPECIFIED :不指定大小的測量模式  想多大就多大
     *
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasuredWidth(widthMeasureSpec),MeasuredHeight(heightMeasureSpec));//將測量後的寬高進行顯示
    }
    //獲取layout_height 的模式一個所佔的大小
    private int MeasuredHeight(int heightMeasureSpec) {
        int result = 0;
        //獲取寬度的模式和size
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int spcSize = MeasureSpec.getSize(heightMeasureSpec);
        if(specMode==MeasureSpec.EXACTLY){
            result = spcSize;
        }else {
            result =200;
            if(specMode==MeasureSpec.AT_MOST){
                result = Math.min(result,spcSize);//求兩數之間最小的
            }

        }
        return result;
    }

    //獲取layout_width 的模式一個所佔的大小
    private int MeasuredWidth(int widthMeasureSpec) {
        int result = 0;
        //獲取寬度的模式和size
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int spcSize = MeasureSpec.getSize(widthMeasureSpec);
        if(specMode==MeasureSpec.EXACTLY){
            result = spcSize;
        }else {
           result =200;
           if(specMode==MeasureSpec.AT_MOST){
               result = Math.min(result,spcSize);//求兩數之間最小的
           }

        }
        return result;
    }

    /**
     *
     * @param canvas   畫板  paint畫筆作畫      其他地方拿到canvas  Canvas canvas = new Canvas(bitmap)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

在xml文件中進行使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    tools:context=".MainActivity">

<com.example.zidingyidemo.widget.HeroView
    android:background="#FF25"
    android:layout_width="400dp"
    android:layout_height="400dp"/>

</LinearLayout>

總結:對於自定義的view的layout_width 和layout_height 屬性的設置 當寬高都爲wrapcontent時 自定義的view寬高都爲200dp
主要時對於onMeasure()方法的實現
效果圖如下:
在這裏插入圖片描述

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