问题描述
- 当从grid view中点击时如何显示指定图像?
-
我使用一个grid view在程序中显示一些图像:<?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; } } }
当从屏幕上点击时,我想以实际大小来显示指定的图像。如何实现?
解决方案
我之前在新的 activity 中显示图像,是这么实现的:
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);
}
在 activity 中使用:
@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);
}
解决方案二:
点击可以弹出一个Dialog或者Dialog风格的Activity,至于要想显示实际大小,那么你首先得得到image的长和宽。
int width = bitmap.getWidth();
int height = bitmap.getHeight();
然后在你要显示的View去设置imageView.setLayoutParams(new ViewGroup.LayoutParams(width, height));
解决方案三:
你是就想在gridview中放大么,那样效果会不太好如果实现
不如用dialog去显示
时间: 2024-10-24 16:54:20