从网格视图中单击时如何显示特定图像

时间:2012-11-07 18:51:39

标签: android

我正在使用网格视图在我的应用程序中显示一些图像

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

这是处理图像的以下代码

public class Gallery extends Activity {
    Integer[] imageIDs = {
            R.drawable.a,
            R.drawable.b,
            R.drawable.c,
            R.drawable.d,
            R.drawable.e,
            R.drawable.f,
            R.drawable.g,
            R.drawable.k,
            R.drawable.j,
            R.drawable.i,
            R.drawable.h,

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

        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));

        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, 
            View v, int position, long id) 
            {                
                Toast.makeText(getBaseContext(), 
                        "pic" + (position + 1) + " selected", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

    public class ImageAdapter extends BaseAdapter 
    {
        private Context context;

        public ImageAdapter(Context c) 
        {
            context = c;
        }

        //---returns the number of images---
        public int getCount() {
            return imageIDs.length;
        }

        //---returns the ID of an item--- 
        public Object getItem(int position) {
            return position;
        }

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

        //---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(5, 5, 5, 5);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(imageIDs[position]);
            return imageView;
        }
    }    
}

我想在屏幕上点击时以完整尺寸显示特定图像。 有人可以提出解决方案吗?

3 个答案:

答案 0 :(得分:4)

在你的OnItemClickListener中你应该添加你想要开始一个新活动并通过putExtra将它传递给图像的位置:

    gridView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();

            Intent intent = new Intent(Gallery.this,FullImage.class);
            intent.putExtra("imgPos",position);
            Gallery.this.startActivity(intent);
        }
    });

正如您在意图中所看到的,您将调用FullImage类,您应该创建它:

    public class FullImage extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);
    int position = getIntent().getExtras().getInt("imgPos");
    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(Gallery.imageIDs[position]);

}   

}

这就是full_image布局的样子,请注意它有一个id为full_image_view的ImageView,它在FullImage类中被尊重

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > 
<ImageView android:id="@+id/full_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/> 

您应该将ImageID设置为静态Integer,以便可以在FullImage类中引用它。

最后,不要忘记在AndroidManifest中添加FullImage活动。

这应该这样做。

答案 1 :(得分:2)

由于你想在一个新的Activity中显示你的Image,我会做这样的事情:

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {                
    Intent intent = new Intent(Gallery.this, DisplayActivity.class);
    intent.putExtra("Image Int", imageIDs[position]);
    startActivity(intent);
}

在显示活动使用中:

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

    int imageInt = getIntent().getIntExtra("Image Int", R.drawable.oops); // oops should be a fallback if an error happens
    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    imageView.setImageResource(imageInt);
}

答案 2 :(得分:1)

基于位置从imageIds获取图像并显示它 - 启动新活动并传递图像ID或您希望如何。

gridView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                
            int imageId = imageIDs[position];
            //now you have clicked imageId so display it where you want
        }
    });