可以才用继承AlertDialog的形式来写:
点击张图:
public void onClick(View v) {
viewBigsizeBmp(((ImageView) v).getDrawable(), Constants.PREVIEW_BMP_DIR_SUFFIX);
}
private void viewBigsizeBmp(Drawable drawable, String suffix) {
final String sdDir = Utils.getSDPath();
final String saveDir = TextUtils.isEmpty(sdDir) ? mCacheDir : sdDir
+ suffix;
if (mBlog != null && mBlog.pic != null) {
ImageViewerDialog dialog = new ImageViewerDialog(context,
mBlog.pic, saveDir, drawable, suffix); //mBlog.pic为图片请求的地址,如http://ww3.sinaimg.cn/wap120/67079b84jw1eiws9eajgxj20c80cbjt0.jpg
//drawable为这张图片的缩略图资源
dialog.setCanceledOnTouchOutside(true);
dialog.show();
dialog.setOnCancelListener(new AlertDialog.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = context.getResources().getDimensionPixelSize(
R.dimen.dialog_width);
lp.height = context.getResources()
.getDimensionPixelSize(R.dimen.dialog_height);
dialog.getWindow().setAttributes(lp);
}
}
public class ImageViewerDialog extends AlertDialog {
private Context context;
private LayoutInflater inflater;
private LinearLayout layout;
private ImageView iv;
private ProgressBar pb;
private String uri;
private AsyncTask<Void, Void, Uri> loadPicTask;
// 图片加载
String picfilePath;
public boolean loadPictureRunning;
private boolean finishPicLoad;
private boolean isRedirectBlog;
// private File downloadedImage;
private LoadPictureTask mldPicTsk;
private String mSaveDir;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(layout);
try {
new LoadPictureTask().execute();
} catch (RejectedExecutionException e) {
}
}
private class LoadPictureTask extends AsyncTask<Object, Void, Object[]> {
protected Object[] doInBackground(Object... args) {
Bitmap bmp = null;
try {
bmp = Utils.getPreviewBitmap(uri, context.getCacheDir()
.getAbsolutePath(), context, null, false,
Constants.PREVIEW_BMP_DIR_SUFFIX);
} catch (Exception e) {
// Utils.loge(e);
return null;
}
return new Object[] { bmp };
}
protected void onPostExecute(Object[] objs) {
loadPictureRunning = false;
if (objs != null && objs[0] != null) {
pb.setVisibility(View.GONE);
iv.setImageBitmap((Bitmap) objs[0]);
finishPicLoad = true;
}
}
protected void onPreExecute() {
super.onPreExecute();
}
}
public ImageViewerDialog(Context context, String uri, String dir,
Drawable drawable, String suffix) {
super(context);
this.context = context;
this.mSaveDir = dir;
inflater = LayoutInflater.from(context);
layout = (LinearLayout) inflater.inflate(R.layout.imageviewerdialog,
null);
iv = (ImageView) layout.findViewById(R.id.ivImage);
iv.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
dismiss();
}
});
pb = (ProgressBar) layout.findViewById(R.id.ProgressBar01);
pb.setBackgroundDrawable(drawable);
this.uri = uri;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:background="@drawable/pic_bg"> <ProgressBar android:id="@+id/ProgressBar01" android:layout_width="@integer/preview_pic_size" android:layout_height="@integer/preview_pic_size" ></ProgressBar> <ImageView android:id="@+id/ivImage" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
如果写一个列表点击一张图片来preview这张图片,布布扣,bubuko.com
原文地址:http://blog.csdn.net/baidu_nod/article/details/38342773