Android Gallery 更改幻灯片上的单个图像

发布于 2025-01-01 15:33:50 字数 103 浏览 5 评论 0原文

我使用了画廊视图,它占据了整个屏幕空间。通常,当用户向左或向右滑动时,根据用户滚动力,图像会左右移动。我希望当用户向左或向右滑动时,即使用户几乎不滑动它,一次也只有一个图像移动。有什么想法吗?

I have used a gallery view which takes the whole space of screen. Normally when user slides left or right, depending upon the user scrolling force, images are moved right and left. I want that when user slides left or right, only a single image moves at a time even if user slides it hardly. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2025-01-08 15:33:50

您需要创建一个扩展画廊类的自定义画廊,然后重写 onfling 方法。您可以参考下面的代码:
包挂.view;
public class CustomGallery extends Gallery {

public CustomGallery(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public CustomGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    //return super.onFling(e1, e2, velocityX, velocityY);
    //return false ;
    int kEvent;
      if(isScrollingLeft(e1, e2)){ //Check if scrolling left
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
      }
      else{ //Otherwise scrolling right
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
      }
      onKeyDown(kEvent, null);
      return true;
}

 private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
     return e2.getX() > e1.getX();
  }

}

最后,在布局 xml 文件中,将默认画廊替换为自定义画廊,示例:

    <hung.view.CustomGallery android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:spacing="8px"
        android:clipToPadding="true"/>

我希望它对您有用。
此致

You need to create a custom gallery that extends gallery class, and then you override onfling method. You can refer the code below :
package hung.view;
public class CustomGallery extends Gallery {

public CustomGallery(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public CustomGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    // TODO Auto-generated method stub
    //return super.onFling(e1, e2, velocityX, velocityY);
    //return false ;
    int kEvent;
      if(isScrollingLeft(e1, e2)){ //Check if scrolling left
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
      }
      else{ //Otherwise scrolling right
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
      }
      onKeyDown(kEvent, null);
      return true;
}

 private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
     return e2.getX() > e1.getX();
  }

}

The last, in the layout xml file, you replace default gallery by custom gallery, example :

    <hung.view.CustomGallery android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:spacing="8px"
        android:clipToPadding="true"/>

I hope it is useful for you.
Best regards

末蓝 2025-01-08 15:33:50

您可以为滚动事件编写自己的事件监听器,您可以在其中更改图像。

You can write your own eventListener for scrolling event where you can change the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文