gpt4 book ai didi

安卓图库一掷千金 : how to calculate the necessary velocity for a fling

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:13 28 4
gpt4 key购买 nike

我必须实现一个画廊,它可以移动到下一张带有动画的幻灯片。我在这里找到了一些解决方案: How to have scrolling animation programmatically

我正在使用这段代码:

//scroll forward or backward
private void scroll(int type){
View selectedV = mG.getSelectedView();
int idx = mG.indexOfChild(selectedV);
switch(type){
case FORWARD:
default:
if(idx<mG.getChildCount()-1)
idx++;
break;
case BACKWARD:
if(idx>0)
idx--;
break;
}
//now scrolled view's child idx in gallery is gotten
View nextView = mG.getChildAt(idx);
//(x,y) in scrolled view is gotten
int x = nextView.getLeft()+nextView.getWidth()/2;
int y = nextView.getTop()+nextView.getHeight()/2;
String out = String.format("x=%d, y=%d", x, y);
Log.i(TAG+".scroll", out);

//Kurru's simulating clicking view
MotionEvent event = MotionEvent.obtain(100, 100, MotionEvent.ACTION_DOWN, x, y, 0);
mG.onDown(event);
boolean res = mG.onSingleTapUp(null);
Log.i(TAG+".scroll", "onSingleTapUp return =" + res);

问题是它只有在我有 3 张图像可见时才有效,而且显然它甚至在某些设备上也不起作用。

但是,当我一次显示一张图片时(它们几乎占据了设备的所有宽度),此方法不起作用。这就是我实现以下方法的原因:

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {

if(e1 == null || e2 == null) return false;
if (isScrollingLeft(e1, e2)) { // Check if scrolling left


if(State.curentZoom==0)
return super.onFling(e1, e2, State.imgWidthBig*1.1f, 0);
else {
scroll(BACKWARD);
return true;
}
} else if (isScrollingRight(e1, e2)) { // Otherwise scrolling right

if(State.curentZoom==0)
return super.onFling(e1, e2, (-1)*State.imgWidthBig*1.1f, 0);
else {
scroll(FORWARD);
return true;
}
} else
return false;

}

使用其他帖子中的代码: How to stop scrolling in a Gallery Widget?

目标:计算正确的 velocityX,以便从一张幻灯片向左或向右平滑滚动到另一张幻灯片。速度以像素/秒计算。如果我提供的速度太小,那么图像会滚动一点并返回到前一个。如果速度太大,那么它会滚动多个图像,但我需要它一张一张地滚动到下一张/上一张图像,即使距离很小。我发现尝试时,最佳值略大于设备宽度,但我想知道是否所有设备都是这种情况。

最佳答案

聚会有点晚了。 AOSP 提供了 2 个类来帮助您计算速度,VelocityTrackerViewConfiguration。跟踪器消耗 MotionEvents 并输出 X/Y 速度。而 ViewConfiguration 声明了不同手势类型的阈值。

下面是一个使用这 2 个类来检测滑动手势的简单示例。

    mVelocityTracker = VelocityTracker.obtain();
mViewConfiguration = ViewConfiguration.get(mContext);

mListView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

final int action = event.getActionMasked();
mVelocityTracker.addMovement(event);

if (action == MotionEvent.ACTION_UP) {
mVelocityTracker.computeCurrentVelocity(1000, mViewConfiguration.getScaledMaximumFlingVelocity());
if (mVelocityTracker.getXVelocity() > mViewConfiguration.getScaledMinimumFlingVelocity()) {
// horizontal fling!
return true;
}
}
return false;
}
});

关于安卓图库一掷千金 : how to calculate the necessary velocity for a fling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12405960/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com