2

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

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

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

4

3 回答 3

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 活动。

这应该这样做。

于 2012-11-07T21:16:42.760 回答
2

由于您想在新活动中显示您的图像,我会做这样的事情:

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);
}
于 2012-11-07T19:43:30.860 回答
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
        }
    });  
于 2012-11-07T19:04:33.780 回答