【问题标题】:Android change background image with fade in/out animationAndroid使用淡入/淡出动画更改背景图像
【发布时间】:2014-09-16 08:14:42
【问题描述】:

我编写的代码可以每 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);

}

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

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

【问题讨论】:

标签: android android-imageview android-animation


【解决方案1】:

您需要调用这些代码。

首先,你必须为这样的淡入淡出动画制作两个 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 以在淡入完成时更改淡出。

Animation fadeOut = AnimationUtils.loadAnimation(YourActivity.this, R.anim.fade_out);
imageView.startAnimation(fadeOut);

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

【讨论】:

  • 就像一个魅力。将淡入和淡出都应用于我的 LinearLayout 视图。必须更改为 500 毫秒。 2000 年太长了。
  • 是不是应该是 imageView.startAnimation(fadeIn);
  • 这不是在回答真正的问题。这是ImageView(基本上是View)上的动画,而不是View 的background。
  • 这个YouTube video是真正的答案
【解决方案2】:

对于动画淡入,在 res 文件夹中创建名为“anim”的新文件夹,并在其中使用以下代码创建“fade_in.xml”

<?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 的值

希望这会有所帮助。

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      【解决方案4】:

      你需要 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:src="@anim/anim_android" 的 ImageView。

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

      第二步

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

      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) {
                }
      });
      

      你不需要处理程序。

      【讨论】:

        【解决方案5】:

        kimkevin 的回答为View 设置动画(可以是ImageView、TextView 等),而不是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()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-06
          • 2014-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多