开发者

to display magnify image in bottom of gallery view when that particular image in gallery is clicked in android

Can anyone tell me how I can magnify an image in the bottom of a gallery view when that particular image in a Gallery is clicked in android?

I tried to do it as follows, but the problem is that my image is getting displayed behind the same image in the Gallery:

My code is:

 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;
    }
}
  }


Change your showImage method to the following

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));
}

First you resize all your visible imageViews to their original size, then you resize the chosen imageView. You will also have to make your gallery variable to a member variable, thus moving it to the class definition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜