java 如何像 Instagram 应用程序一样创建动画背景渐变?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35439573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:09:05  来源:igfitidea点击:

How to create animated background gradient like Instagram app?

javaandroidandroid-layoutandroid-studioandroid-animation

提问by Bugdroid

Below are screenshots of instagram app. The background keeps on transforming from one gradient to other very beautifully. Instagram background gradient

以下是 Instagram 应用程序的屏幕截图。背景不断地从一个渐变过渡到另一个非常漂亮。 Instagram 背景渐变

Instagram background gradient

Instagram 背景渐变

I want to know how can I achieve this. IS THERE ANY CODE TO ANIMATE A GRADIENT AUTOMATICALLY OR DO I NEED TO ADD DIFFERENT GRADIENTS WITH SLIGHT VARIATION, and if I use it won't cause any copyright issues with Instagram? I have tried animating using multiple gradient files with slight variation using Frame Animation but it isn't smooth and doesn't work well. Thanks

我想知道我怎样才能做到这一点。是否有任何代码可以自动为渐变设置动画,或者我是否需要添加具有轻微变化的不同渐变,如果我使用它不会对 Instagram 造成任何版权问题?我曾尝试使用多个渐变文件进行动画处理,使用帧动画略有变化,但它不平滑且效果不佳。谢谢

回答by Farhad Faghihi

Create a TransitionDrawable to change between two drawables that you use for the background.

创建一个 TransitionDrawable 以在用于背景的两个可绘制对象之间进行更改。

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- use whatever drawables you want, gradients, shapes etc-->
    <item android:drawable="@drawable/start" />
    <item android:drawable="@drawable/end" />
</transition>

Transition Drawables in Android

Android 中的过渡可绘制对象

TransitionDrawable trans = (TransitionDrawable) myLayout.getBackground();
trans.startTransition(2000);