我在我的Android应用程序中有网格视图,我希望当我点击网格视图中的一个图像时,它会在全视图中显示该图像。
目前它会在点击图像时显示图像的位置。
这是代码
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(ViewActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
这是ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load(mThumbIds[position]).into(imageView);
//imageView.setImageResource(Integer.parseInt(mThumbIds[position]));
return imageView;
}
public String[] mThumbIds = new String[]{
"http://192.168.1.4/comic_app/index.php?id=1",
"http://192.168.1.4/comic_app/index.php?id=67",
"http://192.168.1.4/comic_app/index.php?id=4",
"http://192.168.1.4/comic_app/index.php?id=89",
"http://192.168.1.4/comic_app/index.php?id=98",
"http://192.168.1.4/comic_app/index.php?id=23",
"http://192.168.1.4/comic_app/index.php?id=45",
};
}
如何在点击该图片时显示图片。
提前致谢。
答案 0 :(得分:0)
您可以通过以下方式将BitMap
传递给其他活动。
Intent intent = new Intent(getApplicationContext(),CameraPreviewActivity.class);
Bundle extras = new Bundle();
gridItem = gridItems.get(position);
bitmap = gridItem.getImage();
image_title = gridItem.getImageTitle();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
extras.putString(IMAGE_TITLE,image_title);
extras.putByteArray(IMAGE, bos.toByteArray());
intent.putExtras(extras);
startActivity(intent);
此代码将进入您的gridview.setOnItemClickListener
。
在下一个活动中,使用以下代码获取图像。
byte[] byteArray = getIntent().getByteArrayExtra(IMAGE);
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
希望这有帮助!
答案 1 :(得分:0)
您可以将点击的项目的位置传递到下一个屏幕
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
然后在FullImageActivity中,您可以获取图像位置并将其显示为
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
如需更多参考,请访问以下链接:
http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/
答案 2 :(得分:0)
我已经解决了我的问题,我只是替换了这一行
imageView.setImageResource(imageAdapter.mThumbIds[position]);
用这个
Picasso.with(imageAdapter.mContext).load(imageAdapter.mThumbIds[position]).into(imageView);
现在工作正常......