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>