如何将Android gallery中的fling限制为每次fling仅一个项目?
我有一个有几个全屏图像的图库。我想限制投掷动作一次只能推进一张图片(比如HTC Gallery应用程序)。实现这一点的正确/最简单的方法是什么?我找不到任何限制滚动的方法,但我成功地解决了实现/调整此代码的问题:如何将Android gallery中的fling限制为每次fling仅一个项目?,android,gallery,Android,Gallery,我有一个有几个全屏图像的图库。我想限制投掷动作一次只能推进一张图片(比如HTC Gallery应用程序)。实现这一点的正确/最简单的方法是什么?我找不到任何限制滚动的方法,但我成功地解决了实现/调整此代码的问题: 它使用“fling”实现一个gallery只需覆盖gallery小部件的onFling()方法,而不调用超类onFling()方法 这将使图库每刷一次就提前一个项目。我有一个解决方案,虽然它不能保证最多提前一个项目,但它非常简单(很可能与您在代码中手动执行的操作相同):只需降低onF
它使用“fling”实现一个gallery只需覆盖gallery小部件的
onFling()
方法,而不调用超类onFling()
方法
这将使图库每刷一次就提前一个项目。我有一个解决方案,虽然它不能保证最多提前一个项目,但它非常简单(很可能与您在代码中手动执行的操作相同):只需降低onFling参数中的x-velocity。也就是说,覆盖onFling使其看起来像这样:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return super.onFling(e1, e2, velocityX / 4, velocityY);
}
最好的
Michael我也有同样的要求,我刚刚发现,如果我只返回false,那么每次投掷只会滑动一个项目
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
回答问题的代码示例:
public class SlowGallery extends Gallery
{
public SlowGallery(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public SlowGallery(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public SlowGallery(Context context)
{
super(context);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
//limit the max speed in either direction
if (velocityX > 1200.0f)
{
velocityX = 1200.0f;
}
else if(velocityX < -1200.0f)
{
velocityX = -1200.0f;
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
公共类SlowGallery扩展库
{
公共SlowGallery(上下文、属性集属性、int-defStyle)
{
超级(上下文、属性、定义样式);
}
公共SlowGallery(上下文、属性集属性)
{
超级(上下文,attrs);
}
公共SlowGallery(上下文)
{
超级(上下文);
}
@凌驾
公共布尔onFling(MotionEvent e1、MotionEvent e2、float-velocityX、float-velocityY)
{
//限制任一方向的最大速度
如果(速度x>1200.0f)
{
速度x=1200.0f;
}
否则如果(速度X<-1200.0f)
{
速度x=-1200.0f;
}
返回super.onFling(e1、e2、velocityX、velocityY);
}
}
面对同样的问题,我用下面的逻辑解决了这个问题
1->创建一个类,该类应扩展库2->和重写onFling方法 见以下代码:
package com.sra;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;
public class GallView extends Gallery{
public GallView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public GallView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public GallView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
}
使用xml中的此类作为库:
太棒了!这正是我想要的。准确地说,您必须在重写的方法上返回false。我认为应该是:else if(velocityX<-1200.0f)我使用了它。它起作用了。现在我怎样才能让它成为无尽的画廊?我是说。。。最后一张图片之后的第一张图片…这是一张很好的图片,你可以通过一些动画来降低它的速度:D喜欢/2,/3这是另一个可能的答案!
<com.sra.GallView
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="250dip" >
</com.sra.GallView>