gpt4 book ai didi

android - 我想在循环中使用具有平滑过渡效果的随机颜色作为背景

转载 作者:行者123 更新时间:2023-11-29 19:55:30 25 4
gpt4 key购买 nike

我的问题不是在背景中获取随机颜色,而是在背景中获取具有动画效果的 2 种颜色。当我重新启动应用程序时,2 种颜色会发生变化。我的目标是必须使用随机颜色作为背景,并且颜色变化动画流畅这是我的代码

public class MainActivity extends AppCompatActivity {
int color1,color2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout targetView = (RelativeLayout) findViewById(R.id.new2);

BackgroundPainter backgroundPainter = new BackgroundPainter();


color1=getRandColor();
color2=getRandColor();
backgroundPainter.animate(targetView, color1, color2);





// int color1 = ContextCompat.getColor(this, R.color.colorAccent);
//int color2 = ContextCompat.getColor(this, R.color.colorPrimary);


}

public int getRandColor(){
Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
int rando = Color.rgb(r, g, b);
return rando;
}

public class BackgroundPainter {

private static final int MIN = 1800;
private static final int MAX = 2300;

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

这是我的主要 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="weathercheck.bt4u.com.myapplication.MainActivity"
android:id="@+id/screen"
android:orientation="horizontal">


</LinearLayout>

最佳答案

改变

animate(target, color2, color1);

animate(target, color2, getRandColor());

关于android - 我想在循环中使用具有平滑过渡效果的随机颜色作为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com