Android进阶——自定义View之继承ViewGroup实现自己的ScrollView

xiaoxiao2021-02-28  25

引言

前面很多文章都是总结关于自定义控件中自定义View的,总结了下自定义View的通用套路和开发主要步骤,相信对于大家应该更了解自定义View了吧,今天主要总结自定义控件中的另一大分支——自定义ViewGroup的主要知识。

一、ViewGroup概述

总所周知,Android中View的管理是基于ViewTree的,ViewGroup作为容器负责管理和盛放其他子View,但是他们都是View的子类,所以他们的周期方法的功能大同小异。开发中要实现ViewGroup,我们有很多种方式,比如说继承现有的ViewGroup,像LinearLayout、RelativeLayout、FrameLayout等等,也可以直接继承ViewGroup,无论采用哪一种形式,核心思想和基本流程都是一样的,区别在于有些父类已经替我们实现了部分功能,我们可以直接采用,无须重写对应的方法。

二、ViewGroup重要的成员方法

ViewGroup会依次执行:onMeasure——>onLayout——>【onMeasure——onLayout】——>computeScroll,其中onMeasure——>onLayout可能会重复执行多次取决于布局中的子View数量。

方法名说明getChildCount()获取子View的数量getChildAt(int index)通过索引获取对应的子View对象measureChild(childView,childWidth,childHeight)通知子View自自己进行测量自身的宽高setLayoutParams(ViewGroup.LayoutParams layoutParams)设置ViewGroup的布局参数onMeasure重写用于通知子类自己进行测量自身的宽高onLayout重写用于确定ViewGroup的高度,并遍历设置View的放置位置,直接通过调用子View的layout(w,h)方法computeScrollstartScroll执行过程中即在duration时间内,computeScrollOffset 方法会一直返回false,但当动画执行完成后会返回返加true.当我们执行ontouch或invalidate()或postInvalidate()都会导致computeScroll()这个方法的执行。所以postInvalidate执行后,会去调computeScroll 方法,而这个方法里再去调postInvalidate,这样就可以不断地去调用scrollTo方法了,直到mScroller动画结束,当然第一次时,我们需要手动去调用一次postInvalidate才会去调用。

三、实现自定义ViewGroup的一般步骤

1、继承ViewGroup或者ViewGroup其他子类

2、实现对应的构造方法并完成相关初始化

3、重写onMeasure方法来要求子类自身去完成自身的测量工作

如果ViewGroup不需要解析wrap_content属性的话,简单的模板如下:遍历获取子View并且设置要求的宽高,如果是考虑嵌套ViewGroup的话还得分层遍历。

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //以遍历的方式通知ChildView对自身进行测量 super.onMeasure(widthMeasureSpec, heightMeasureSpec); int childCounts=getChildCount(); for(int i=0;i<childCounts;i++){ View childView=getChildAt(i);//遍历获取所有的子View measureChild(childView,widthMeasureSpec,heightMeasureSpec); } }

4、重写onLayout方法确定ViewGroup的宽高和子View的布局位置

@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { //确定ViewGroup的高度,遍历设置View的放置位置,直接通过调用子View的layout(w,h)方法 int childCounts=getChildCount(); if (childCounts>0) { //计算ViewGroup的高度 MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams(); layoutParams.height = screenHeight * childCounts; setLayoutParams(layoutParams); for (int i = 0; i < childCounts; i++) { View childView = getChildAt(i);//遍历获取所有的子View if(childView.getVisibility()!=childView.GONE) { childView.layout(left,i*screenHeight,right, (i+1)*screenHeight);//修改top、Bottom属性,使其可以依次排下来 } } } }

5、重写onTouchEvent、computeScroll等方法实现自己的交互逻辑

四、自定义ViewGroup实战

接下来就以一个简单的例子实现类似系统ScrollView的控件,对了这个控件有些小bug,懒得处理了

public static int[] getScreenSize(Context context){ WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);//获取WM对象 DisplayMetrics displayMetrics = new DisplayMetrics(); manager.getDefaultDisplay().getMetrics(displayMetrics); int screenPxHeight=displayMetrics.heightPixels;//获取真实屏幕的高度以px为单位 int sceenPxWidth=displayMetrics.widthPixels; return new int[]{sceenPxWidth,screenPxHeight}; } package com.crazymo.scrollview.widget; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Scroller; /** * Auther: Crazy.Mo * DateTime: 2017/5/15 9:40 * Summary: */ public class CustomScrollView extends ViewGroup { private final static String TAG="CustomScrollView"; private int screenHeight; private int lastY,start,end; private Scroller scroller; public CustomScrollView(Context context) { super(context); init(context); } public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context){ screenHeight=ScreenUtil.getScreenSize(context)[1]; scroller=new Scroller(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //以遍历的方式通知ChildView对自身进行测量 super.onMeasure(widthMeasureSpec, heightMeasureSpec); int childCounts=getChildCount(); for(int i=0;i<childCounts;i++){ View childView=getChildAt(i);//遍历获取所有的子View measureChild(childView,widthMeasureSpec,heightMeasureSpec); } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { //确定ViewGroup的高度,遍历设置View的放置位置,直接通过调用子View的layout(w,h)方法 int childCounts=getChildCount(); if (childCounts>0) { //计算ViewGroup的高度 MarginLayoutParams layoutParams = (MarginLayoutParams) getLayoutParams(); layoutParams.height = screenHeight * childCounts; setLayoutParams(layoutParams); for (int i = 0; i < childCounts; i++) { View childView = getChildAt(i);//遍历获取所有的子View if(childView.getVisibility()!=childView.GONE) { childView.layout(left,i*screenHeight,right, (i+1)*screenHeight);//修改top、Bottom属性,使其可以依次排下来 } } } } @Override public boolean onTouchEvent(MotionEvent event) { int y=(int)event.getY();//获取Android坐标系对应的Y值 switch (event.getAction()){ case MotionEvent.ACTION_DOWN: lastY=y; start=getScrollY();//getScrollY表示手机屏幕显示区域左上角y坐标减去CustomScrollView视图左上角y坐标;getScrollX表示手机屏幕显示区域左上角x坐标减去CustomScrollView视图左上角x坐标 LogUtil.showErroLog("Action_Down"+getScaleY()); break; case MotionEvent.ACTION_MOVE: if(!scroller.isFinished()){ scroller.abortAnimation(); } int dy=lastY-y; if(getScrollY()<0){ dy=0; } if(getScrollY()>getHeight()-screenHeight){ dy=0; } LogUtil.showErroLog("Action_Move"+dy); scrollBy(0,dy);//在视图的X、Y方向上各移动0、dy距离,dx>0表示视图(View或ViewGroup)的内容从右向左滑动;反之,从左向右滑动;dy>0表示视图(View或ViewGroup)的内容从下向上滑动;反之,从上向下滑动 lastY=y; break; case MotionEvent.ACTION_UP: end=getScrollY(); int dscrollY=end-start; LogUtil.showErroLog("ACTION_UP"+getScrollY()+"dscrollY"+dscrollY); if(dscrollY>0){ if(dscrollY<screenHeight/3){ scroller.startScroll(0,getScrollY(),0,-dscrollY); }else { scroller.startScroll(0,getScrollY(),0,screenHeight-dscrollY); } }else { if(-dscrollY>screenHeight/3){ scroller.startScroll(0,getScrollY(),0,-dscrollY); }else{ scroller.startScroll(0,getScrollY(),0,-screenHeight-dscrollY); } } break; default: break; } postInvalidate(); return true; } @Override public void computeScroll() { /* *startScroll执行过程中即在duration时间内,computeScrollOffset 方法会一直返回false,但当动画执行完成后会返回返加true. 当我们执行ontouch或invalidate()或postInvalidate()都会导致computeScroll()这个方法的执行。所以postInvalidate执行后, 会去调computeScroll 方法,而这个方法里再去调postInvalidate, 这样就可以不断地去调用scrollTo方法了,直到mScroller动画结束,当然第一次时,我们需要手动去调用一次postInvalidate才会去调用。 */ super.computeScroll(); if(scroller.computeScrollOffset()){ LogUtil.showErroLog("computeScroll"); scrollTo(0,scroller.getCurrY());//x=10,表示视图从右向左移动了10个单位,y=200,表示视图从下到上移动了200个单位 postInvalidate(); } } }

简单实用

<?xml version="1.0" encoding="utf-8"?> <com.crazymo.scrollview.widget.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.crazymo.scrollview.MainActivity"> <ImageView android:layout_width="match_parent" android:layout_height="400dp" android:src="@mipmap/src_jing"/> <ImageView android:layout_width="match_parent" android:layout_height="400dp" android:src="@mipmap/ic_launcher"/> </com.crazymo.scrollview.widget.CustomScrollView>
转载请注明原文地址: https://www.6miu.com/read-34091.html

最新回复(0)