gpt4 book ai didi

android - 从 GridView 中单击时如何显示特定图像

转载 作者:太空狗 更新时间:2023-10-29 16:06:27 25 4
gpt4 key购买 nike

我在我的应用程序中使用 GridView 显示一些图像

<?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 中,您应该添加您想要开始一个新 Activity 并通过 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 类的 Intent 中所见,您应该创建该类:

    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 布局的样子,请注意它有一个 ImageView,id 为 full_image_view,在 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 Activity 。

应该这样做。

关于android - 从 GridView 中单击时如何显示特定图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13276207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com