我制作了一些简单的动画,我称之为呼吸,因为物体会在一个循环中慢慢变大和变小。然后我将此动画与 3 个图像按钮相关联,一切正常,但是当我与一个按钮交互时,它会执行另一个“振动”动画,所有这些都重新启动动画,而预期的行为是只有按下的按钮会重新启动动画。
我想更好地理解这一点,也许如何实现每个按钮的动画都有自己的生命周期。
那是代码:
呼吸.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
<scale
android:duration="1500"
android:startOffset="0"
android:fromXScale="0.9"
android:fromYScale="0.9"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
这就是java代码:
package com.anesoft.android.citmania;
import com.anesoft.android.citmania.models.Defines;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MenuActivity extends Activity implements OnClickListener, AnimationListener {
ImageButton opzioni;
ImageButton play;
ImageButton espansioni;
ImageView logo;
TextView title,title2;
Animation breath,rotate_r,rotate_l,vibrate;
int flag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
logo = (ImageView)findViewById(R.id.logo);
title = (TextView)findViewById(R.id.title);
title2 = (TextView)findViewById(R.id.title2);
opzioni = (ImageButton)findViewById(R.id.options);
play = (ImageButton)findViewById(R.id.play);
espansioni = (ImageButton)findViewById(R.id.expansions);
breath = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.breath);
rotate_r = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_right);
rotate_l = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_left);
vibrate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.vibrate);
vibrate.setAnimationListener(this);
Typeface tf = Typeface.createFromAsset(getAssets(), "font/Canter_Light.otf");
Typeface tf2 = Typeface.createFromAsset(getAssets(), "font/Canter Bold.otf");
title.setTypeface(tf);
title2.setTypeface(tf2);
title.setText("CIT");
title.setTextSize(Defines.TITLE_SIZE);
title2.setText(".MANIA");
title2.setTextSize(Defines.TITLE_SIZE);
opzioni.startAnimation(breath);
play.startAnimation(breath);
espansioni.startAnimation(breath);
logo.setOnClickListener(this);
opzioni.setOnClickListener(this);
play.setOnClickListener(this);
espansioni.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.options) {
Intent i = new Intent(this, OptionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
} else if (id == R.id.play) {
Intent i = new Intent(this, LevelPickerActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
} else if(id == R.id.expansions){
espansioni.startAnimation(vibrate);
Toast.makeText(this, "Implementando", Toast.LENGTH_SHORT).show();
//espansioni.startAnimation(bounce);
}else if(id == R.id.logo){
if(flag==0){
logo.startAnimation(rotate_r);
flag=1;
}else{
logo.startAnimation(rotate_l);
flag=0;
}
}
}
@Override
public void onAnimationEnd(Animation arg0) {
if (arg0 == vibrate) {
espansioni.startAnimation(breath);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
你可以看到我是如何开始振动动画的,然后当它完成时,我只在被点击的按钮中重新开始呼吸,但所有这些都以同步的方式重新开始动画。
提前致谢