2011-10-03 86 views
1

我使用此tutorial爲Android創建了一個帶GridView的應用程序。我將所有圖像 放入drawable-hdpi文件夾中。 gridview工作得很好,但現在我想在用戶觸摸gridview中的圖像時啓動或打開另一個活動。新的活動應該是該單幅圖像的預覽。如何在Android中使用GridView Activity顯示圖像預覽

我該怎麼做?

這我怎麼推杆,但它不工作 我1activity:

gridView.setOnItemClickListener(new OnItemClickListener() 
{ 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    {     
     Intent fullScreenIntent = new Intent(v.getContext(), 2activity.class); 
     fullScreenIntent.putExtra(1activity.class.getName(),imageIDs); 

     1activity.this.startActivity(fullScreenIntent); 
    } 
}); 

我2activity:

public void onCreate(Bundle savedInstanceState, int[] imageIDs) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.image); 
     Intent intent = getIntent(); 

     long imageIDs = (Long) intent.getExtras().get(2activity.class.getName()); 
     ImageView imageView = (ImageView)v.findViewById(R.id.preview); 

     imageView.setLayoutParams(new ViewGroup.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT)); 

     imageView.setImageResource((int) imageIDs); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY);  

    } 

,當我去2activity從1activity它只是深藏不露只是空白的不捕圖片從第一次活動?我該怎麼辦 ???

回答

0

創建啓動您的活動的OnItemClickListener(http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html),並使用setOnItemClickListener方法(http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener% 29)將偵聽器附加到您的GridView。

-Kurtis

+0

由於裴家我想,鏈接,但它不是工作,所以能PLZ告訴我詳細介紹瞭如何從1activity gridview的圖像調用2activity,如果你不介意b'coz我在安卓 – Abhijeet

+0

嘗試新手檢查了這一點:http://developer.android.com/guide/topics/ui/binding.html。向下滾動到底部,看看他們如何設置onItemClickListener。你會想做同樣的事情,除了創建一個敬酒之外,你想用你想要啓動的活動創建一個Intent並調用startActivity()。 –

+0

我仍然沒有得到它我試圖和它的工作,但是當我去2activity但它只是沒有顯示任何東西只是空白其第一次活動沒有捕捉圖像?????我該怎麼辦 ??? – Abhijeet

0

在你2Activity你得到空值

long imageIDs = (Long) intent.getExtras().get(2activity.class.getName()); 

你應該把這裏:

int imageIDs1 = intent.getIntExtra(2activity.class.getName(), R.drawable.icon); 
1

錯誤的位置:

long imageIDs = (Long) intent.getExtras().get(2activity.class.getName()); 

錯鍵,修復它:

long imageIDs = (Long) intent.getExtras().get(1activity.class.getName()); 
0

enter image description here

FragmentManager fm = activity.getFragmentManager(); 
ImageDialogFragment newFragment = ImageDialogFragment.newInstance();                                                          
newFragment .setArguments(bundle); 
newFragment .show(fm, "slideshow"); 

的更多信息請點擊here

0

在圖片點擊獲取圖像位圖,轉換位爲字節數,並通過經意圖字節到你的活動

   Bitmap b=image.getDrawingCache(); 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       b.compress(Bitmap.CompressFormat.PNG,75, stream); 
       byte[] bytes = stream.toByteArray(); 
       Intent i = new Intent(mContext, ShowImageActivity.class); 
       i.putExtra("Bitmap", bytes); 
       startActivity(i); 

在第二個活動中從包中獲取字節並設置爲imagevie w^

Bundle bundle = getIntent().getExtras(); 
    if (bundle != null) { 
     if(bundle.containsKey("Bitmap")) { 
      byte[] bytes = bundle.getByteArray("Bitmap"); 
      Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
      BitmapDrawable ob = new BitmapDrawable(getResources(), bmp); 
      photo.setBackground(ob); 
     } 

    } 
相關問題