Android 动画不显示-为什么?
我是新来的,正在网上学习。这是我第一次在安卓学习动画。问题是,当我选择答案时,我的卡片视图动画应该立即显示。但我一点击它就没显示出来。但我发现,在我点击按钮后,我需要按下Home按钮,然后从后台应用程序中恢复它来启动动画。动画只有在我完成此过程后才会启动。吐司没有问题。在我选择答案后,我总是需要这样做来显示我的动画。这个应用程序是真是假。 另外,编码与我的在线教程完全相同。也许我的电话有问题??我使用的是三星S 7 edgeAndroid 动画不显示-为什么?,android,Android,我是新来的,正在网上学习。这是我第一次在安卓学习动画。问题是,当我选择答案时,我的卡片视图动画应该立即显示。但我一点击它就没显示出来。但我发现,在我点击按钮后,我需要按下Home按钮,然后从后台应用程序中恢复它来启动动画。动画只有在我完成此过程后才会启动。吐司没有问题。在我选择答案后,我总是需要这样做来显示我的动画。这个应用程序是真是假。 另外,编码与我的在线教程完全相同。也许我的电话有问题??我使用的是三星S 7 edge public class MainActivity extends A
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView QuestionTextView,QcountTextView;
private Button Tbutton,Fbutton;
private ImageButton prev,next;
private int count = 0;
private CardView cardView;
private Animation shake;
public Questions Qarray[] = new Questions[]{
new Questions(R.string.Q1, true),
new Questions(R.string.Q2, false),
new Questions(R.string.Q3, true),
new Questions(R.string.Q4, true),
new Questions(R.string.Q5, false),
new Questions(R.string.Q6, true),
new Questions(R.string.Q7, false)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tbutton = findViewById(R.id.tb);
Fbutton = findViewById(R.id.fb);
QuestionTextView = findViewById(R.id.questionshow);
QcountTextView = findViewById(R.id.Qcount);
next = findViewById(R.id.nb);
prev = findViewById(R.id.pb);
cardView = findViewById(R.id.cardView);
shake = AnimationUtils.loadAnimation(MainActivity.this,R.anim.shakeanimation);
Tbutton.setOnClickListener(this);
Fbutton.setOnClickListener(this);
next.setOnClickListener(this);
prev.setOnClickListener(this);
QuestionTextView.setText(Qarray[count].question);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tb:
giveanswer(true);
pagechange();
break;
case R.id.fb:
giveanswer(false);
pagechange();
break;
case R.id.pb:
if(count != 0) {
count = (count - 1);
pagechange();
}
break;
case R.id.nb:
count = (count + 1) % Qarray.length;
pagechange();
break;
}
}
private void pagechange() {
QuestionTextView.setText(Qarray[count].getQuestion());
QcountTextView.setText((count+1) + " out of " + Qarray.length);
}
private void giveanswer(boolean b) {
boolean correctanswer = Qarray[count].ans;
if(b == correctanswer){
fadeView();
Toast.makeText(this,R.string.yes,Toast.LENGTH_SHORT).show();
}
else{
ShakeAnimation();
Toast.makeText(this,R.string.no,Toast.LENGTH_SHORT).show();
}
}
private void fadeView(){
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new
Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.GREEN);
}
@Override
public void onAnimationEnd(Animation animation){
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void ShakeAnimation(){
cardView.setAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
cardView.setCardBackgroundColor(Color.RED);
}
@Override
public void onAnimationEnd(Animation animation) {
cardView.setCardBackgroundColor(Color.rgb(41,226,205));
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
我希望在选择答案后立即显示动画。您需要调用
alphaAnimation.start()代码>或抖动.start()代码>只有动画侦听器并不能在您希望动画启动时启动动画
尝试将这些线添加到希望动画运行的位置
例如:
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(350);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
cardView.setAnimation(alphaAnimation);
alphaAnimation.start();
如果我没有弄错的话,setAnimation
应该与xml动画一起使用您忘了给StartHanks打电话了。我把它改成了cardView.startAnimation(震动);成功了。