2011-08-11 106 views
1

任何人都可以告訴我如何在Android中點擊Gallery中的特定圖像時在圖庫視圖底部放大圖像?顯示在圖庫視圖底部的放大圖像,當圖庫中的特定圖像被點擊時android

我試過如下做到這一點,但問題是,我的形象在Gallery越來越顯示相同圖像的背後:

我的代碼是:

package com.HelloGallery; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.Toast; 


public class HelloGallery extends Activity 
    { 
    public static Integer[] mImageIds = { 
    R.drawable.ashtavinayak, 
    R.drawable.ayadhya, 
    R.drawable.buddh, 
    R.drawable.centersoffaith, 
    R.drawable.chandra, 
    R.drawable.gajananmaharaj, 
    R.drawable.guru, 
    R.drawable.haridwar, 
    R.drawable.kanchipuram, 
    R.drawable.kanyakumari, 
    R.drawable.ketu, 
    R.drawable.maiher, 
    R.drawable.mangal, 
    R.drawable.mathura, 
    R.drawable.meenakshi, 
    R.drawable.pandharpur, 
    R.drawable.rahu, 
    R.drawable.shabrimalai, 
    R.drawable.shani, 
    R.drawable.shirdi, 
    R.drawable.shukra, 
    R.drawable.surya, 
    R.drawable.ujjain, 
    R.drawable.vaishnodevi, 
    R.drawable.vindhyavasini, 
}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Gallery gallery = (Gallery) findViewById(R.id.gallery); 
    gallery.setAdapter(new ImageAdapter(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() 
    { 

     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
      showImage(v, position); 

      //imageView.setImageResource(mImageIds[0]); 
     } 

    }); 


} 


protected void showImage(View v, int position) 
{ 

    ImageView imgView = (ImageView)v; 
    imgView.setImageResource(mImageIds[position]); 
    imgView.bringToFront(); 
    imgView.setLayoutParams(new Gallery.LayoutParams(150,100)); 
    imgView.setScaleType(ImageView.ScaleType.FIT_CENTER);   
} 


class ImageAdapter extends BaseAdapter 
{ 
    int mGalleryItemBackground; 
    private Context mContext; 

    public ImageAdapter(Context c) 
    { 
     mContext = c; 
     TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); 
     mGalleryItemBackground = attr.getResourceId(
       R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     attr.recycle(); 
    } 

    public int getCount() 
    { 
     return mImageIds.length; 
    } 

    public Object getItem(int position) 
    { 
     return position; 
    } 

    public long getItemId(int position) 
    { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imageView = new ImageView(mContext); 

     imageView.setImageResource(mImageIds[position]); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150,100)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     return imageView; 
    } 
} 
    } 
+0

首先在您的主要線性/相對佈局中添加該imageview,然後在showImage函數中使用bringToFront。 – MKJParekh

回答

0

更改您的showImage方法以下

protected void showImage(View v, int position) 
{ 
    for(int i = 0; i < gallery.getChildCount(); i++){ 
     if(gallery.getChildAt(i) != null) { 
      gallery.getChildAt(i).setLayoutParams(new Gallery.LayoutParams(150, 100)); 
     } 
    } 
    v.setLayoutParams(new Gallery.LayoutParams(300, 200)); 
} 

首先你調整你的所有可見imageViews到原來的大小,那麼你調整選擇ImageView的。您還必須將畫廊變量變爲成員變量,從而將其移至類定義。

+0

@brig我有獎金嗎? – Herrbert74

相關問題