2013-08-05 43 views
0

我知道這個問題已經被問了好幾次了,但我只是不知道在哪裏放置代碼並不斷收到錯誤。它是一個基本的淡入淡出動畫的圖像,我想首先隱藏,然後淡入。預先感謝 我的動畫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; 
} 

} 
+0

它不乾淨你的要求。 – user2870

+0

您是否嘗試將代碼放在'onResume()'中? –

+0

只是把你的代碼rImage.startAnimation(a);在onCreate()方法。 – Andrain

回答

1

刪除rImage.startAnimation();來自點擊監聽器。

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

does not work ... loadAnimation在點擊列表中出現錯誤,出於某種原因 – user2643867

+0

正如我所說的,從onClick監聽器中刪除它,你已經說過你想要在不需要點擊的情況下啓動動畫。另外,提供你正在得到的錯誤的堆棧跟蹤將會非常有用。 – kev

+0

哦,好吧,它現在可以工作..謝謝你soo多爲你的幫助! – user2643867

0

看看你能不能啓動的onCreate一個動畫,因爲它不是那種粘到主界面,搜索更

這裏是一個鏈接,這將幫助你。

Animation

+0

我在我的應用程序中使用了向下滑動動畫,我希望每當打開應用程序時動畫都將自動處理,無需按鈕。給我解決方案.. – jeevamuthu

1

您可以使用droidQuery完成動畫:

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