1. 程式人生 > >android 點選縮圖檢視大圖

android 點選縮圖檢視大圖

android中點選縮圖檢視大圖的方法一般有兩種,一種是想新浪微博list頁面那樣,彈出一個視窗顯示大圖(原activity為背景)。另一種就是直接開啟一個新的activity顯示大圖。

1、第一種方法我們可以使用自定義的Dialog來實現:

/**
	 * 點選縮圖開啟原圖
	 * */
	private void openBigImage() {
		
		if(openBigImageDialog != null && !openBigImageDialog.isShowing()){
			
			initBitmapUtils();    
			bitmapUtils.display(iv_big_image, person.getSmallPhotoUrl());
			
		}else{
			openBigImageDialog = new Dialog(this,R.style.dialog_style);
			View view = View.inflate(this, R.layout.show_big_image_dialog, null);
			openBigImageDialog.setContentView(view);
			openBigImageDialog.setFeatureDrawableAlpha(Window.FEATURE_OPTIONS_PANEL, 0);
			//點選其他區域dialog消失
			openBigImageDialog.setCanceledOnTouchOutside(true);
			openBigImageDialog.setCancelable(true);
			openBigImageDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			openBigImageDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			WindowManager.LayoutParams params = openBigImageDialog.getWindow().getAttributes();
			int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
			params.width = screenWidth;		
			openBigImageDialog.getWindow().setAttributes(params);
			
			rl_big_image = (RelativeLayout) view.findViewById(R.id.rl_big_image);
			iv_big_image = (ImageView) view.findViewById(R.id.iv_big_image);
			initBitmapUtils();    
			bitmapUtils.display(iv_big_image, person.getSmallPhotoUrl());
			rl_big_image.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(openBigImageDialog != null && openBigImageDialog.isShowing()){
						openBigImageDialog.dismiss();
					}
				}
			});
		}
		
		openBigImageDialog.show();
	}

	private void initBitmapUtils(){
		if (bitmapUtils == null) {
			bitmapUtils = new BitmapUtils(this);
			bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
			bitmapUtils.configDefaultLoadFailedImage(R.drawable.ic_launcher);
			bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
			bitmapUtils.configMemoryCacheEnabled(true);
			bitmapUtils.configDiskCacheEnabled(true);
			bitmapUtils.configDefaultConnectTimeout(2000);
			bitmapUtils.configDefaultBitmapMaxSize(BitmapCommonUtils.getScreenSize(this));
		}
	}


show_big_image_dialog佈局xml檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_big_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_big_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:scaleType="fitCenter"
        android:layout_centerInParent="true" />

</RelativeLayout>