Android 自定义显示图片Dialog

效果图:
在这里插入图片描述

  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        tools:ignore="ContentDescription" />

</RelativeLayout>
  • 新建style样式
<style name="ShowImageDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>
  • 新建ShowImageDialog继承Dialog:
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class ShowImageDialog extends Dialog {

    private ImageView imageView;
    private Object showImage;
    private boolean canCancle;

    public ShowImageDialog(Context context, Object showImage) {
        this(context, showImage, true);
    }

    public ShowImageDialog(Context context, Object showImage, boolean canCancle) {
        super(context, R.style.ShowImageDialog);
        this.showImage = showImage;
        this.canCancle = canCancle;
    }

    public void setShowImage(Object showImage) {
        this.showImage = showImage;
        handler.sendEmptyMessage(0);
    }

    @SuppressLint("HandlerLeak")
    private
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
	            MyCommon.setViewContent(imageView, showImage);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_showimage);

        imageView = findViewById(R.id.imageView);
        handler.sendEmptyMessage(0);
        // 设置点击屏幕或物理返回键,dialog是否消失
        setCanceledOnTouchOutside(canCancle);
        //设置点击返回键,dialog是否消失
        setCancelable(canCancle);
        Window w = getWindow();
        WindowManager.LayoutParams lp = null;
        if (w != null) {
            lp = w.getAttributes();
            lp.x = 0;
            lp.y = 40;
            w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        onWindowAttributesChanged(lp);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }
}
  • 附MyCommon方法
	/**
	 * 设置 ImageView 的显示内容。
	 * 
	 * @param content
	 *            支持 null,Integer(ResID),Bitmap,Drawable。
	 */
	public static boolean setViewContent(ImageView view, Object content) {
		if (view == null)
			return false;

		Object tagValue = getTagValue(content);
		if (view.getTag(R.string.setTagKey_content).equse(tagValue))
			return view.getDrawable() != null;
		view.setTag(R.string.setTagKey_content, tagValue);

		Drawable real = getDrawable(view, content);
		view.setImageDrawable(real);
		return (real != null);
	}
	
	/**
	 * 为指定 View 准备其需要使用的 Drawable 对象。
	 */
	@SuppressWarnings("deprecation")
	public static Drawable getDrawable(View view, Object drawable) {
		if (view == null)
			return null;

		if (drawable == null)
			return null;

		Drawable real = null;
		try {
			if (drawable instanceof Integer) {
				int resId = (Integer) drawable;
				if (resId == 0)
					return null;
				real = view.getResources().getDrawable(resId);
			} else if (drawable instanceof Drawable) {
				real = (Drawable) drawable;
			} else if (drawable instanceof Bitmap) {
				real = new BitmapDrawable(view.getResources(), (Bitmap) drawable);
			}
		} catch (Throwable e) {
			Log.e("", "MyCommon.getViewDrawable(.., ..) failed for %s", e.toString());
		}

		// 需要设置一下,否则在 TextView 中显示不出来
		if (real != null) {
			real.setBounds(0, 0, real.getMinimumWidth(), real.getMinimumHeight());
		}

		return real;
	}
  • 使用示例

使用的话只需要调用 new ShowImageDialog(context , 图片).show(); 即可。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值