美文网首页
自定义控件之仿QQ提示Dialog

自定义控件之仿QQ提示Dialog

作者: XINHAO_HAN | 来源:发表于2017-09-30 16:35 被阅读0次

上次无意之间删除了一个好友(呃...删除好友...)就发现了QQ的删除提示框是由上往下提示,感觉挺霸气的(可能是因为删除好友的心情吧....0.0,唉不管了先上图)

2017-09-30-04mzdsaf.gif

我的思路是这样的
继承于Dialog,设置Dialog样式与动画在Handler中控制Dialog的销毁
代码:

package com.example.downlist.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.renderscript.RenderScript;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.downlist.R;
import com.example.downlist.utils.BlurBitmapUtil;
import com.example.downlist.utils.UIUtlis;

/**
 *
 */

/*
*
* this the dialog window,do up show and QQ message Window
*
* */
public class DialogThis extends Dialog {

    private DialogThis dialog;
    private Activity activity;
    private String title;
    private String message;
    private View.OnClickListener clickListener;
    private int wight;
    private int height;
    private View view;

    public DialogThis(@NonNull Context context) {
        super(context);
        init(context);
    }

    public DialogThis(@NonNull Context context, @StyleRes int themeResId) {
        super(context, R.style.MyDialog);
        init(context);
    }

    protected DialogThis(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }
    /*
    *
    * start init
    *
    * */

    private void init(Context context) {
        dialog = this;
        //load view

        view = UIUtlis.getView(R.layout.dialog_up);
        startViewMeasure();
        setContentView(view);

    }

    /*
    *
    *  start Measure view
    *
    * */

    private void startViewMeasure() {

        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int wight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        view.measure(wight, height);
        this.wight = view.getMeasuredWidth();
        this.height = view.getMeasuredHeight();


        this.getWindow().setGravity(Gravity.TOP | Gravity.LEFT);
        this.getWindow().setWindowAnimations(R.style.mystyle);
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }

    private void setViewMessage() {
        TextView title = view.findViewById(R.id.title);
        title.setText(this.title);
        TextView message = view.findViewById(R.id.message);
        message.setText(this.message);

       /* RelativeLayout viewById = view.findViewById(R.id.lz);
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        decorView.setDrawingCacheEnabled(true);
        decorView.buildDrawingCache();
        Bitmap drawingCache = decorView.getDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(drawingCache, 0, 0, UIUtlis.getDriveX(activity), height + 100);

        Bitmap bitmap1 = BlurBitmapUtil.blurBitmap(activity, bitmap, 20f);
        Drawable drawable = new BitmapDrawable(bitmap1);*/

        // viewById.setBackground(drawable);

        //dialog.getWindow().setBackgroundDrawable(drawable);
    }

    public static class Build {
        private Activity activity;
        private String title;
        private String message;
        private View.OnClickListener clickListener;

        public Build(Activity activity) {
            this.activity = activity;

        }

        public Activity getActivity() {
            return activity;
        }

        public Build setActivity(Activity activity) {
            this.activity = activity;
            return this;
        }

        public String getTitle() {
            return title;
        }

        public Build setTitle(String title) {
            this.title = title;
            return this;
        }

        public String getMessage() {
            return message;
        }

        public Build setMessage(String message) {
            this.message = message;
            return this;
        }

        public View.OnClickListener getClickListener() {
            return clickListener;
        }

        public Build setClickListener(View.OnClickListener clickListener) {
            this.clickListener = clickListener;
            return this;
        }

        public DialogThis build() {
            DialogThis dialogThis = new DialogThis(activity, -1);
            dialogThis.activity = activity;
            dialogThis.clickListener = clickListener;
            dialogThis.message = message;
            dialogThis.title = title;
            dialogThis.setViewMessage();

            return dialogThis;
        }
    }

    public static void ToastDialog(Activity activity, String title, String message) {

        Build build = new Build(activity);
        DialogThis build1 = build
                .setTitle(title)
                .setMessage(message)
                .build();

        build1.show();
        Message message1 = new Message();
        message1.obj = build1;


        han.sendMessageAtTime(message1, 2000);

        han = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                DialogThis build1 = (DialogThis) msg.obj;
                build1.dismiss();

            }
        };
    }


    public static void ToastDialog(Activity activity, String message) {

        Build build = new Build(activity);
        final DialogThis build1 = build
                .setMessage(message)
                .setTitle("提示")
                .build();

        build1.show();
        Message message1 = new Message();
        message1.obj = build1;


        han = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                build1.dismiss();

            }
        };
        han.sendEmptyMessageDelayed(0, 2000);
    }

    static Handler han;

    @Override
    public void show() {
        super.show();
        this.getWindow().setLayout(getDisplayWight(), height + 100);

    }


    /*
    *
    *  get display wight
    *
    * */

    public int getDisplayWight() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }


}


布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/lz"
    android:background="#ccddaa"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:textColor="#000"
        android:textSize="18sp" />


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="这是文字显示区"
        android:textColor="#000"
        android:textSize="16sp" />

</RelativeLayout>

主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <style name="MyDialog" parent="Base.V11.Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@color/back</item> <!-- 背景透明 -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowCloseOnTouchOutside">false</item>
        <item name="android:backgroundDimEnabled">false</item> <!-- 背景模糊 -->
    </style>


    <style name="mystyle" parent="android:Animation">
        <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
        //进入时的动画
        <item name="@android:windowExitAnimation">@anim/dialog_exit</item>
        //退出时的动画

    </style>
</resources>

使用:

    DialogThis.ToastDialog(activity, title,message);
    DialogThis.ToastDialog(activity, message);

Demo下载:
链接: https://pan.baidu.com/s/1i5CayhB 密码: t8wg

相关文章

网友评论

      本文标题:自定义控件之仿QQ提示Dialog

      本文链接:https://www.haomeiwen.com/subject/jnfnextx.html