Android用淡入/淡出动画改变背景图像

时间:2014-07-24 16:36:57

标签: android android-imageview android-animation

我编写的代码可以每隔5秒随机更改一次背景图像。现在我想使用淡入/淡出动画来改变背景图像,但我不知道如何使用这个动画。

这是我的来源:

void handlechange() {

    Handler hand = new Handler();
    hand.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            // change image here
            change();

        }

        private void change() {
            // TODO Auto-generated method stub

            Random rand = new Random();

            int index = rand.nextInt(image_rundow.length);

            mapimg.setBackgroundResource(image_rundow[index]);

            handlechange();
        }
    }, 4000);

}

目前一切正常。我可以随机更改背景图像,但我不知道如何使用动画淡入/淡出。

如果有人知道解决方案,请帮助我, 感谢。

5 个答案:

答案 0 :(得分:16)

您需要调用这些代码。

首先,您必须制作两个用于淡入淡出的xml文件。像这样的动画。

fade_in.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:fillAfter="true"
        android:duration="2000"
        />
</set>

fade_out.xml

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

其次,您必须像下面一样运行imageView的动画,并且还必须设置AnimationListener以在fadein完成时更改淡出。

Animation fadeIn = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.startAnimation(fadeIn);

fadeIn.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {
      }
      @Override
      public void onAnimationEnd(Animation animation) {
          Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out);
          imageView.startAnimation(fadeOut);
      }
      @Override
      public void onAnimationRepeat(Animation animation) {
      }
});

答案 1 :(得分:5)

对于淡入动画,请创建名为&#39; anim&#39;的新文件夹。在你的res文件夹和里面,创建&#39; fade_in.xml&#39;使用以下代码

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

    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="1.0" />

</set>

现在要在您的imageview上运行此动画,请在您的活动中使用以下代码

Animation anim = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_in);
imageView.setAnimation(anim);
anim.start();

对于淡出动画,只需交换android:fromAlpha和android:toAlpha

的值

希望这有帮助。

答案 2 :(得分:2)

您可以使用relativeLayout,并添加一层背景视图,其中设置高度和宽度match_parent。所有其他UI元素都应放在此视图的顶部。在你的代码中。定义fadeOut和fadeIn动画。按ID查找背景视图,然后执行以下操作:

 xxxBackground.startAnimation(fadeOut);
 xxxBackground.setBackgroundResource(R.drawable.your_random_pic);
 xxxBackground.startAnimation(fadeIn);

您可以使用一些插补器来调整性能。

答案 3 :(得分:1)

你需要动画的AnimationDrawable。

第一步AnimationDrawable

创建文件/res/anim/anim_android.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="false">
    <item android:drawable="@drawable/android_1"
          android:duration="100"/>
    <item android:drawable="@drawable/android_2"
          android:duration="100"/>
    <item android:drawable="@drawable/android_3"
          android:duration="100"/>
    <item android:drawable="@drawable/android_4"
          android:duration="100"/>
    <item android:drawable="@drawable/android_5"
          android:duration="100"/>
    <item android:drawable="@drawable/android_6"
          android:duration="100"/>
    <item android:drawable="@drawable/android_7"
          android:duration="100"/>
</animation-list>

- 使用android添加一个ImageView:src =&#34; @ anim / anim_android&#34;。

<ImageView
    android:id="@+id/myanimation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@anim/anim_android" />

第二步

- 在您的活动中创建AnimationDrawable和动画

AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
            animationDrawable.setOneShot(true);
            animationDrawable.start();

    Animation animation = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_in);

    imageView.setAnimation(animation);
        animation.start();
        animation.setAnimationListener(new Animation.AnimationListener() {
          @Override
          public void onAnimationStart(Animation animation) {
          }
          @Override
          public void onAnimationEnd(Animation animation) {
              Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, android.R.anim.fade_out);
              imageView.startAnimation(fadeOut);
          }
          @Override
          public void onAnimationRepeat(Animation animation) {
          }
});

你不需要Handler。

答案 4 :(得分:1)

kimkevin回答的动画是View(可能是ImageViewTextView等)而不是View的背景。这意味着,如果您只想突出显示任何View,则必须在其后添加另一个View(将其称为backgroundView)并为该backgroundView设置动画。

使用以下代码为视图背景设置动画

val startColor = view.solidColor
val endColor = ContextCompat.getColor(context, R.color.your_color)

val colorAnim = ObjectAnimator.ofInt(view, "backgroundColor", startColor, endColor, startColor)
colorAnim.duration = 2000
colorAnim.setEvaluator(ArgbEvaluator())
colorAnim.start()
相关问题