Android自定义ViewGroup实现弹性滑动效果
作者:yangtianrui95 时间:2022-07-25 19:44:08
自定义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();
}
}
}
来源:http://blog.csdn.net/y874961524/article/details/52752169
标签:Android,ViewGroup,弹性滑动
0
投稿
猜你喜欢
Android开发中ImageLoder进行图片加载和缓存
2023-08-18 10:14:30
Spring实战之协调作用域不同步的Bean操作示例
2023-01-23 02:48:10
Spring mvc拦截器实现原理解析
2023-06-06 15:26:33
Java多线程编程小实例模拟停车场系统
2022-07-07 06:36:29
JAVA中AES加密方法实例分析
2023-12-18 13:09:40
Android编程之控件状态配置文件实例
2023-07-26 02:38:54
Android架构组件Room的使用详解
2022-04-30 14:59:24
小谈Kotlin的空处理的使用
2022-05-05 05:23:24
Java中synchronized锁升级的过程
2022-09-27 16:52:10
一文带你了解C#中的协变与逆变
2022-08-06 22:31:21
C++实现哈夫曼树算法
2023-05-21 01:03:46
Activiti如何启动流程并使流程前进
2023-11-18 18:38:51
C#实现通过winmm.dll控制声音播放的方法
2022-12-02 06:35:17
java实现文件夹解压和压缩
2022-06-07 03:00:39
GC调优实战之高分配速率High Allocation Rate
2021-09-30 22:03:21
判断图片-判断位图是否是黑白图片的方法
2023-06-09 17:20:07
C#线程间通信的异步机制
2023-10-13 16:45:23
Android 中SP与DP的区别实例详解
2021-12-26 22:43:13
在idea中显示springboot面板的方法
2022-01-02 22:00:57
C#实现文字转语音功能
2022-06-18 13:07:59