Android BaseDialog(开发必备)动画、加载进度、阴影

简介: (开发必备)动画、加载进度、阴影

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开发常用开源库

简书

目录
相关文章
|
6天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
1天前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
18 10
|
3天前
|
Java Linux Android开发
深入理解Android开发:从基础到高级
【9月更文挑战第17天】本文将深入探讨Android开发的各个方面,包括应用开发、操作系统等。我们将通过代码示例来展示如何创建一个简单的Android应用,并解释其背后的原理。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和启示。
|
1天前
|
Java Android开发 C++
🚀Android NDK开发实战!Java与C++混合编程,打造极致性能体验!📊
在Android应用开发中,追求卓越性能是不变的主题。本文介绍如何利用Android NDK(Native Development Kit)结合Java与C++进行混合编程,提升应用性能。从环境搭建到JNI接口设计,再到实战示例,全面展示NDK的优势与应用技巧,助你打造高性能应用。通过具体案例,如计算斐波那契数列,详细讲解Java与C++的协作流程,帮助开发者掌握NDK开发精髓,实现高效计算与硬件交互。
7 1
|
6天前
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
14 3
|
8天前
|
API Android开发 iOS开发
安卓与iOS开发中的线程管理对比
【9月更文挑战第12天】在移动应用的世界中,安卓和iOS平台各自拥有庞大的用户群体。开发者们在这两个平台上构建应用时,线程管理是他们必须面对的关键挑战之一。本文将深入探讨两大平台在线程管理方面的异同,通过直观的代码示例,揭示它们各自的设计理念和实现方式,帮助读者更好地理解如何在安卓与iOS开发中高效地处理多线程任务。
|
9天前
|
搜索推荐 Android开发 UED
安卓开发中的自定义视图:打造个性化用户界面
【9月更文挑战第11天】在安卓应用开发领域,自定义视图是实现独特用户体验的基石。本文将引导你通过一个简单的自定义视图示例,探索如何从零开始创建并应用自定义组件,以增强你的应用界面。我们将一起学习如何扩展View类,重写onDraw方法,处理触摸事件,并最终在我们的安卓项目中使用这个自定义视图。无论你是初学者还是有一定经验的开发者,这篇文章都将为你提供清晰的步骤和实用的技巧,帮助你提升用户界面设计的能力。
|
10天前
|
IDE 开发工具 Android开发
安卓与iOS开发对比:平台选择对项目成功的影响
【9月更文挑战第10天】在移动应用开发的世界中,选择正确的平台是至关重要的。本文将深入探讨安卓和iOS这两大主要移动操作系统的开发环境,通过比较它们的市场份额、开发工具、编程语言和用户群体等方面,为开发者提供一个清晰的指南。我们将分析这两个平台的优势和劣势,并讨论如何根据项目需求和目标受众来做出最佳选择。无论你是初学者还是有经验的开发者,这篇文章都将帮助你更好地理解每个平台的特性,并指导你做出明智的决策。
|
10天前
|
开发框架 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的指南
在移动应用开发的广阔天地中,安卓与iOS两大平台各占半壁江山。本文将深入浅出地对比这两大操作系统的开发环境、工具和用户体验设计,揭示它们在编程语言、开发工具以及市场定位上的根本差异。我们将从开发者的视角出发,逐步剖析如何根据项目需求和目标受众选择适合的平台,同时探讨跨平台开发框架的利与弊,为那些立志于打造下一个热门应用的开发者提供一份实用的指南。
27 5