在项目中需要对缩略图进行大图查看,就类似微信的点击查看大图,只不过这里作只采用了一张图片,没有左右滑动的效果。
这里的图片是从储存卡中获取,首先得需要该图片的本地地址,然后将本地图片转换成Bitmap,设置获取比例为1.
关键代码如下:
public class ShowBigPhoto {
private Context context; private String path;public ShowBigPhoto(Context context, String path){ this.context = context; this.path = path; } public void showDetailPhoto(){ // 全屏显示的方法 final Dialog dialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen); ImageView imgView = getView(); dialog.setContentView(imgView); dialog.show(); // 大图显示之后,点击图片消失 imgView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); } //设置当前imgView的图片 private ImageView getView() { ImageView imgView = new ImageView(context); imgView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); imgView.setImageBitmap(decodeBigPhoto()); return imgView; } private Bitmap decodeBigPhoto(){ if (path != null) { Bitmap bigBitmap; BitmapFactory.Options opt = new BitmapFactory.Options(); bigBitmap = BitmapFactory.decodeFile(path); opt.inSampleSize = 1; opt.inJustDecodeBounds = false;
bigBitmap = BitmapFactory.decodeFile(path, opt);
//iv_photo.setImageBitmap(bigBitmap); return bigBitmap; // releaseBitmap(); } return null;}
在需要进行显示的activity中,new一个ShowBigPhoto,并调用方法showDetailPhoto()即可。