【问题标题】:How can I animate a change to a background image?如何为背景图像的更改设置动画?
【发布时间】:2016-09-08 17:01:53
【问题描述】:

我正在创建一个模糊基础活动的新活动。我通过执行以下操作创建了它:

  • 在旧 Activity 中,截取屏幕截图并将其作为字节数组发送到 Intent 中
  • 在新的活动中,给它一个透明的主题
  • 让新活动从 Intent 中获取图像,然后将其设置为背景。
  • 在其上方添加一层模糊背景的图层。

但是,我想淡化背景变化而不是即时变化。

这就是我现在正在做的事情:

if (getIntent().hasExtra("image")) {
    byte[] byteArray = getIntent().getByteArrayExtra("image");
    if (byteArray != null) {
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        BitmapDrawable drawable = new BitmapDrawable(getResources(), bmp);

        if (drawable != null) {
            mBackgroundFrame.setBackground(drawable);
        }
    }
}

如何使用淡入为 setBackground(drawable) 设置动画?

【问题讨论】:

    标签: android animation


    【解决方案1】:

    假设mBackgroundFrameImageView,您可以按如下方式进行淡入淡出:

    mBackgroundFrame.setBackground(drawable);
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.fadein);
    mBackgroundFrame.startAnimation(animation);
    

    并创建一个anim/fadein.xml 资源:

    <?xml version="1.0" encoding="UTF-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <alpha
            android:duration="3000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0" />
    </set>
    

    【讨论】:

      【解决方案2】:

      你可以简单地使用overridePendingTransition

      Intent i = new Intent(this, NewlyStartedActivity.class);
      startActivity(i);
      overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
      

      fade_in.xml

      <?xml version="1.0" encoding="utf-8"?>
      <alpha xmlns:android="http://schemas.android.com/apk/res/android"
                 android:interpolator="@android:anim/accelerate_interpolator"
                 android:fromAlpha="0.0" android:toAlpha="1.0"
                 android:duration="500" />
      

      fade_out.xml

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

      这将淡出当前的Activity并淡入新的Activity。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 2018-01-05
        相关资源
        最近更新 更多