无需点击即可自动启动动画

时间:2013-08-05 12:13:37

标签: android animation

我知道这个问题已被问过几次,但我不知道在哪里放置代码并不断出错。它是一个基本淡化动画的图像,我想先隐身然后淡入。提前感谢 我的动画xml后跟类的java。

XML:

<?xml version="1.0" encoding="UTF-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android">
     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
      android:interpolator="@android:anim/accelerate_interpolator" 
      android:duration="4000"/>
 </set>

爪哇:

package com.example.whattodo2;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Title extends Activity {
Button reset, rts;
ImageView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_title);
    reset = (Button) findViewById(R.id.reset);
    reset.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                       double rand = Math.random();
                       if(rand < 0.5){
                           Intent reset1 = new Intent(Title.this, MainActivity.class);
                           startActivity(reset1);
                       } else {
                           Intent reset2 = new Intent(Title.this, Question36.class);
                           startActivity(reset2);
                       }
                }
            });
    rts = (Button) findViewById(R.id.rts);
    rts.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent rts=new Intent(Title.this,Rts.class);
                    startActivity(rts);

                }
            });

         final Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
         a.reset();
         final ImageView rImage = (ImageView) findViewById(R.id.title);

         RelativeLayout layout = (RelativeLayout) findViewById(R.id.root);
         layout.setOnClickListener(new OnClickListener() {

             @Override public void onClick(View v) {
         rImage.startAnimation(a);
         func(); //A call to the function.

         }
         });


}

protected void func() {
    // TODO Auto-generated method stub

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.title, menu);
    return true;
}

}

3 个答案:

答案 0 :(得分:1)

删除rImage.startAnimation();来自点击监听器。

Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
ImageView rImage = (ImageView) findViewById(R.id.title);
rImage.startAnimation(a);

答案 1 :(得分:1)

您可以使用droidQuery完成动画:

$.with(rImage).attr("alpha", 0f).animate("{ alpha    : 1.0 }",
                    new AnimationOptions("{ duration : 4000,
                                            easing   : accelerate }"));

答案 2 :(得分:0)

看到你无法在onCreate中启动动画,因为它不是主要的ui,搜索更多

这是一个可以帮助您的链接。

Animation