gpt4 book ai didi

java - 通过动画过渡动态更改背景颜色

转载 作者:行者123 更新时间:2023-12-01 06:53:33 26 4
gpt4 key购买 nike

我正在尝试以 3 秒的速度生成随机颜色并将其设置为背景。我创建了一个线程来处理此更改,现在我想在颜色更改之间添加过渡以使其很好地混合。

作为引用,请查看 this app .

编辑:我尝试过在一个具有 3 秒过渡周期的循环中使用 ObjectAnimatorArgbEvaluator,但是屏幕一直以类似频闪的方式闪烁,这只会给您一个头痛。除此之外,颜色变化得很好,其他一切都很完美。有人可以运行这个并看看可能出了什么问题吗?

public class Main extends Activity {

public int color1, color2, red1, red2, blue1, blue2, green1, green2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);


new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(3000); // I've also tried 1000 and 4000, same issue.
} catch (InterruptedException e) {
e.printStackTrace();
}
Main.this.runOnUiThread(new Runnable() {
public void run() {

//generate color 1
red1 = (int)(Math.random() * 128 + 127);
green1 = (int)(Math.random() * 128 + 127);
blue1 = (int)(Math.random() * 128 + 127);
color1 = 0xff << 24 | (red1 << 16) |
(green1 << 8) | blue1;


//generate color 2

red2 = (int)(Math.random() * 128 + 127);
green2 = (int)(Math.random() * 128 + 127);
blue2 = (int)(Math.random() * 128 + 127);
color2 = 0xff << 24 | (red2 << 16) |
(green2 << 8) | blue2;

//start animation
View v = findViewById(R.id.view);
ObjectAnimator anim = ObjectAnimator.ofInt(v, "backgroundColor", color1, color2);


anim.setEvaluator(new ArgbEvaluator());
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(3000);
anim.start();

}
});
}
}
}.start();
}

}

编辑:我缩小了范围,发现“.setRepeatMode”导致了问题。我仍然没有解决办法。通过将“反向”更改为其他内容(无限或其他提供的选项),可以防止动画发生。知道我能做些什么来解决这个问题吗?

另外,有人知道更好的方法来生成更鲜艳的颜色吗?我研究的一切都已经过时了。

最佳答案

除了一件事之外,您所做的一切都是正确的:每 3 秒,您就会随机生成 2 种颜色。所以,这就是正在发生的事情:

1st iteration:

color1 is generated

color2 is generated

View's background is set to color1. And then the background changes from color1 to color2.

// All's good

2nd iteration:

There's a new color1

There's a new color2

View's background is set to new color1. Immediate change causes the strobe light effect. And then the background changes from new color1 to new color2.

您应该采取什么措施来解决此问题:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

// Generate color1 before starting the thread
red1 = (int)(Math.random() * 128 + 127);
green1 = (int)(Math.random() * 128 + 127);
blue1 = (int)(Math.random() * 128 + 127);
color1 = 0xff << 24 | (red1 << 16) |
(green1 << 8) | blue1;


new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Main.this.runOnUiThread(new Runnable() {
public void run() {

//generate color 2

red2 = (int)(Math.random() * 128 + 127);
green2 = (int)(Math.random() * 128 + 127);
blue2 = (int)(Math.random() * 128 + 127);
color2 = 0xff << 24 | (red2 << 16) |
(green2 << 8) | blue2;

//start animation
View v = findViewById(R.id.view);
ObjectAnimator anim = ObjectAnimator.ofInt(v, "backgroundColor", color1, color2);


anim.setEvaluator(new ArgbEvaluator());
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setDuration(3000);
anim.start();

// Now set color1 to color2
// This way, the background will go from
// the previous color to the next color
// smoothly
color1 = color2;

}
});
}
}
}.start();
}

因此,从第二次迭代开始,起始颜色应与上一次迭代的结束颜色相同。仅初始化/生成 color1 一次:在启动线程之前。在 anim.start() 之后,添加:

color1 = color2;

另外,请注意,您将每 3 秒创建一个新的 ObjectAnimator:

ObjectAnimator anim = ObjectAnimator.ofInt(v, "backgroundColor", color1, color2);

因此,以下语句无效:

anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);

这是我的建议:

public class Main extends Activity {

public int color1, color2, red1, red2, blue1, blue2, green1, green2;

View v;

ObjectAnimator anim;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

// White or whatever color background R.id.view
// has at the beginning
color1 = 0xffffffff;

v = findViewById(R.id.llMain);

// We haven't initialized color2 yet. Will set this later
anim = ObjectAnimator.ofInt(v, "backgroundColor", color1);

anim.setEvaluator(new ArgbEvaluator());

anim.setDuration(3000);


new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Main.this.runOnUiThread(new Runnable() {
public void run() {

//generate color 2

red2 = (int)(Math.random() * 128 + 127);
green2 = (int)(Math.random() * 128 + 127);
blue2 = (int)(Math.random() * 128 + 127);
color2 = 0xff << 24 | (red2 << 16) |
(green2 << 8) | blue2;

// Update the color values
anim.setIntValues(color1, color2);

anim.start();

// Order the colors
color1 = color2;

}
});
}
}
}.start();
}
}

这样,您就创建了一次 ObjectAnimator 对象,并每 3 秒更新一次颜色值。

关于java - 通过动画过渡动态更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18818611/

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