2011-04-01 63 views
4

我對Android動畫和手勢比較陌生。圖片之間滑動android

我有15張圖片想要滑動。每次只有一個圖像出現在屏幕上,當我在第一張圖像上滑動L-> R時,第二張圖像會顯示出來,等等 - 就像幻燈片一樣。我看了一下Android Gallery教程,但我不想顯示縮略圖。我的理解是使用ImageView並不斷更改圖像。這是正確的方式還是有更好的方法呢?

+1

您仍然可以使用圖庫;只需將其全屏並首先使用較大的圖像即可。 – 2011-04-01 16:17:58

+0

謝謝Yoni。隨着畫廊,我不知道如何得到相同的投影效果。此外,在畫廊縮略圖 - 我需要顯示文字,當縮略圖被點擊 - 我需要打開一個新的畫廊。 – 2011-04-01 17:42:10

+0

謝謝Yoni!我使用了Gallery,將圖像製作成全屏,然後使用這裏的合併教程:http://developer.android.com/resources/articles/layout-tricks-merge.html – 2011-04-04 14:40:37

回答

7

這樣你就不會看到投擲效果。

有一種方式與畫廊做到這一點。

clreate這樣的galery:

<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:id="@+id/HorizontalGallery" 
    android:gravity="center_vertical" android:spacing="2px"/> 

在getview你必須:

public View getView(int position, View convertView, ViewGroup parent) { 

ImageView i = new ImageView(_Context); 

i.setImageResource(R.drawable.YourPicture); 
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); 

//setting the scale: 

int viewWidthtmp; 
int viewHeighttmp; 

if(getHeight() == 0) 
{ 
    if(_horizGallery.getWidth() == 0){ 
     viewWidthtmp = _horizGallery.getWidth(); 
     viewHeighttmp = _horizGallery.getHeight(); 
    } 
    else 
    { 
     viewWidthtmp = _screenWidth; 
     viewHeighttmp = _screenHeight; 
    } 

//getting the size of the image. 
BitmapFactory.Options o = new BitmapFactory.Options(); 
o.inJustDecodeBounds = true; //returns null, but fills the out methods 
bm = BitmapFactory.decodeResource(getResources(), R.drawable.YourPicture, o); 
if(o.outHeight> viewHeight || o.outWidth> viewWidth) 
    {i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);} 
else 
    {i.setScaleType(ImageView.ScaleType.FIT_CENTER);} 

//DO NOT ADD the line below 
//i.setBackgroundResource(mGalleryItemBackground); 

return i; 

} 

你也要申報2級全局變量的變量,並在活動的OnCreate初始化它們。

public class ScrollingGallery extends Activity 
{ 
    private int _screenWidth; 
    private int _screenHeight; 


... 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.scrollingallery); 

     Display display = getWindowManager().getDefaultDisplay(); 
     _screenWidth = display.getWidth(); 
     _screenHeight = display.getHeight(); 

... 

之後,你只需要用定時器畫廊滾動。

如果我沒有弄錯,這應該與整頁畫廊一起工作。代碼有點長,我只是寫了它,所以可能有一個錯誤。

+0

感謝您的努力!我實際上不得不使用投球,因爲只有當用戶在屏幕上水平滑動他的手指時,圖像纔會改變。 – 2011-04-01 17:21:08

+1

畫廊一炮而紅。如果你希望只有一個圖像滾動,而lingigng轉到http://stackoverflow.com/questions/2373617/how-to-stop-scrolling-in-a-gallery-widget。 – 2011-04-01 19:52:48

+0

謝謝!這有幫助! – 2011-04-04 14:25:43