2016-08-18 90 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。非常感谢你! –