java 如何在android中创建可弹跳的滚动视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7470267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create bouncable scrollview in android?
提问by Asif Rafiq
How to create a bounce ScrollView
in Android like iPhone?
如何ScrollView
在像 iPhone 一样的 Android 中创建反弹?
回答by Thien Nguyen
Add effect bounce to scrollview in android
在android中为scrollview添加效果反弹
Step 1: Create new file BounceScrollView in package com.base.view
步骤 1:在包 com.base.view 中创建新文件 BounceScrollView
public class BounceScrollView extends ScrollView
{
private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;
private Context mContext;
private int mMaxYOverscrollDistance;
public BounceScrollView(Context context)
{
super(context);
mContext = context;
initBounceScrollView();
}
public BounceScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
initBounceScrollView();
}
public BounceScrollView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mContext = context;
initBounceScrollView();
}
private void initBounceScrollView()
{
//get the density of the screen and do some maths with it on the max overscroll distance
//variable so that you get similar behaviors no matter what the screen size
final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
final float density = metrics.density;
mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent)
{
//This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance;
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);
}
}
Step 2:At your layout, please change
第 2 步:在您的布局中,请更改
<ScrollView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
to
到
<com.base.view.BounceScrollView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
回答by ultraon
I have improved version of this answer: https://stackoverflow.com/a/13391248/3256989. So my version is (example for HorizontalScrollView):
我已经改进了这个答案的版本:https: //stackoverflow.com/a/13391248/3256989。所以我的版本是(以 HorizontalScrollView 为例):
public class HorizontalOverScrollView extends HorizontalScrollView {
private static final int WIDTH_DEVIDER_OVERSCROLL_DISTANCE = 3;
private TimeInterpolator mInterpolator;
private int mMaxOverscrollDistance;
private int mAnimTime;
private long mStartTime;
/**
* Instantiates {@link HorizontalOverScrollView} object.
*/
public HorizontalOverScrollView(final Context context) {
super(context);
init();
}
/**
* Instantiates {@link HorizontalOverScrollView} object.
*/
public HorizontalOverScrollView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* Instantiates {@link HorizontalOverScrollView} object.
*/
public HorizontalOverScrollView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
final int widthPixels = getContext().getResources().getDisplayMetrics().widthPixels;
mMaxOverscrollDistance = widthPixels / WIDTH_DEVIDER_OVERSCROLL_DISTANCE;
mAnimTime = getContext().getResources().getInteger(android.R.integer.config_mediumAnimTime);
mInterpolator = new DecelerateInterpolator();
}
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
int overScrollDistance = mMaxOverscrollDistance;
if (isTouchEvent) {
mStartTime = AnimationUtils.currentAnimationTimeMillis();
} else {
final long elapsedTime = AnimationUtils.currentAnimationTimeMillis() - mStartTime;
float interpolation = mInterpolator.getInterpolation((float) elapsedTime / mAnimTime);
interpolation = interpolation > 1 ? 1 : interpolation;
overScrollDistance -= overScrollDistance * interpolation;
overScrollDistance = overScrollDistance < 0 ? 0 : overScrollDistance;
}
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, overScrollDistance, maxOverScrollY, isTouchEvent);
}
}
回答by Marc Van Daele
It's called overscroll in Android.
See http://developer.android.com/reference/android/widget/OverScroller.htmland
(for example) http://developer.android.com/reference/android/widget/ListView.html#setOverscrollFooter(android.graphics.drawable.Drawable)
它在 Android 中称为过度滚动。
请参阅http://developer.android.com/reference/android/widget/OverScroller.html和(例如)http://developer.android.com/reference/android/widget/ListView.html#setOverscrollFooter( android.graphics .drawable.Drawable)
It's only available from API level 9 onward.
However, Samsung devices do seem to support overscroll natively in Android 2.2
它仅从 API 级别 9 开始可用。
但是,三星设备似乎确实在 Android 2.2 中原生支持过度滚动
回答by Eduardo Reis
Just use OverScrollDecoratorHelper
只需使用 OverScrollDecoratorHelper
ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
OverScrollDecoratorHelper.setUpOverScroll(scrollView);
HorizontalScrollView horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontal_scroll_view);
OverScrollDecoratorHelper.setUpOverScroll(horizontalScrollView);