以前网上找的感觉挺好看的,分享一下。
效果图
SpotsDialog
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import com.c.mysmall.app.main.R;
/**
* 加载中
*/
public class SpotsDialog extends AlertDialog {
private static final int DELAY = 150;
private static final int DURATION = 1500;
private int size;
private AnimatedView[] spots;
private AnimatorPlayer animator;
public SpotsDialog(Context context) {
this(context, R.style.SpotsDialogDefault);
}
public SpotsDialog(Context context, int theme) {
super(context, theme);
}
public SpotsDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
setCanceledOnTouchOutside(false);
initProgress();
}
@Override
protected void onStart() {
super.onStart();
animator = new AnimatorPlayer(createAnimations());
animator.play();
}
@Override
protected void onStop() {
super.onStop();
animator.stop();
}
//~
private void initProgress() {
ProgressLayout progress = (ProgressLayout) findViewById(R.id.progress);
size = progress.getSpotsCount();
spots = new AnimatedView[size];
int size = getContext().getResources().getDimensionPixelSize(R.dimen.spot_size);
int progressWidth = getContext().getResources().getDimensionPixelSize(R.dimen.progress_width);
for (int i = 0; i < spots.length; i++) {
AnimatedView v = new AnimatedView(getContext());
v.setBackgroundResource(R.drawable.dlg_spot);
v.setTarget(progressWidth);
v.setXFactor(-1f);
progress.addView(v, size, size);
spots[i] = v;
}
}
private Animator[] createAnimations() {
Animator[] animators = new Animator[size];
for (int i = 0; i < spots.length; i++) {
Animator move = ObjectAnimator.ofFloat(spots[i], "xFactor", 0, 1);
move.setDuration(DURATION);
move.setInterpolator(new HesitateInterpolator());
move.setStartDelay(DELAY * i);
animators[i] = move;
}
return animators;
}
}
AnimatedView
class AnimatedView extends View {
private int target;
public AnimatedView(Context context) {
super(context);
}
public float getXFactor() {
return getX() / target;
}
public void setXFactor(float xFactor) {
setX(target * xFactor);
}
public void setTarget(int target) {
this.target = target;
}
public int getTarget() {
return target;
}
}
AnimatorPlayer
class AnimatorPlayer extends AnimatorListenerAdapter {
private boolean interrupted = false;
private Animator[] animators;
public AnimatorPlayer(Animator[] animators) {
this.animators = animators;
}
@Override
public void onAnimationEnd(Animator animation) {
if (!interrupted) animate();
}
public void play() {
animate();
}
public void stop() {
interrupted = true;
}
private void animate() {
AnimatorSet set = new AnimatorSet();
set.playTogether(animators);
set.addListener(this);
set.start();
}
}
HesitateInterpolator
class HesitateInterpolator implements Interpolator {
private double POW = 1.0/2.0;
@Override
public float getInterpolation(float input) {
return input < 0.5
? (float) Math.pow(input * 2, POW) * 0.5f
: (float) Math.pow((1 - input) * 2, POW) * -0.5f + 1;
}
}
ProgressLayout
class ProgressLayout extends FrameLayout {
private static final int DEFAULT_COUNT = 5;
private int spotsCount;
public ProgressLayout(Context context) {
this(context, null);
}
public ProgressLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs, defStyleAttr, defStyleRes);
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(
attrs,
R.styleable.Dialog,
defStyleAttr, defStyleRes);
spotsCount = a.getInt(R.styleable.Dialog_DialogSpotCount, DEFAULT_COUNT);
a.recycle();
}
public int getSpotsCount() {
return spotsCount;
}
}
attrs文件
<resources>
<declare-styleable name="Dialog">
<attr name="DialogTitleAppearance" format="reference" />
<attr name="DialogTitleText" format="reference|string" />
<attr name="DialogSpotColor" format="reference|color"/>
<attr name="DialogSpotCount" format="integer"/>
</declare-styleable>
</resources>
styles文件
<style name="SpotsDialogDefault" parent="android:Theme.DeviceDefault.Light.Dialog">
<item name="DialogTitleAppearance">@android:style/TextAppearance.Medium</item>
<item name="DialogTitleText">Loading…</item>
<item name="DialogSpotCount">5</item>
</style>
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/title_margin"
android:layout_gravity="center"
android:textAppearance="?attr/DialogTitleAppearance"
android:text="?attr/DialogTitleText"/>
<com.example.ProgressLayout
android:id="@+id/progress"
android:layout_width="@dimen/progress_width"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/progress_margin"/>
</LinearLayout>
dlg_spot.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/holo_blue_dark" />
</shape>
dimen.xml
<dimen name="spot_size">6dp</dimen>
<dimen name="title_margin">10dp</dimen>
<dimen name="progress_margin">24dp</dimen>
<dimen name="progress_width">250dp</dimen>