2016-08-18 88 views
0

我正在構建我正在開發的應用程序的設計,並且我試圖讓圖像淡入屏幕。但是,圖像在轉換成功時仍然不可見。這是代碼:試圖動畫圖像的阿爾法

facebookLoginButton = (ImageView)findViewById(R.id.facebookLoginButton); // Start of facebookLoginButton code 
facebookLoginButton.setTranslationY(250f); 
facebookLoginButton.setImageAlpha(0); 
facebookLoginButton.animate().translationYBy(-250f).alpha(1).setDuration(1500); // End of facebookLoginButton code 

我知道,因爲當我刪除facebookLoginButton.setImageAlpha(0);,我看到的圖像移動到屏幕上的圖像成功移動。圖像如何保持隱形?

注意:應用程序沒有功能,這就是爲什麼我的按鈕是ImageView。

回答

2

ViewPropertyAnimator返回View.animate()遵循生成器模式。每種方法都會返回待處理的ViewPropertyAnimator,您必須致電start()來激活動畫。

編輯:阿爾法也沒有動畫因爲setImageAlpha()設置View內的圖像的α值,而不是View本身。而ViewPropertyAnimator動畫View的alpha,而不是View內的圖像。雖然ImageView.setAlpha(int alpha)已棄用,但View.setAlpha(float alpha)不是,您必須使用此方法設置View的字母。然後ViewPropertyAnimator可以爲該值創建動畫:

facebookLoginButton.setTranslation(250F); 
facebookLoginButton.setAlpha(0.0F); 
facebookLoginButton.animate() 
      .translationYBy(-250F) 
      .alpha(1.0F) 
      .setDuration(1500) 
      .start(); 
+0

謝謝你教我新的東西。但是,圖像仍然不可見。 –

+0

@ConnectionCoder想通了,檢查編輯。 – Bryan

+0

哇,謝謝你提供這個新信息!現在,我不必將圖像屬性下的初始Alpha設置爲0。非常感謝你! –