2016-07-01 119 views
0

我的代碼的問題是,它先顯示圖像,然後顯示動畫(淡入)。首先,圖像在屏幕上顯示大約一秒鐘,然後淡入的相同的圖像發生。請幫助。 這是代碼:應用程序首先顯示圖像,然後顯示動畫

public class LoginActivity extends Activity { 

    private Animation translate; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_login); 



     final ImageView image = (ImageView) findViewById(R.id.image); 
     Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate); 
     image.startAnimation(translate); 

     final int[] imageArray = {R.drawable.agra, R.drawable.bang, 
       R.drawable.mumbai 
     }; 

     final Handler handler = new Handler(); 
     Runnable runnable = new Runnable() { 
      int i = 0; 

      public void run() { 
       image.setImageResource(imageArray[i]); 
       i++; 
       if (i > imageArray.length - 1) { 
        i = 0; 
       } 
       handler.postDelayed(this, 4000); 
      } 
     }; 
     handler.postDelayed(runnable, 4000); 

    } 




} 

,這是動畫文件translate.xml

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="4000" 
    android:fillAfter="true" 
    android:fromAlpha="0" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:repeatCount="-1" 
    android:toAlpha="1"> 
</alpha> 

回答

0

在你的XML,設置圖像視圖不可見:

<ImageView 
    .... 
    android:visibility:"invisible"/> 

然後,嘗試更新您的代碼,如下所示:

final ImageView image = (ImageView) findViewById(R.id.image); 
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate); 
translate.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     findViewById(R.id.image).setVisibility(View.VISIBLE); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 
}); 

image.startAnimation(translate); 
+0

感謝您的快速回答,但問題仍然存在 –

相關問題