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()方法的实现
效果图如下:
在这里插入图片描述

发布了43 篇原创文章 · 获赞 60 · 访问量 6765

猜你喜欢

转载自blog.csdn.net/yuhang01/article/details/102958830