自定义view相关总结:

本文详细解释了Android中自定义View的测量机制(onMeasure)、布局处理(onLayout),以及事件拦截机制(onInterceptTouchEvent)的应用,通过实例代码展示了如何实现一个可横向滑动的View。
摘要由CSDN通过智能技术生成

一 先来看下要注意的点


1 View.java的源码当中并没有对AT_MOST和EXACTLY两个模式做出区分,也就是说所以继承View的自定义view 在wrap_content和match_parent两个模式下是完全相同的,都会是match_parent
因此自定义view时候要注意
1.1、在onDraw当中对padding属性进行处理。

1.2、在onMeasure过程中对wrap_content属性进行处理
如果不重写onMeasure方法,那么自定义view的尺寸默认就和父控件一样大小

2  自定义view的第三个构造函数是用户有自定义样式需求的时候,通过第二个构造函数去xml中获取自定义style,然后调用并第三个构造函数用并赋给其该style参数。
    因此,有自定义样式需求可以看这里

3  通过view的onmearure方法我们可以看到:当View没有设置背景时,默认大小就是Android:minWidth属性,如果没有设置时默认为0
   如果有设置背景,则默认大小为mMinWidth和背景图当中的较大值

4 坐标原点在左上方,除了getRawx(),getRawy()  其他都是相对值

5 自定义view的事件拦截:如果父控件发现满足自己的条件(比如movex>movey)就会onInterceptTouchEvent()里面返回true来进行拦截,否则不拦截交给子view处理
  同样的方式如果子控件发现满足自己的条件,就会通过 getParent().requestDisallowInterceptTouchEvent() 返回请求不拦截的事件。
  这两个事件是逻辑或 运算,只要有一个条件满足,都不拦截。 
  

二 来个实例更好的理解上面的知识点  


  下面的例子一个横向滑屏的自定view包含了,onmeasure,onlayout,事件拦截的参考实例代码
  
  public class Viewpage3 extends ViewGroup {
    private int lastX;
    private int lastY;
    private int currentIndex = 0;
    private int childWidth = 0;
    private Scroller scroller;
    private VelocityTracker tracker;

    public Viewpage3(Context context) {
        super(context);
        init(context);
    }
    public Viewpage3(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    private void init(Context context) {
        scroller = new Scroller(context);
        tracker = VelocityTracker.obtain();
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //测量所有子View
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() == 0) {
            setMeasuredDimension(0, 0);
        } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            View childOne = getChildAt(0);
            int childWidth = childOne.getMeasuredWidth();
            int childHeight = childOne.getMeasuredHeight();
            //View的宽度=单个子View宽度*子View个数,View的高度=子View高度
            setMeasuredDimension(getChildCount() * childWidth, childHeight);
        } else if (widthMode == MeasureSpec.AT_MOST) {
            View childOne = getChildAt(0);
            int childWidth = childOne.getMeasuredWidth();
            //View的宽度=单个子View宽度*子View个数,View的高度=xml当中设置的高度
            setMeasuredDimension(getChildCount() * childWidth, heightSize);
        } else if (heightMode == MeasureSpec.AT_MOST) {
            View childOne = getChildAt(0);
            int childHeight = childOne.getMeasuredHeight();
            //View的宽度=xml当中设置的宽度,View的高度=子View高度
            setMeasuredDimension(widthSize, childHeight);
        }
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int left = 0;
        View child;
        for (int i = 0; i < childCount; i++) {
        child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            childWidth = child.getMeasuredWidth();
            child.layout(left, 0, left + childWidth, child.getMeasuredHeight());
            left += childWidth;
        }
    }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean intercrpt = false;
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
            int deltaX = x - lastX;
            int delatY = y - lastY;
            //当X轴移动的绝对值大于Y轴移动的绝对值时,对事件进行拦截
            if (Math.abs(deltaX) > Math.abs(delatY)) {
                intercrpt = true;
            }
            break;
        }
        lastX = x;
        lastY = y;
        return intercrpt;
    }

    //省略非核心代码.....
}

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值