在最初不可见时淡入
Fade in while being initially invisible
按照此处的指南操作:
我已经成功地为我的 imageView 实现淡入淡出动画。然而,imageView 最初是可见的,当动画开始时它只是闪烁变为不可见(alpha = 0.0)并慢慢淡入回到 alpha = 1.0。这里的问题是,如何设置 imageView 初始不可见?
我已尝试将 xml 布局中的可见性更改为 INVISIBLE
和 GONE
,但动画不起作用。它将在最终结果中显示为空白 imageView。
我还尝试更改 xml 布局中 imageView 的 alpha 级别,但结果动画很奇怪,因为它应用于我配置的 alpha 级别之上。例如,如果我将 alpha 级别设置为 0.3,则动画将在 0.0 到 0.3 之间淡出。如果我把它设置为0.0,它看起来像没有动画,最后不显示图像。
这是我的代码片段:
anim/fade_in_view.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="1000"
android:repeatCount="0" />
</set>
layout/activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >
<ImageView
android:id="@+id/imgCharacter"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgLogo"
android:src="@drawable/darth_vader" />
</RelativeLayout>
src/MainActivity.java
protected void onCreate(Bundle savedInstanceState)
{
...
imageView = (ImageView) findViewById(R.id.imgCharacter);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(SplashScreen.this, R.anim.fade_in_view);
imageView.startAnimation(fadeInAnimation);
}
}, 1000);
}
替换您的新处理程序方法
到
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Animation fadeInAnimation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.fade_in_view);
imageView.startAnimation(fadeInAnimation);
fadeInAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
imageView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
}
}, 1000);
并在 activity_splash.xml
set imageview visibility to invisible
按照此处的指南操作: 我已经成功地为我的 imageView 实现淡入淡出动画。然而,imageView 最初是可见的,当动画开始时它只是闪烁变为不可见(alpha = 0.0)并慢慢淡入回到 alpha = 1.0。这里的问题是,如何设置 imageView 初始不可见?
我已尝试将 xml 布局中的可见性更改为 INVISIBLE
和 GONE
,但动画不起作用。它将在最终结果中显示为空白 imageView。
我还尝试更改 xml 布局中 imageView 的 alpha 级别,但结果动画很奇怪,因为它应用于我配置的 alpha 级别之上。例如,如果我将 alpha 级别设置为 0.3,则动画将在 0.0 到 0.3 之间淡出。如果我把它设置为0.0,它看起来像没有动画,最后不显示图像。
这是我的代码片段:
anim/fade_in_view.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="1000"
android:repeatCount="0" />
</set>
layout/activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >
<ImageView
android:id="@+id/imgCharacter"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imgLogo"
android:src="@drawable/darth_vader" />
</RelativeLayout>
src/MainActivity.java
protected void onCreate(Bundle savedInstanceState)
{
...
imageView = (ImageView) findViewById(R.id.imgCharacter);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Animation fadeInAnimation = AnimationUtils.loadAnimation(SplashScreen.this, R.anim.fade_in_view);
imageView.startAnimation(fadeInAnimation);
}
}, 1000);
}
替换您的新处理程序方法
到
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Animation fadeInAnimation = AnimationUtils.loadAnimation(
MainActivity.this, R.anim.fade_in_view);
imageView.startAnimation(fadeInAnimation);
fadeInAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
imageView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
}
}, 1000);
并在 activity_splash.xml
set imageview visibility to invisible