2014-03-13 40 views
0

我是新來的Android,所以我有一個關於顯示圖像我需要一個代碼,以動態顯示圖像

enter image description here

我已經創造了main.xml文件按鈕,工作正常,但它顯示的查詢只有一個圖像。我希望所有的圖像能夠一個接一個地顯示,這樣我就可以滑動到左側並檢查左側顯示的可繪製文件夾中的所有圖像。

我已經使用它的代碼是

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 



<ImageView 
    android:id="@+id/image" 
    android:layout_width="244dp" 
    android:layout_height="0dip" 
    android:layout_weight="0.85" 
    android:contentDescription="@string/content" 
    android:src="@drawable/ic_launcher" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_label" /> 


</LinearLayout> 

mainactivity.java

package com.pokemon.ash; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity { 
Button button; 
ImageView image; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    addButtonListener(); 
} 

public void addButtonListener() { 

    image = (ImageView) findViewById(R.id.image); 

    button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View view) { 
       //i am using all the image files name 
       image.setImageResource(R.drawable.blastoise); 
       image.setImageResource(R.drawable.bulbasaur); 
       image.setImageResource(R.drawable.charizard); 
       image.setImageResource(R.drawable.charmander); 
       image.setImageResource(R.drawable.charmeleon); 
       image.setImageResource(R.drawable.ivysaur); 
       image.setImageResource(R.drawable.squirtle); 
       image.setImageResource(R.drawable.venusaur); 
       image.setImageResource(R.drawable.wartortle); 
            //but only last image is printing in the output I want all the images to be taken by drawable folder please help me in this such that i can swipe the images    
     } 
    }); 
}} 

enter image description here

這裏只有一個圖像被正確打印我希望所有的圖像可以看出,這樣當我刷到左側,必須轉到其他圖像像水箭龜圖像,並向左滑動它有要去bulbasaur圖像。

+0

您可以使用** ** ViewPager或** ViewFlipper ** 爲了那個原因。 – Piyush

+0

@Sandeep檢查[this](http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/)的例子。 – Kunu

+0

可以給你的代碼片段@PiyushGupta –

回答

0

現在你的Button覆蓋了你的1個ImageView的ImageResource 9次。這就是爲什麼你只看到最後的JPG。一個ImageView只能保存一個圖像。如果你想顯示9張圖片,你必須使用9個ImageViews,或者使用ViewPager(就像@Piyush Gupta已經建議的那樣)。此ViewPager的每一頁都將擁有自己的ImageView,其中包含9張圖片中的一張。我想你想用ViewPager去。 http://developer.android.com/training/animation/screen-slide.html

在這個環節你找到關於如何創建一個ViewPager與儘可能多的網頁,你 需要說明:關於這個在這裏更多信息

 
Create an activity that does the following things: 

- Sets the content view to be the layout with the ViewPager. 
- Creates a class that extends the FragmentStatePagerAdapter 
    abstract class and implements the getItem() method 
    to supply instances of ScreenSlidePageFragment as new pages. 
- The pager adapter also requires that you implement the getCount() method, 
    which returns the amount of pages the adapter will create (five in the example). 
- Hooks up the PagerAdapter to the ViewPager. 
- Handles the device's back button by moving backwards in the virtual stack of fragments. If the user is already on the first page, go back on the activity back stack. 
+0

感謝您的指導朋友如果我得到任何錯誤我將與您溝通 –

+0

當然,只是在這裏發佈。如果我的答案幫助你,請將其標記爲正確。 – muetzenflo

+0

老兄我創建了一個私有數組並將所有圖像傳遞給它,就像6-7圖像它工作正常我可以刷一切,但不是創建一個數組並靜態聲明圖像我們可以動態聲明,以便我可以找到n個數字來自可繪製文件夾的圖像@muetzenflo –