这是需要实现的效果,第一个球跟第二个球互换位置,第二个球跟第三个球互换位置,等,这里提供一个思路,先把代码全部贴上来
import com.example.loadingtest.R;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
@SuppressLint("NewApi")
public class AnimatorDialog extends Dialog {
public static final int TIME = 600;
private final static int GREEN = R.drawable.l;
private final static int YELLOW = R.drawable.y;
private final static int RED = R.drawable.h;
private LinearLayout ll_ball1;
private LinearLayout ll_ball2;
private ImageView iv_ball1;
private ImageView iv_ball2;
private ImageView iv_ball3;
private ImageView iv_ball4;
private int count = 1;// 执行动画的次数
private int previousCount;//记录上一次动画执行到哪了
private ObjectAnimator o1;
private ObjectAnimator o2;
public AnimatorDialog(Context context, boolean cancelable,
OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
public AnimatorDialog(Context context, int theme) {
super(context, R.style.my_loading_dialog);
}
public AnimatorDialog(Context context) {
this(context,R.style.my_loading_dialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three_ball);
this.getWindow().getAttributes().gravity = Gravity.CENTER;
ll_ball1 = (LinearLayout) findViewById(R.id.ll_ball1);
ll_ball2 = (LinearLayout) findViewById(R.id.ll_ball2);
iv_ball1 = (ImageView) findViewById(R.id.iv_ball1);
iv_ball2 = (ImageView) findViewById(R.id.iv_ball2);
iv_ball3 = (ImageView) findViewById(R.id.iv_ball3);
iv_ball4 = (ImageView) findViewById(R.id.iv_ball4);
configAnimator();
}
public void startAnimator() {
if(o1 != null){
if(o1.isRunning()){
return;
}
if(previousCount != 0)
count = previousCount;
o1.start();
}
}
public void stopAnimator(){
if(o1 != null){
o1.cancel();
o1.end();
}
if(o2 != null){
o2.cancel();
o2.end();
}
previousCount = count;
count = 0;
}
@Override
public void show() {
super.show();
startAnimator();
}
@Override
public void dismiss() {
stopAnimator();
super.dismiss();
}
public void configAnimator() {
final int mWidth = (ll_ball1.getRight() - ll_ball1.getLeft()) / 2;
final int rotationX2 = (ll_ball2.getRight() - ll_ball2.getLeft()) / 2;
o1 = ObjectAnimator.ofFloat(ll_ball1, "rotation",
0, 180);
ll_ball1.setRotationX(mWidth);
ll_ball1.setRotationY(ll_ball1.getHeight() / 2);
o1.setInterpolator(new DecelerateInterpolator());
o1.setDuration(TIME);
o1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (count == 1) {
iv_ball3.setBackgroundResource(GREEN);
iv_ball3.setAlpha(1f);
iv_ball1.setAlpha(0f);
count++;
o2.start();
}else if(count == 3){
iv_ball4.setBackgroundResource(GREEN);
iv_ball4.setAlpha(1f);
iv_ball3.setBackgroundResource(YELLOW);
iv_ball1.setAlpha(0f);
ll_ball2.setRotation(180);
count++;
o2.start();
}else if(count == 5){
iv_ball4.setBackgroundResource(YELLOW);
iv_ball4.setAlpha(1f);
iv_ball3.setBackgroundResource(RED);
iv_ball1.setAlpha(0f);
ll_ball2.setRotation(180);
count ++;
o2.start();
}else if(count == 7){
iv_ball3.setBackgroundResource(GREEN);
iv_ball1.setAlpha(0f);
iv_ball4.setBackgroundResource(RED);
iv_ball4.setAlpha(1f);
ll_ball2.setRotation(0);
count = 2;
o2.start();
}
}
});
o2 = ObjectAnimator.ofFloat(ll_ball2, "rotation",
0, 180);
ll_ball2.setRotationX(rotationX2);
ll_ball2.setRotationY(ll_ball2.getHeight() / 2);
o2.setInterpolator(new DecelerateInterpolator());
o2.setDuration(TIME);
o2.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (count == 2) {
iv_ball1.setBackgroundResource(YELLOW);
iv_ball1.setAlpha(1f);
iv_ball2.setBackgroundResource(RED);
iv_ball4.setAlpha(0f);
ll_ball1.setRotation(180);
count++;
o1.start();
}else if(count == 4){
iv_ball1.setBackgroundResource(RED);
iv_ball1.setAlpha(1f);
iv_ball2.setBackgroundResource(GREEN);
iv_ball4.setAlpha(0f);
ll_ball1.setRotation(180);
count++;
o1.start();
}else if(count == 6){
iv_ball1.setBackgroundResource(GREEN);
iv_ball1.setAlpha(1f);
iv_ball2.setBackgroundResource(YELLOW);
iv_ball4.setAlpha(0f);
ll_ball1.setRotation(180);
ll_ball2.setRotation(180);
count = 7;
o1.start();
}
}
});
}
}
因为这是一个dialog,所以使用直接调用dialog.show()方法就好!这里面使用到了三张颜色的图片,也可以自己随便找三张图片替换以下,因为是项目中需要所以匆匆记录一下!!!