1.重新測量、布局
繼承ViewGroup重寫onMeasure和onLayout方法
1)在onMeasure中計算childVIew的測量值及模式,并設置自己的寬高
測量子View:
方法1:調用 measureChildren(widthMeasureSpec, heightMeasureSpec);
方法2:遍歷childVIew調用
measureChild(childVIew, widthMeasureSpec, heightMeasureSpec);
設置自身ViewGro寬高:
設置自定義的控件MyViewGroup的大小,如果是MeasureSpec.EXACTLY則直接使用父ViewGroup傳入的寬和高,否則設置為自己計算的寬和高
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width, heightMode == MeasureSpec.EXACTLY ? heightSize : height);
2)在onLayout中為每個子View設置布局,可以獲取每個子View并分別得到子View坐標,調用child.layout(left, top,right , bottom)為子View設置布局位置。
2.滑動事件處理
3)重寫onInterceptTouchEvent,在MotionEvent.ACTION_MOVE事件中通過水平、豎直方向位移大小判斷,并對水平方向事件進行攔截(返回true為攔截,由自己處理)
if (Math.abs(deltaX) > Math.abs(deltaY)) {return true;}
4)重寫onTouchEvent,在MotionEvent.ACTION_MOVE調用scrollBy(-deltaX, 0)實現左右滑動;在MotionEvent.ACTION_UP用scroller.startScroll(getScrollX(), 0, dx, 0, 500)限定ViewGroup左右邊界
下面貼上代碼,結合代碼看便于理解:
public class ZhanfHorizontalScrollview extends ViewGroup {
private int childCount;//子View數量
private int childIndex;//子View索引
private int measuredHeight;//子View的高度
private int measuredWidth;//子View的寬度
private Scroller scroller;//彈性滑動對象,用于實現View的彈性滑動
private VelocityTracker velocityTracker;//速度追蹤,
public ZhanfHorizontalScrollview(Context context) {
this(context, null);
}
public ZhanfHorizontalScrollview(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ZhanfHorizontalScrollview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
scroller = new Scroller(getContext());
velocityTracker = VelocityTracker.obtain();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 獲得它的父容器為它設置的測量模式和大小
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
// 1.計算自定義的ViewGroup中所有子控件的大小
// measureChildren(widthMeasureSpec, heightMeasureSpec);
int height = 0;
int width = 0;
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child != null && child.getVisibility() != GONE) {
// 2.計算自定義的ViewGroup中所有子控件的大小
measureChild(child, widthMeasureSpec, heightMeasureSpec);
measuredHeight = child.getMeasuredHeight();
measuredWidth = child.getMeasuredWidth();
height = Math.max(height, measuredHeight);
width += measuredWidth;
}
}
}
// 設置自定義的控件MyViewGroup的大小,如果是MeasureSpec.EXACTLY則直接使用父ViewGroup傳入的寬和高,否則設置為自己計算的寬和高
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width, heightMode == MeasureSpec.EXACTLY ? heightSize : height);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
childCount = getChildCount();
int height = 0;
int width = 0;
if (childCount > 0) {
for (int index = 0; index < childCount; index++) {
View child = getChildAt(index);
if (child.getVisibility() != GONE && child != null) {
measuredHeight = getMeasuredHeight();
measuredWidth = getMeasuredWidth();
height = Math.max(height, measuredHeight);
child.layout(width, 0, width + measuredWidth, height);
}
width += measuredWidth;
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
velocityTracker.addMovement(event);
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//如果動畫還沒有結束,再次點擊時結束上次動畫,即開啟這次新的ACTION_DOWN的動畫
if (!scroller.isFinished()) {
scroller.abortAnimation();
}
break;
case MotionEvent.ACTION_MOVE:
int deltaX = x - mLastX;
scrollBy(-deltaX, 0);
break;
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();//View的左邊緣 - View內容的左邊緣 位置的像素點
// int scrollToChildIndex = scrollX / measuredWidth;
velocityTracker.computeCurrentVelocity(1000);
float xVelocity = velocityTracker.getXVelocity();//獲取X方向手指滑動的速度,之前必須調用computeCurrentVelocity()方法
if (Math.abs(xVelocity) > 200) {//當滑動速度>200Px/S時
childIndex = xVelocity > 0 ? childIndex - 1 : childIndex + 1;
} else {
childIndex = (scrollX + measuredWidth / 2) / measuredWidth;
}
childIndex = Math.max(0, Math.min(childIndex, childCount - 1));//限定childIndex在0到childCount之間
int dx = childIndex * measuredWidth - scrollX;
scroller.startScroll(getScrollX(), 0, dx, 0, 500);//up 時自動滾動到
invalidate();
break;
}
mLastX = x;
mLastY = y;
return true;
}
@Override
public void computeScroll() {
if (scroller.computeScrollOffset()) {
scrollTo(scroller.getCurrX(), scroller.getCurrY());
postInvalidate();
}
}
//分別記錄上次滑動的坐標
private int mLastX = 0;
private int mLastY = 0;
//分別記錄上次滑動的坐標(onINterceptTouchEvent)
private int mLastXIntercept = 0;
private int mLastYIntercept = 0;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int x = (int) ev.getX();
int y = (int) ev.getY();
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (!scroller.isFinished()) {
scroller.abortAnimation();
}
break;
case MotionEvent.ACTION_MOVE:
int deltaX = x - mLastXIntercept;
int deltaY = y - mLastYIntercept;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
return true;
}
break;
case MotionEvent.ACTION_UP:
break;
}
mLastX = x;
mLastY = y;
mLastXIntercept = x;
mLastYIntercept = y;
return super.onInterceptTouchEvent(ev);
}
}
好了,有關水平滑動的實現就到這里。記住,自定義ViewGroup無非就是對子VIew進行逐個measure、layout,再進行相應情況的事件攔截、處理的過程,細細做下來也沒那么難
參考:
《Android開發藝術探索》——3.5View的滑動沖突
Android 手把手教您自定義ViewGroup(一)