package com.example.user.scroller_02;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;
/**
* Created by David on 2015/9/25.
*/
public class MyViewGroup extends ViewGroup {
private static final String TAG = "MyViewGroup";
private Context mContext;
private int mScreenHeight, mScreenWidth;
private int mLastY;
private int mChildCount;
private Scroller mScroller;
private int mScrollStart;
private VelocityTracker mVelocityTracker;
public MyViewGroup(Context context) {
this(context, null);
}
public MyViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.d(TAG,"onAttachToWindow");
mVelocityTracker = VelocityTracker.obtain();
}
private void init(Context context) {
mContext = context;
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
//获取屏幕的宽高(包括了status bar + title bar),这就会导致TextView的字体为何没有居中显示
mScreenHeight = outMetrics.heightPixels;
mScreenWidth = outMetrics.widthPixels;
Log.d(TAG,"mScreenHeight = " + mScreenHeight + " ,mScreenWidth = " + mScreenWidth);
mScroller = new Scroller(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mChildCount = getChildCount();
int mWidthMeasureSpec = MeasureSpec.makeMeasureSpec(mScreenWidth, MeasureSpec.EXACTLY);
int mHeightMeasureSpec = MeasureSpec.makeMeasureSpec(mScreenHeight, MeasureSpec.EXACTLY);
/*定义子View的大小以及规格
当然也可以遍历子View,然后用measureChild()
这里这样用是因为,子View的大小和规格都是统一的
*/
measureChildren(mWidthMeasureSpec, mHeightMeasureSpec);
//设置屏幕显示区域的大小,这里你可以随意调整,将会看到不同的效果
setMeasuredDimension(mWidthMeasureSpec, mHeightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int mCount = getChildCount();
MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
params.height = mScreenHeight * mCount;
setLayoutParams(params);
for (int j = 0; j < mCount; j++) {
View child = getChildAt(j);
child.layout(l, j * mScreenHeight, r, (j + 1) * mScreenHeight);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
mVelocityTracker.addMovement(event);
int y = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
//如果它在滚,就继续让它滚,不要打扰,不要这个ACTION_DOWN事件
if (!mScroller.isFinished()) {
return false;
}
mLastY = y;
mScrollStart = getScrollY();
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "ACTION_MOVE");
int dy = y - mLastY;
int mScrollY = getScrollY();
//向上移动,如果超出了ViewGroup的边界 ,自动让它滚动到(0,0)
if (mScrollY - dy <= 0) {
scrollTo(0, 0);
}
//向下滚动 ,如果超出了ViewGroup的边界,自动让它滚动到(0,(mChildCount - 1) * mScreenHeight)
else if (mScrollY - dy >= (mChildCount - 1) * mScreenHeight) {
scrollTo(0, (mChildCount - 1) * mScreenHeight);
}
//正常情况就按照正常情况滚动
else {
scrollBy(0, -dy);
}
mLastY = y;
break;
case MotionEvent.ACTION_UP:
//滚了多少距离
int dScrollY = Math.abs(getScrollY() - mScrollStart);
//向上滑动,dScrollY为正值
if (getScrollY() - mScrollStart > 0) {
if (getScrollY() - mScrollStart > mScreenHeight / 2 || getVelocity() > 600) {
//达到要求就向上滚到下一页,向上滚dy就要为正值,还需要滚mScreenHeight - dScrollY
mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY);
} else {
//没有达到要求就向下滚回原来的位置,向下滚动dy就要为负值,回滚刚才已经滚过的距离-dScrollY
mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
}
}
//向下滑动,dScrollY为负值
if(getScrollY() - mScrollStart < 0) {
if (mScrollStart - getScrollY() > mScreenHeight / 2 || getVelocity() > 600) {
//达到要求,向下滚动到上一页,向下dy就为负值,还要滚 -(mScreenHeight - dScrollY)
mScroller.startScroll(0, getScrollY(), 0, dScrollY - mScreenHeight);
} else {
//没有达到要求,向上回滚,向上dy就为正值,回滚到刚才的位置dScrollY
mScroller.startScroll(0, getScrollY(), 0, dScrollY);
}
}
postInvalidate();
break;
}
//代表接收这个事件
return true;
}
private int getVelocity() {
mVelocityTracker.computeCurrentVelocity(1000);
int mYVelocity = (int) Math.abs(mVelocityTracker.getYVelocity());
return mYVelocity;
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(0, mScroller.getCurrY());
postInvalidate();
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mVelocityTracker.recycle();
}
}