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

2024-03-28

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

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

我想在从屏幕上单击时以全尺寸显示特定图像。 有人能提出解决方案吗?


在 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.xml 中添加 FullImage 活动。

这应该可以做到。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从网格视图中单击时如何显示特定图像 的相关文章

随机推荐