Android 加载动画

1.仿京东小人加载动画

loading_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@mipmap/loding_one" android:duration="150"/>
    <item android:drawable="@mipmap/loding_two" android:duration="150"/>
    <item android:drawable="@mipmap/loding_three" android:duration="150"/>
    <item android:drawable="@mipmap/loding_four" android:duration="150"/>
</animation-list>

帧动画:4张图片一帧一帧连接起来,形成动画

layout_loading_dialog.xml

<?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:minWidth="100dp"
    android:gravity="center"
    android:padding="20dp"
    android:background="#ccc"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/dialog_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/aaa"
        />
    <TextView
        android:gravity="center"
        android:id="@+id/dialog_message"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

自定义ProgressDialog

package com.example.think.progressdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class ProgressDialog extends Dialog {

    private TextView mMessageView;
    private ImageView mSpaceshipImage;
    private Context mContext;
    private AnimationDrawable animationDrawable;

    public ProgressDialog(Context context, int message, int negativeButtonText, View.OnClickListener listener) {
        this(context, context.getString(message), context.getString(negativeButtonText), listener);
    }

    public ProgressDialog(Context context, String message, String negativeButtonText, View.OnClickListener listener) {
        super(context, R.style.ProgressDialog);
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        //加载布局
        View inflate = inflater.inflate(R.layout.layout_loading_dialog, null);
        //初始化控件Id
        mSpaceshipImage = inflate.findViewById(R.id.dialog_image);
        mMessageView = inflate.findViewById(R.id.dialog_message);
        //设置提示文字
        mMessageView.setText(message);
        setContentView(inflate);
        setCancelable(false);
    }

    public void setMessage(String message) {
        if (mMessageView != null) {
           mMessageView.setText(message);
       }
    }

    public void setMessage(int message) {
        if (mMessageView != null) {
           mMessageView.setText(message);
        }
    }

    @Override
    public void show() {
        // 加载动画
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(mContext, R.anim.loading_animation);
       // 使用ImageView显示动画
        mSpaceshipImage.startAnimation(hyperspaceJumpAnimation);
        //小人动画
         /* mSpaceshipImage.setImageResource(R.drawable.loading_animation_2);
        animationDrawable = (AnimationDrawable) mSpaceshipImage.getDrawable();
        animationDrawable.start();*/
        super.show();
    }
    //关闭动画
    @Override
    public void dismiss() {
        animationDrawable = (AnimationDrawable) mSpaceshipImage.getDrawable();
        animationDrawable.stop();
        super.dismiss();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值