码迷,mamicode.com
首页 > 其他好文 > 详细

如果写一个列表点击一张图片来preview这张图片

时间:2014-08-01 23:08:12      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:alertdialog   preview图片   

可以才用继承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);
		}
	}

然后写这个Dialog:

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;
	}

}

这个Dialog的Layout:

<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>

效果图:

bubuko.com,布布扣

如果写一个列表点击一张图片来preview这张图片,布布扣,bubuko.com

如果写一个列表点击一张图片来preview这张图片

标签:alertdialog   preview图片   

原文地址:http://blog.csdn.net/baidu_nod/article/details/38342773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!