Android自定义ViewGroup实现弹性滑动效果

所属分类: 软件编程 / Android 阅读数: 68
收藏 0 赞 0 分享

自定义View实现一个弹性滑动的效果,供大家参考,具体内容如下

实现原理

onMeasure()中测量所有子View

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // 测量所有子View
  int count = getChildCount();
  for (int i = 0; i < count; i++) {
   View childView = getChildAt(i);
   measureChild(childView, widthMeasureSpec, heightMeasureSpec);
  }
  setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
 }

onLayout()中,将所有的子View按照位置依次往下排列

@Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // 设置ViewGroup的高度,对所有子View进行排列
  int childCount = getChildCount();
  MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
  params.height = mScreenHeight * childCount;
  for (int i = 0; i < childCount; i++) {
   View childView = getChildAt(i);
   if (childView.getVisibility() != View.GONE) {
    // 给每个ChildView放置在指定位置
    childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);
   }
  }
 }

onTouchEvent()中处理滑动

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    mLastY = (int) event.getY();
    mStart = getScrollY();
    return true;
   case MotionEvent.ACTION_MOVE:
    if (!mScroller.isFinished()) {
     // 终止滑动
     mScroller.abortAnimation();
    }
    int offsetY = (int) (mLastY - event.getY());
    Log.d(TAG, "onTouchEvent: getScrollY: " + getScrollY());
    Log.d(TAG, "onTouchEvent: offsetY " + offsetY);
    // 到达顶部,使用offset判断方向
    if (getScrollY() + offsetY < 0) { // 当前已经滑动的 Y 位置
     offsetY = 0;
    }
    // 到达底部
    if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) {
     offsetY = 0;
    }
    scrollBy(0, offsetY);
    // 滑动完成后,重新设置LastY位置
    mLastY = (int) event.getY();
    break;
   case MotionEvent.ACTION_UP:
    mEnd = getScrollY();
    int distance = mEnd - mStart;
    if (distance > 0) { // 向上滑动
     if (distance < mScreenHeight / 3) {
      Log.d(TAG, "onTouchEvent: distance < screen/3");
      // 回到原来位置
      mScroller.startScroll(0, getScrollY(), 0, -distance);
     } else {
      // 滚到屏幕的剩余位置
      mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance);
     }
    } else {    // 向下滑动
     if (-distance < mScreenHeight / 3) {
      mScroller.startScroll(0, getScrollY(), 0, -distance);
     } else {
      mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance);
     }
    }
    postInvalidate();
  }
  return super.onTouchEvent(event);
 }

其中ACTION_UP这段代码是处理弹性滑动的

case MotionEvent.ACTION_UP:
    mEnd = getScrollY();
    int distance = mEnd - mStart;
    if (distance > 0) { // 向上滑动
     if (distance < mScreenHeight / 3) {
      Log.d(TAG, "onTouchEvent: distance < screen/3");
      // 回到原来位置
      mScroller.startScroll(0, getScrollY(), 0, -distance);
     } else {
      // 滚到屏幕的剩余位置
      mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance);
     }
    } else {    // 向下滑动
     if (-distance < mScreenHeight / 3) {
      mScroller.startScroll(0, getScrollY(), 0, -distance);
     } else {
      mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance);
     }
    }
    postInvalidate();

完整代码

public class ScrollViewGroup extends ViewGroup {

 private static final String TAG = "ScrollView";

 private Scroller mScroller;
 private int mScreenHeight; // 窗口高度
 private int mLastY;
 private int mStart;
 private int mEnd;

 public ScrollViewGroup(Context context) {
  this(context, null);
 }

 public ScrollViewGroup(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  mScroller = new Scroller(context);
  // 获取屏幕高度
  WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics metrics = new DisplayMetrics();
  windowManager.getDefaultDisplay().getMetrics(metrics);
  mScreenHeight = metrics.heightPixels;
  Log.d(TAG, "ScrollViewGroup: ScreenHeight " + mScreenHeight);
 }


 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // 测量所有子View
  int count = getChildCount();
  for (int i = 0; i < count; i++) {
   View childView = getChildAt(i);
   measureChild(childView, widthMeasureSpec, heightMeasureSpec);
  }
  setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
 }

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // 设置ViewGroup的高度,对所有子View进行排列
  int childCount = getChildCount();
  MarginLayoutParams params = (MarginLayoutParams) getLayoutParams();
  params.height = mScreenHeight * childCount;
  for (int i = 0; i < childCount; i++) {
   View childView = getChildAt(i);
   if (childView.getVisibility() != View.GONE) {
    // 给每个ChildView放置在指定位置
    childView.layout(l, i * mScreenHeight, r, (i + 1) * mScreenHeight);
   }
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    mLastY = (int) event.getY();
    mStart = getScrollY();
    return true;
   case MotionEvent.ACTION_MOVE:
    if (!mScroller.isFinished()) {
     // 终止滑动
     mScroller.abortAnimation();
    }
    int offsetY = (int) (mLastY - event.getY());
    Log.d(TAG, "onTouchEvent: getScrollY: " + getScrollY());
    Log.d(TAG, "onTouchEvent: offsetY " + offsetY);
    // 到达顶部,使用offset判断方向
    if (getScrollY() + offsetY < 0) { // 当前已经滑动的 Y 位置
     offsetY = 0;
    }
    // 到达底部
    if (getScrollY() > getHeight() - mScreenHeight && offsetY > 0) {
     offsetY = 0;
    }
    scrollBy(0, offsetY);
    // 滑动完成后,重新设置LastY位置
    mLastY = (int) event.getY();
    break;
   case MotionEvent.ACTION_UP:
    mEnd = getScrollY();
    int distance = mEnd - mStart;
    if (distance > 0) { // 向上滑动
     if (distance < mScreenHeight / 3) {
      Log.d(TAG, "onTouchEvent: distance < screen/3");
      // 回到原来位置
      mScroller.startScroll(0, getScrollY(), 0, -distance);
     } else {
      // 滚到屏幕的剩余位置
      mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - distance);
     }
    } else {    // 向下滑动
     if (-distance < mScreenHeight / 3) {
      mScroller.startScroll(0, getScrollY(), 0, -distance);
     } else {
      mScroller.startScroll(0, getScrollY(), 0, -mScreenHeight - distance);
     }
    }
    postInvalidate();
  }
  return super.onTouchEvent(event);
 }


 @Override
 public void computeScroll() {
  if (mScroller.computeScrollOffset()) {
   scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
   postInvalidate();
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多