Android自定义ProgressDialog

Android自定义ProgressDialog

Android应用开发中,加载对话框是最常用也是最基本的一个控件,主要用于网络请求的等待过程。Android其实有自带的ProgressDialog可以做到加载等待的效果,但是其ProgressBar的样式不容易进行自定义设置,比如颜色,没有现成的api可以更改ProgressDialog进度条的颜色,还以一个KPKProgressHUD,也可以实现进度对话框,但都是封装好的。如果要自己写一个进度对话框,该怎么写呢?

我的写法比较简单,需要3个资源文件,一个是要旋转的图片,一个是对话框的布局文件,一个是图片的旋转动画文件。图片资源这里不再给出,可以让UI给,这里贴一下后两个资源文件的内容。

对话框的布局文件dialog_layout.xml

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:id="@+id/dialog_layout"
   
android:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:minHeight="60dp"
   
android:minWidth="180dp"
   
android:gravity="center"
   
android:padding="10dp"
   
>
    <ImageView
       
android:id="@+id/loading_image"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:src="@mipmap/loading"
       
/>
    <TextView
       
android:id="@+id/tip_textview"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="10dp"
       
android:text=" "/>
</LinearLayout>

 

旋转动画文件loading_anim.xml

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

    <rotate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="359"
        android:duration="1500"
        android:repeatCount="-1"
        />

我的Activity的写法如下:

public class DialogActivity extends AppCompatActivity {
    private Button showBtn,dismissBtn;
    private Dialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_main);
        showBtn = (Button) findViewById(R.id.show);
        dismissBtn = (Button) findViewById(R.id.dismiss);
        mDialog = createLoadingDialog(this,"test");
        showBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.show();
            }
        });

    }

    public static Dialog createLoadingDialog(Context context, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog_layout, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                context, R.anim.loading_anim);
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog

        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        return loadingDialog;

    }
}

 

经本人测试,效果很好,足以满足一般项目的需求。在这期间可能会碰到两个问题:

1、  旋转动画的写法,如果不注意,动画不是匀速的,而是变速的,就是速度先变快,在动画快结束的时候速度为0,视觉上有一种停顿的现象出现,原因就是资源文件中的android:interpolator属性没有生效,有两个解决办法:第一是看下资源文件最外层的标签是不是set,如果是set,把set层去掉,保证rotate标签处于最外层;第二个办法就是代码动态设置动画的加速器:

Animation rotate = AnimationUtils.loadAnimation(this, R.drawable.rotate_anim);
LinearInterpolator lin = new LinearInterpolator();
rotate.setInterpolator(lin);
((ImageView)findViewById(R.id.img)).setAnimation(rotate);
((ImageView)findViewById(R.id.img)).startAnimation(rotate);

 

2、  动画资源文件的所在目录,android项目的资源目录res下面关于动画有两个子目录anim和animator,这两个子目录的区别就是:

anim文件夹下存放tween animation和frame animation;xml文件里只有scale、rotate、translate、alpha、set五个标签;在代码中使用AnimationUtils.loadAnimation()方法加载;使用mView.setAnimation(mAnimation)为mView加载动画;使用mView.startAnimation()开启动画;

animator文件夹下存放property animation,即属性动画,xml文件里有animator、objectAnimator和set三个标签;在代码中使用AnimatorInflater.loadAnimator()方法加载动画;使用mAnimation.setTarget(mView)为mView加载动画。使用mAnimation.start()开启动画

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android海纳百川

打赏加微信,送跑车加管理

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值