2016-02-24 88 views
1

所以我試圖在動畫中實現啓動畫面和圖像視圖淡入淡出,我想要同時啓動啓動畫面活動。我還希望啓動屏幕活動在短暫延遲後結束(觸摸事件是可選的),在圖像視圖動畫完成後。在ImageView動畫中淡入淡出的畫面

我SplashScreen.java:

package hillbillys.delivery; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class SplashScreen extends AppCompatActivity implements Animation.AnimationListener { 
protected Animation fadeIn; 
protected ImageView img1; 
protected ImageView img2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.screen_splash); 

    fadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in); 

    /* 
       img1.setVisibility(View.VISIBLE); 
       img2.setVisibility(View.VISIBLE); 
       img1.startAnimation(fadeIn); 
       img2.startAnimation(fadeIn); 
    */ 

    Thread timerThread = new Thread(){ 
     public void run(){ 
      try{ 
       sleep (2000); 
      }catch(InterruptedException e){ 
       e.printStackTrace(); 
      }finally{ 
       Intent intent = new Intent(SplashScreen.this,MainActivity.class); 
       startActivity(intent); 
      } 
     } 
    }; 
    timerThread.start(); 


} 

@Override 
protected void onPause() { 
    super.onPause(); 
    finish(); 
} 

@Override 
public void onAnimationStart(Animation animation) { 


} 

@Override 
public void onAnimationEnd(Animation animation) { 
    Toast.makeText(getBaseContext(), "Animation Stopped!", Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onAnimationRepeat(Animation animation) { 

} 
} 

應用程序崩潰每次我嘗試添加的代碼塊在留言的時候,無論在什麼地方,我把它。沒有淡入淡出動畫工作就好了。有什麼辦法可以輕鬆地同步這兩個?在編碼方面我相當新,所以在我試圖實現的東西中可能會有一些致命的錯誤。

我screeen_splash.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#fff0d9" 
android:orientation="vertical"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView" 
    android:layout_gravity="center_horizontal" 
    android:src="@drawable/logo1" 
    android:layout_marginTop="250dp" 
    android:visibility="gone" /> 

<ImageView 
    android:layout_width="260dp" 
    android:layout_height="41dp" 
    android:id="@+id/imageView2" 
    android:layout_gravity="center_horizontal" 
    android:src="@drawable/logo2" 
    android:visibility="gone" /> 

</LinearLayout> 

我fade_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillAfter="true"> 

<alpha 
    android:duration="1000" 
    android:fromAlpha="0.0" 
    android:interpolator="@android:anim/accelerate_interpolator" 
    android:toAlpha="1.0" 
    /> 

</set> 
+0

您可以使用Picasso加載ImageView和淡入淡出動畫。睡眠後(2000)再次使用畢加索。 – PatrickMA

回答

3

您需要在嘗試訪問他們的屬性之前初始化ImageView。例如。

img1 = (ImageView) findViewById(R.id.imageView); 
img2 = (ImageView) findViewById(R.id.imageView2); 
img1.setVisibility(View.VISIBLE); 
img2.setVisibility(View.VISIBLE); 
img1.startAnimation(fadeIn); 
img2.startAnimation(fadeIn); 
+0

好吧,你回答得更快:D我會刪除我的不是垃圾郵件 – MyWay

+0

愚蠢的我非常感謝它的工作 –

0

像黑帶說你需要初始化,

final img1 = (ImageView) findViewById(R.id.imageView); 
final img2 = (ImageView) findViewById(R.id.imageView2); 

,如果你想將動畫正確啓動創建視圖後,你需要做的這個

img1.post(new Runnable(){ 
    img1.startAnimation(fadeIn); 
}); 

img2.post(new Runnable(){ 
    img2.startAnimation(fadeIn); 
}); 
0
Animation animation= AnimationUtils.loadAnimation(this,R.anim.fadeIn); 
img1.startAnimation(animation); 
+0

感謝您的參與。不鼓勵Coe-Only的回答。請給出一些解釋,以便更好地理解。 –