Android Scroller应用 之 实现自定义ViewGroup的垂直滚动

继上篇大致总结了一些相关的概念后,我也来做个东西试试
所谓前人栽树后人乘凉,每次写文章的时候 ,总会先去baidu一把,不要问我为什么还在用baidu,进入正题,Scroller应用,参考博客:
http://www.cnblogs.com/wanqieddy/archive/2012/05/05/2484534.html (介绍了scroller, VelocityTracker等等一些知识比较全)
http://blog.csdn.net/lmj623565791/article/details/23692439 (这里好多介绍自定义ViewGroup的东西,赞一个)
http://blog.csdn.net/guolin_blog/article/details/16330267 (这是一个系列文章,绝壁的叼)

看下布局
activity_main.xml
    
    
    
    
<com.example.user.scroller_02.MyViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
 
 
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ee342a"
android:gravity="center"
android:text="First Page" />
 
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#adee40"
android:gravity="center"
android:text="Second Page" />
 
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#347cee"
android:gravity="center"
android:text="Third Page" />
 
</com.example.user.scroller_02.MyViewGroup>

自定义的ViewGroup类MyViewGroup.java
    
    
    
    
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();
}
}
这个自定义的布局如何写,在前面的参考博客中介绍好多,重点的地方我做了注释,就不多说了。

MainActivity.java
    
    
    
    
package com.example.user.scroller_02;
 
import android.app.Activity;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
 
public class MainActivity extends Activity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
 
 
private class Dimension {
public int mWidth ;
public int mHeight ;
public Dimension(){}
}
 
private Dimension getFullScreenSize(Activity activity){
Dimension dimen = new Dimension();
Display disp = activity.getWindowManager().getDefaultDisplay();
Point outP = new Point();
disp.getSize(outP);
dimen.mWidth = outP.x ;
dimen.mHeight = outP.y;
return dimen;
}
private Dimension getViewSizeWithTitle(Activity activity){
Dimension dimen = new Dimension();
Rect outRect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
System.out.println("top:"+outRect.top +" ; left: "+outRect.left) ;
dimen.mWidth = outRect.width() ;
dimen.mHeight = outRect.height();
return dimen;
}
private Dimension getViewSize(Activity activity){
Dimension dimen = new Dimension();
// 用户绘制区域
Rect outRect = new Rect();
activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
dimen.mWidth = outRect.width() ;
dimen.mHeight = outRect.height();
// end
return dimen;
}
 
/**
* 获取全屏的尺寸,带标题的View的尺寸,不带标题View尺寸
* @param hasFocus
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
System.out.println("second");
StringBuilder sb = new StringBuilder();
Dimension dimen1 = getFullScreenSize(this);
Dimension dimen2 = getViewSizeWithTitle(this);
Dimension dimen3 = getViewSize(this);
sb.append("屏幕的尺寸 : \n\tWidth: "+dimen1.mWidth + ";\tHeight: "+dimen1.mHeight);
sb.append("\n带标题的View尺寸: \n\tWidth: "+dimen2.mWidth + ";\tHeight: "+dimen2.mHeight);
sb.append("\n绘制区域的View尺寸: \n\tWidth: "+dimen3.mWidth + ";\tHeight: "+dimen3.mHeight);
Log.d("MyViewGroup",sb.toString());
}
}
 
}
公司禁网络了,不能录制gif的图,后续补上。

但是,但是,对于我们这样的人,听到这个词是不是菊花一紧。
在MainActivity中,我们让整个应用全屏显示后,我们才看到每个TextView的大小刚好是一个屏幕 ,如果不全屏,我们慢慢的滑动的时候 会看到,有点小小的问题,这个问题就是下一屏幕的时候 ,会带有上一个屏幕 的内容,我这里不没搞清楚怎么搞,如果有知道的朋友指导我一下,在此先感谢。

你可能感兴趣的:(android,scroller,Vie)