2012-02-03 61 views
0

我已經使用了一個需要屏幕整個空間的圖庫視圖。通常,當用戶向左或向右滑動時,根據用戶的滾動力,圖像左右移動。我希望當用戶向左或向右滑動時,即使用戶幾乎不滑動,每次只移動一個圖像。任何想法?Android Gallery更改幻燈片上的單個圖像

回答

0

您可以編寫自己的事件監聽器來滾動事件,以便更改圖像。

1

您需要創建一個擴展gallery類的自定義庫,然後重寫onfling方法。你可以參考下面的代碼: package hung.view; 公共類CustomGallery擴展庫{

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"/> 

我希望這是有益的爲你。 致以問候