Android 自定义显示图片Dialog
2019-05-22 11:39
232 查看
效果图:
- 布局文件
<?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="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" 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 DzShowImageDialog extends Dialog { private Object showImage; public DzShowImageDialog(Context context, Object showImage) { super(context, R.style.ShowImageDialog); this.showImage = showImage; } @SuppressLint("ResourceType") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_showimage); ImageView imageView = findViewById(R.id.imageView); MyCommon.setViewContent(imageView, showImage); setCanceledOnTouchOutside(true); // 设置点击屏幕或物理返回键,dialog是否消失 Window w = getWindow(); assert w != null; WindowManager.LayoutParams lp = w.getAttributes(); lp.x = 0; lp.y = 40; 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 DzInteger) drawable = ((DzInteger) drawable).value; 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(); 即可。
相关文章推荐
- Android自定义组件,实现图片和文字同时显示
- android 自定义Dialog背景透明及显示位置设置的方法
- Android Dialog设置宽度显示不正常的问题(自定义设置Dialog的宽高)
- Android可以动态控制图片显示区域的自定义ImageView
- Android中自定义Dialog对话框消除边距消除黑边框和显示动画
- Android 图片黑白显示 自定义饱和度
- Android TextView显示Html类解析的网页和图片及自定义标签用法示例
- android 缩放平移自定义View 显示图片
- android 自定义属性,分享一个自适应容器,多用于显示图片,保证图片不变形
- android 自定义Dialog背景透明及显示位置设置
- android 设置自定义dialog全屏显示
- Android 自定义对话框(Dialog)设置显示大小方向
- 自定义dialog包含一个显示图片的listview
- Android SeekBar自定义使用图片和颜色显示
- android 自定义Dialog背景透明及显示位置设置
- Android自定义Dialog带Dialog的显示消失动画(一)
- Android笔记:Dialog显示图片
- android 自定义ImageView显示圆形图片
- Android自定义ImageView实现图片圆形 ,椭圆和矩形圆角显示
- Android自定义Dialog显示进度框