在gridview android的onClick中显示更大的图像
我使用的是gridview,它显示SD卡上应用程序文件夹中的图像。在gridview中单击图像时如何显示全屏图像。在gridview中加载图像时,下面的代码工作正常。谢谢 更新:所以我创建了另一个活动FullImageActivity,并试图在其中显示单击的图像grom gridview。问题是它只显示图像路径,而不显示图像。我需要在FullImageActivity中显示图像。我如何做到这一点在gridview android的onClick中显示更大的图像,android,Android,我使用的是gridview,它显示SD卡上应用程序文件夹中的图像。在gridview中单击图像时如何显示全屏图像。在gridview中加载图像时,下面的代码工作正常。谢谢 更新:所以我创建了另一个活动FullImageActivity,并试图在其中显示单击的图像grom gridview。问题是它只显示图像路径,而不显示图像。我需要在FullImageActivity中显示图像。我如何做到这一点 public class ImageAdapter extends BaseAdapter{
public class ImageAdapter extends BaseAdapter{
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c){
mContext = c;
}
void add (String path){
itemList.add(path);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemList.get(position);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
}
ImageAdapter myImageAdapter;
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.library_layout);
gridview = (GridView) findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/AppFolder/";
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
gridview.setOnItemClickListener(myOnItemClickListener);
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String prompt = (String)parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_LONG).show();
Intent intent = new Intent(LibraryActivity.this, FullImageActivity.class);
intent.putExtra("Image", position);
LibraryActivity.this.startActivity(intent);
}
};
}
}在活动中使用并将图像传递给该活动
示例活动
public class FullScreenImageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_full_screen_image);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
TouchImageView imageView = (TouchImageView)findViewById(R.id.imgFullScreen);
imageView.setLayoutParams( new RelativeLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
imageView.setImageBitmap(bitmap);
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".FullScreenImage" >
<com.app.TouchImageView
android:id="@+id/imgFullScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
以这种方式检索它(在FullScreenImageActivity中)
我在上面的原始问题中添加了一些代码,但我仍然无法在另一个活动中显示gridview中的图像。请参阅我的编辑,您可以传递位图或图像url,但现在您正在传递gridview中视图的位置。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".FullScreenImage" >
<com.app.TouchImageView
android:id="@+id/imgFullScreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");