Instagram应用程序登录时背景如何变化?

我让我的应用程序每隔几毫秒更改一次背景颜色。 但它对用户没有吸引力。 如果您已经看过Instagram的登录屏幕,它会以非常柔和的速度改变颜色并且效果模糊。

我只想要我的应用程序的那种背景。 我应该怎么做呢

使用以下代码可能是您的良好开端:

public class BackgroundPainter { private static final int MIN = 800; private static final int MAX = 1500; private final Random random; public BackgroundPainter() { random = new Random(); } public void animate(@NonNull final View target, @ColorInt final int color1, @ColorInt final int color2) { final ValueAnimator valueAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), color1, color2); valueAnimator.setDuration(randInt(MIN, MAX)); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { target.setBackgroundColor((int) animation.getAnimatedValue()); } }); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { //reverse animation animate(target, color2, color1); } }); valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); valueAnimator.start(); } private int randInt(int min, int max) { return random.nextInt((max - min) + 1) + min; } } 

用法:

 final View targetView = findViewById(R.id.root_view); BackgroundPainter backgroundPainter = new BackgroundPainter(); int color1 = ContextCompat.getColor(this, R.color.colorAccent); int color2 = ContextCompat.getColor(this, R.color.colorPrimary); backgroundPainter.animate(targetView, color1, color2); 

更新

要更改由多种颜色组成的背景,一般来说可以使用drawable而不是ValueAnimator您可以尝试使用以下解决方案。

我已在使用API​​ 19和23的Device上测试了此代码。

colors.xml定义颜色:

  #9C27B0 #FF4081 #7B1FA2 #F8BBD0 #FF5252 #607D8B #FF5722 #FFA000 

drawable定义渐变:

gradient_1.xml

     

gradient_2.xml

     

gradient_3.xml

     

在项目中创建GradientBackgroundPainter类:

 public class GradientBackgroundPainter { private static final int MIN = 4000; private static final int MAX = 5000; private final Random random; private final Handler handler; private final View target; private final int[] drawables; private final Context context; public GradientBackgroundPainter(@NonNull View target, int[] drawables) { this.target = target; this.drawables = drawables; random = new Random(); handler = new Handler(); context = target.getContext().getApplicationContext(); } private void animate(final int firstDrawable, int secondDrawable, final int duration) { if (secondDrawable >= drawables.length) { secondDrawable = 0; } final Drawable first = ContextCompat.getDrawable(context, drawables[firstDrawable]); final Drawable second = ContextCompat.getDrawable(context, drawables[secondDrawable]); final TransitionDrawable transitionDrawable = new TransitionDrawable(new Drawable[] { first, second }); target.setBackgroundDrawable(transitionDrawable); transitionDrawable.setCrossFadeEnabled(false); transitionDrawable.startTransition(duration); final int localSecondDrawable = secondDrawable; handler.postDelayed(new Runnable() { @Override public void run() { animate(localSecondDrawable, localSecondDrawable + 1, randInt(MIN, MAX)); } }, duration); } public void start() { final int duration = randInt(MIN, MAX); animate(0, 1, duration); } public void stop() { handler.removeCallbacksAndMessages(null); } private int randInt(int min, int max) { return random.nextInt((max - min) + 1) + min; } } 

用法:

 public class MainActivity extends AppCompatActivity { private GradientBackgroundPainter gradientBackgroundPainter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View backgroundImage = findViewById(R.id.root_view); final int[] drawables = new int[3]; drawables[0] = R.drawable.gradient_1; drawables[1] = R.drawable.gradient_2; drawables[2] = R.drawable.gradient_3; gradientBackgroundPainter = new GradientBackgroundPainter(backgroundImage, drawables); gradientBackgroundPainter.start(); } @Override protected void onDestroy() { super.onDestroy(); gradientBackgroundPainter.stop(); } } 

您可以查看Property Animation Api

  int colorFrom = getResources().getColor(R.color.red); int colorTo = getResources().getColor(R.color.blue); ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); colorAnimation.setDuration(250); // milliseconds colorAnimation.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { textView.setBackgroundColor((int) animator.getAnimatedValue()); } }); colorAnimation.start(); 

使用Relative layout ,为您的Relative layout提供一个id ,向下转换它,然后进入条件:

 if(something){ rlayout.setBackground(getResource().getDrawable().R.drawable.img); } or use switch 

创建一个TransitionDrawable以在用于背景的两个drawable之间进行更改。

      

在Android中过渡Drawables

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

我认为这个小代码可能会对你有所帮助。