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

时间:2012-02-03 10:52:55

标签: android scroll imageview gallery

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

2 个答案:

答案 0 :(得分:1)

您需要创建一个扩展库类的自定义库,然后重写onfling方法。您可以参考以下代码: 包hung.view; 公共类CustomGallery扩展了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"/>

我希望它对你有用。 最好的问候

答案 1 :(得分:0)

您可以为可以更改图像的滚动事件编写自己的eventListener。

相关问题