dialog 显示图片

    技术2022-07-12  72

    private PopupWindow popupDialog; /** * 弹出图片放大框 * * @param url 图片路径 */ public void showDialog(Context mContext, String url) { if (isFinishing()) { return; } try { //容错 if (popupDialog != null) { hideDialog(); } View popView = getLayoutInflater().inflate(R.layout.dialog_image, null);   //glide 加载图片 ImageView iv_dialog_image = (ImageView) popView.findViewById(R.id.iv_dialog_image); RequestOptions requestOptions = new RequestOptions(); requestOptions.placeholder(R.mipmap.image_load); Glide.with(mContext) .asBitmap() .load(url) .apply(requestOptions) .into(iv_dialog_image); popupDialog = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupDialog.setBackgroundDrawable(getDrawable(R.drawable.dialog_background)); popupDialog.setOutsideTouchable(true);//外部点击消失 //popView.setFocusableInTouchMode(true); popupDialog.setFocusable(true); // 这个很重要 if (popupDialog != null && !popupDialog.isShowing()) { popupDialog.setAnimationStyle(R.style.dialog_anim); popupDialog.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0); popupDialog.setFocusable(true); } } catch (Exception e) { //showAtLocation } }

     

    dialog_image.xml:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#99232323" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/iv_dialog_image" android:layout_width="240dp" android:layout_height="240dp" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@color/color_f6"/> </LinearLayout>

    dialog_background.xml:

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="15dp"/> <solid android:color="@color/white"/> </shape> color_f6 #f6f6f6 放到value/styles 下 <!-- popwidnow 显示消失动画 --> <style name="dialog_anim"> <item name="android:windowEnterAnimation">@anim/dialog_show</item> <item name="android:windowExitAnimation">@anim/dialog_hide</item> </style>

    以下放到anim文件下

    dialog_hide.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 透明度渐变 --> <alpha android:duration="250" android:fromAlpha="1.0" android:toAlpha="0.0"/> </set>

     

     

    dialog_show.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="250" android:fillAfter="false" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0"> </scale> </set>

     

    //glide 图片加载 implementation 'com.github.bumptech.glide:glide:3.7.0'

     

    Processed: 0.009, SQL: 9