Android對話框圖片全屏

 android 自定義對話框使對話框鋪滿全屏。邊緣無縫對接,

效果圖如下:

調用方式:

 new AdFullDialog(MainActivity.this, “”, “”, “”, “”).show();

 對話框代碼:

 /**
     * 全屏廣告
     */
    public class AdFullDialog extends Dialog {
        private Context context;

        private String mUrl;

        Bitmap bmp;

        String mtarget_url;

        String Target_url;


        ImageView ivAdvertising;
        TextView mtv_second;
        LinearLayout layout_skip;

        public AdFullDialog(Context context, String url, String target_url, String Deep_link, List<String> deep_link_monitor_url) {
            // 更改樣式,把背景設置爲透明的
            super(context, R.style.dialog);
            this.context = context;
            mUrl = url;
            mtarget_url = target_url;
            Log.e("getTarget_url111", mtarget_url);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            calculateHeightAndWidth();
        }

        @Override
        public void show() {
            super.show();
            /**
             * 設置寬度全屏,要設置在show的後面
             */
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.gravity = Gravity.BOTTOM;
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;

            getWindow().getDecorView().setPadding(0, 0, 0, 0);

            getWindow().setAttributes(layoutParams);
        }

        /**
         * 按實際圖片比例對其的寬高進行縮放
         */
        private void calculateHeightAndWidth() {
            initAdView();

        }

        @SuppressLint("ResourceType")
        private void initAdView() {
            new Thread(getPicByUrl).start();//獲取圖片

            setContentView(R.layout.activity_ad);//加載自定義佈局

            ivAdvertising = findViewById(R.id.iv_advertising);
            layout_skip = findViewById(R.id.layout_skip_close);
            mtv_second = findViewById(R.id.tv_second);
            ivAdvertising.setOnTouchListener(new TouchListenerImp());
            ivAdvertising.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   
                }
            });
            layout_skip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                }
            });
        }


        Runnable getPicByUrl = new Runnable() {
            @Override
            public void run() {
                try {
                    bmp = GetImgUtil.getImage(mUrl);// BitmapFactory:圖片工廠!
                    Log.i("ggggg", bmp.toString());
                    sendMsg(1);
                } catch (Exception e) {
                    Log.i("ggggg", e.getMessage());
                }
            }
        };

        

    
        /**
         * 獲取點擊對話框座標
         */
        private class TouchListenerImp implements View.OnTouchListener {

            public boolean onTouch(View v, MotionEvent event) {

                ClickXY clickXY = new ClickXY();
                NumberFormat nf = new DecimalFormat("#.#");
                double a = 3.1;

                SLOT_X = nf.format(event.getX());
                SLOT_Y = nf.format(event.getY());
//            Log.e("LZ", "---SLOT_X=" + event.getRawX() + "----SLOT_X=" + event.getRawY())

                DOWN_X = nf.format(event.getRawX());
                DOWN_Y = nf.format(event.getRawY());
                clickXY.setSLOT_X(SLOT_X);
                clickXY.setSLOT_Y(SLOT_Y);
                clickXY.setDOWN_X(DOWN_X);
                clickXY.setDOWN_Y(DOWN_Y);
                Log.e("LZ", "---SLOT_X=" + event.getRawX() + "----SLOT_X=" + event.getRawY() + "DOWN_X=" + DOWN_X + "DOWN_Y=" + DOWN_Y);

                return false;
            }
        }
    }

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_advertising"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:visibility="gone"
        android:scaleType="fitXY"
      />

    <LinearLayout
        android:id="@+id/layout_skip_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="8dp"
        android:layout_marginTop="40dp"
        android:background="@drawable/splash_skip"
        android:orientation="horizontal"

        android:padding="4dp"
        android:visibility="visible">

        <TextView
            android:id="@+id/tv_second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingLeft="5dp"
            android:text=" "
            android:visibility="visible"
            android:textColor="@color/colorWhite"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:text=""
            android:textColor="@color/colorWhite"
            android:textSize="16sp" />
    </LinearLayout>
</FrameLayout>

                                                                                                                                                                    -END

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章