【发布时间】:2015-05-10 10:25:22
【问题描述】:
我的活动中有 9 个按钮,我使用右推动画在创建时显示我的按钮,我将持续时间更改为设置显示,所以我有 9 个具有不同持续时间的 animation.xml(例如从 1000 到 5000)。
我还必须在活动中重复 3 行代码 9 次,
final Button b = (Button)findViewById(R.id.Button06);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
b.startAnimation(anim);
所以,我需要知道有没有更简单的方法可以用更少的代码来制作类似的东西? 例如,使用1个animation.xml并定义按钮来运行动画一个接一个?!
我的完整代码:
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b = (Button)findViewById(R.id.Button06);
Animation anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
b.startAnimation(anim);
final Button b1 = (Button)findViewById(R.id.Button03);
Animation anim1=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in1);
b1.startAnimation(anim1);
final Button b2 = (Button)findViewById(R.id.button1);
Animation anim2=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in2);
b2.startAnimation(anim2);
final Button b3 = (Button)findViewById(R.id.Button08);
Animation anim3=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in3);
b3.startAnimation(anim3);
final Button b4 = (Button)findViewById(R.id.Button04);
Animation anim4=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in4);
b4.startAnimation(anim4);
final Button b5 = (Button)findViewById(R.id.Button01);
Animation anim5=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in5);
b5.startAnimation(anim5);
final Button b6 = (Button)findViewById(R.id.Button07);
Animation anim6=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in6);
b6.startAnimation(anim6);
final Button b7 = (Button)findViewById(R.id.Button05);
Animation anim7=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in7);
b7.startAnimation(anim7);
final Button b8 = (Button)findViewById(R.id.Button02);
Animation anim8=AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in8);
b8.startAnimation(anim8);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startButtonAnimation(b);
}
});
}
public void startButtonAnimation(Button btn){
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
btn.setAnimation(shake);
btn.startAnimation(shake);
shake.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(getApplicationContext(), Activity2.class));
overridePendingTransition(R.anim.animation, R.anim.animation2);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
你试过我的解决方案了吗?
-
我已经使用其他方法在媒体上制作动画并更改活动,所以我很困惑,我把我的所有代码都放了,如果你修复它并给我完整的代码,那就太好了,谢谢
-
现在试试我的解决方案
-
但是再想一想,现在您只为一个按钮设置了一个单击侦听器 - 如果您想为所有按钮设置它,请给我信息,我会更新我的帖子
-
我做了导入但仍然有错误:
标签: android animation duration