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();
}
}