AndroidBaseDialog(開發必備)動畫、載入進度、陰影

小編學it發表於2018-10-16

GitHub

APK

使用方法

將libray模組複製到專案中,或者直接在build.gradle中依賴:

allprojects {
        repositories {
            
            maven { url `https://jitpack.io` }
        }
    }
dependencies {
            compile `com.github.AnJiaoDe:BaseDialog:V1.1.2`
    }

注意:如果sync報錯,是因為和com.android.tools.build:gradle 3.0有關,
可以改將compile改為implementation 或者api

1.Center

這裡寫圖片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="@drawable/white_shape"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="確定刪除嗎?"
        android:textSize="16sp"
        android:gravity="center"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/line"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="48dp"

            android:text="取消"
            android:id="@+id/tv_cancel"
            android:gravity="center"
            android:textSize="16sp"/>
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/line"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:id="@+id/tv_confirm"
            android:layout_height="48dp"
            android:text="確定"
            android:gravity="center"

            android:textSize="16sp"/>
    </LinearLayout>
</LinearLayout>

2.Left

這裡寫圖片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:background="@color/white"

    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Google Assistant: 一句 OK, Google,多少手指都用不上了
人工智慧是今年的 Google I/O 的一大主題。在釋出會一開始,Google CEO 桑達拉·皮蔡(Sundar Pichai)就強調機器學習在生活中扮演的重要角色。隨後,一系列基於 Google 人工智慧的產品紛至沓來。



OK, Google. 這句耳熟能詳的命令,如今承載了 Google 全新的產品——Google Assistant.

之所以 Google Assistant 是釋出會上首個亮相的產品,是因為後續登場的數個產品都基於這一技術。Google 用將近十年的時間,改善自己的語音識別技術,更強調自然語義和對話式搜尋。"
        android:textSize="16sp" />

</LinearLayout>

3.Top

這裡寫圖片描述

<?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="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_photo"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:gravity="center"
            android:text="拍照"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.1dp"
            android:background="@color/line" />

        <TextView

            android:id="@+id/tv_album"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:gravity="center"
            android:text="從相簿選擇"

            android:textSize="16sp" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/bg"/>
    <TextView
        android:id="@+id/tv_photo_cancel"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center"
        android:text="取消"

        android:textSize="16sp" />
</LinearLayout>

4.Right

這裡寫圖片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:background="@color/white"

    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Google Assistant: 一句 OK, Google,多少手指都用不上了
人工智慧是今年的 Google I/O 的一大主題。在釋出會一開始,Google CEO 桑達拉·皮蔡(Sundar Pichai)就強調機器學習在生活中扮演的重要角色。隨後,一系列基於 Google 人工智慧的產品紛至沓來。



OK, Google. 這句耳熟能詳的命令,如今承載了 Google 全新的產品——Google Assistant.

之所以 Google Assistant 是釋出會上首個亮相的產品,是因為後續登場的數個產品都基於這一技術。Google 用將近十年的時間,改善自己的語音識別技術,更強調自然語義和對話式搜尋。"
        android:textSize="16sp" />

</LinearLayout>

5.Bottom

這裡寫圖片描述

<?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="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_photo"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:gravity="center"
            android:text="拍照"
            android:textSize="16sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.1dp"
            android:background="@color/line" />

        <TextView

            android:id="@+id/tv_album"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:gravity="center"
            android:text="從相簿選擇"

            android:textSize="16sp" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/bg"/>
    <TextView
        android:id="@+id/tv_photo_cancel"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center"
        android:text="取消"

        android:textSize="16sp" />
</LinearLayout>

6.Progress

這裡寫圖片描述


public class MainActivity extends BaseActivity {
    private BaseDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_center).setOnClickListener(this);
        findViewById(R.id.btn_left).setOnClickListener(this);
        findViewById(R.id.btn_top).setOnClickListener(this);
        findViewById(R.id.btn_right).setOnClickListener(this);
        findViewById(R.id.btn_bottom).setOnClickListener(this);
        findViewById(R.id.btn_progress).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_center:
                dialog = new BaseDialog(this);

                dialog.config(R.layout.dialog_center, true).show();
                dialog.findViewById(R.id.tv_confirm).setOnClickListener(this);
                dialog.findViewById(R.id.tv_cancel).setOnClickListener(this);

                break;
            case R.id.btn_left:
                BaseDialog dialog_left = new BaseDialog(this);

                dialog_left.config(R.layout.dialog_left, 0.5f, Gravity.LEFT | Gravity.CENTER, BaseDialog.AnimInType.LEFT,
                        WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.MATCH_PARENT, true).show();

                break;
            case R.id.btn_top:
                BaseDialog dialog_top = new BaseDialog(this);

                dialog_top.setOffset(0, ScreenUtils.dpInt2px(this, 48));

                dialog_top.config(R.layout.dialog_photo, 0.5f, Gravity.TOP, BaseDialog.AnimInType.TOP,
                        WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true).show();


                break;
            case R.id.btn_right:
                BaseDialog dialog_right = new BaseDialog(this);

                dialog_right.setOffset(20, 0);

                dialog_right.config(R.layout.dialog_right, Gravity.RIGHT | Gravity.CENTER, BaseDialog.AnimInType.RIGHT,
                        WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.MATCH_PARENT, true).show();
                break;
            case R.id.btn_bottom:
                BaseDialog dialog_bottom = new BaseDialog(this);

                dialog_bottom.config(R.layout.dialog_photo, Gravity.BOTTOM, BaseDialog.AnimInType.BOTTOM, true).show();


                break;
            case R.id.btn_progress:

                CYProgressDialog cyProgressDialog=new CYProgressDialog(this);
                cyProgressDialog.config(0.4f,0xffffffff,20,0xffffffff,0xff2a5caa).show();
                break;
            case R.id.tv_confirm:
                dialog.dismiss();
                break;
            case R.id.tv_cancel:
                dialog.dismiss();
                break;
        }

    }
}

原始碼:

BaseDialog


public class BaseDialog extends Dialog {

    public BaseDialog(Context context) {
        this(context, 0);

    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除對話方塊的標題
        getWindow().setBackgroundDrawableResource(R.drawable.dialog_bg);//設定對話方塊邊框背景,必須在程式碼中設定對話方塊背景,不然對話方塊背景是黑色的

        setDimAmount(0.2f);
    }

    /*
             */
    public BaseDialog config(@LayoutRes int layoutResID, boolean canceledOnTouchOutside) {


        config(layoutResID, 0.2f, Gravity.CENTER, AnimInType.CENTER, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, canceledOnTouchOutside);

        return this;
    }

    /*
             */
    public BaseDialog config(@NonNull View view, boolean canceledOnTouchOutside) {


        config(view, 0.2f, Gravity.CENTER, AnimInType.CENTER, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, canceledOnTouchOutside);

        return this;
    }

    /*
             */
    public BaseDialog config(@LayoutRes int layoutResID, int gravity, AnimInType animInType, boolean canceledOnTouchOutside) {

        config(layoutResID, 0.2f, gravity, animInType, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, canceledOnTouchOutside);


        return this;
    }

    /*
             */
    public BaseDialog config(@NonNull View view, int gravity, AnimInType animInType, boolean canceledOnTouchOutside) {

        config(view, 0.2f, gravity, animInType, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, canceledOnTouchOutside);


        return this;
    }

    /*
             */
    public BaseDialog config(@LayoutRes int layoutResID, int gravity, AnimInType animInType,
                             int width, int height, boolean canceledOnTouchOutside) {

        config(layoutResID, 0.2f, gravity, animInType, width, height, canceledOnTouchOutside);

        return this;
    }

    /*
             */
    public BaseDialog config(@NonNull View view, int gravity, AnimInType animInType,
                             int width, int height, boolean canceledOnTouchOutside) {

        config(view, 0.2f, gravity, animInType, width, height, canceledOnTouchOutside);

        return this;
    }

    /*
           引數:dimAmount背景陰影
            */
    public BaseDialog config(@LayoutRes int layoutResID, float dimAmount, int gravity, AnimInType animInType,
                             int width, int height, boolean canceledOnTouchOutside) {
        setContentView(layoutResID);//填充對話方塊佈局


        setDimAmount(dimAmount);

        getWindow().setGravity(gravity);

        setAnimType(animInType);


        getWindow().setLayout(width, height);

        setCanceledOnTouchOutside(canceledOnTouchOutside ? true : false);
        return this;
    }

    /*
           引數:dimAmount背景陰影
            */
    public BaseDialog config(@NonNull View view, float dimAmount, int gravity, AnimInType animInType,
                             int width, int height, boolean canceledOnTouchOutside) {
        setContentView(view);//填充對話方塊佈局


        setDimAmount(dimAmount);

        getWindow().setGravity(gravity);

        setAnimType(animInType);


        getWindow().setLayout(width, height);

        setCanceledOnTouchOutside(canceledOnTouchOutside ? true : false);
        return this;
    }

    /*
           引數:dimAmount背景陰影
            */
    public BaseDialog config(@NonNull View view, @Nullable ViewGroup.LayoutParams params, float dimAmount, int gravity, AnimInType animInType,
                             int width, int height, boolean canceledOnTouchOutside) {
        setContentView(view, params);//填充對話方塊佈局


        setDimAmount(dimAmount);

        getWindow().setGravity(gravity);

        setAnimType(animInType);


        getWindow().setLayout(width, height);

        setCanceledOnTouchOutside(canceledOnTouchOutside ? true : false);
        return this;
    }

    /*
    動畫型別
     */
    public BaseDialog setAnimType(AnimInType animInType) {

        if (animInType == AnimInType.CENTER) {
            getWindow().setWindowAnimations(R.style.dialog_zoom);
            return this;

        }
        if (animInType == AnimInType.LEFT) {
            getWindow().setWindowAnimations(R.style.dialog_anim_left);
            return this;

        }
        if (animInType == AnimInType.TOP) {
            getWindow().setWindowAnimations(R.style.dialog_anim_top);
            return this;

        }
        if (animInType == AnimInType.RIGHT) {
            getWindow().setWindowAnimations(R.style.dialog_anim_right);
            return this;

        }
        if (animInType == AnimInType.BOTTOM) {
            getWindow().setWindowAnimations(R.style.dialog_anim_bottom);
            return this;

        }
        return this;
    }

    public void setOffset(int x, int y) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.x = x;
        layoutParams.y = y;


    }

    public float getDimAmount() {
        return getWindow().getAttributes().dimAmount;
    }

    /*
    設定背景陰影,必須setContentView之後呼叫才生效
     */
    public BaseDialog setDimAmount(float dimAmount) {


        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.dimAmount = dimAmount;

        return this;
    }


    /*
    動畫型別
     */
    public enum AnimInType {
        CENTER(0),
        LEFT(2),
        TOP(4),
        RIGHT(3),
        BOTTOM(1);

        AnimInType(int n) {
            intType = n;
        }

        final int intType;
    }
}

CYProgressDialog


public class CYProgressDialog extends BaseDialog {

    private MaterialProgressDrawable mProgress;

    private ValueAnimator valueAnimator;

    public CYProgressDialog(Context context) {
        super(context);
        setCanceledOnTouchOutside(false);

    }

    /**
     * @param color_iv
     * @param radius_iv
     * @param color_bg_progress
     * @param colors_progress
     * @return
     */

    public CYProgressDialog config(int color_iv, float radius_iv, int color_bg_progress, int... colors_progress) {
        config(0.2f, color_iv, radius_iv, color_bg_progress, colors_progress);


        return this;
    }

    public CYProgressDialog config(float dimAmount, int color_iv, float radius_iv, int color_bg_progress, int... colors_progress) {


        FrameLayout frameLayout = new FrameLayout(getContext());


        CircleImageView imageView = new CircleImageView(getContext(), color_iv, radius_iv);


        mProgress = new MaterialProgressDrawable(getContext(), imageView);


        mProgress.setBackgroundColor(color_bg_progress);
        //圈圈顏色,可以是多種顏色
        mProgress.setColorSchemeColors(colors_progress);
        //設定圈圈的各種大小
        mProgress.updateSizes(MaterialProgressDrawable.DEFAULT);

        mProgress.showArrow(false);
        imageView.setImageDrawable(mProgress);

        frameLayout.addView(imageView);


        valueAnimator = valueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(600);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float n = (float) animation.getAnimatedValue();
                //圈圈的旋轉角度
                mProgress.setProgressRotation(n * 0.5f);
                //圈圈周長,0f-1F
                mProgress.setStartEndTrim(0f, n * 0.8f);
                //箭頭大小,0f-1F
                mProgress.setArrowScale(n);
                //透明度,0-255
                mProgress.setAlpha((int) (255 * n));
            }
        });

        getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setContentView(frameLayout);


        setDimAmount(dimAmount);

        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    hide();
                    return true;
                }
                return false;
            }
        });

        return this;
    }

    @Override
    public void show() {
        super.show();
        if (mProgress == null) return;

        mProgress.start();
        if (valueAnimator == null) return;

        valueAnimator.start();


    }

    @Override
    public void hide() {
        super.hide();
        if (mProgress == null) return;

        mProgress.stop();
        if (valueAnimator == null) return;

        valueAnimator.cancel();


    }

    public void setBackgroundColor(int color) {
        if (mProgress == null) return;

        mProgress.setBackgroundColor(color);

    }

    public void setColorSchemeColors(int... colors) {
        if (mProgress == null) return;

        mProgress.setColorSchemeColors(colors);

    }
}

GitHub

關注專題Android開發常用開源庫

簡書


相關文章