gpt4 book ai didi

android - 以固定的时间间隔更改背景

转载 作者:行者123 更新时间:2023-11-29 17:58:45 28 4
gpt4 key购买 nike

我正在尝试以固定的时间间隔更改 Activity 的背景。这是我的 Activity 代码

public class ExploreActivity extends Activity {

private int randNum = 0 ;
private int [] colorID = {R.color.AliceBlue,R.color.Beige,R.color.YellowGreen};
private Random rand = new Random();



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explore);


final Handler handler = new Handler(getMainLooper(), new Callback() {

@Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
getWindow().setBackgroundDrawableResource(colorID[msg.arg1]);
return true;
}
});

final Message msg = new Message();


Timer timer = new Timer();
timer.schedule(new TimerTask() {


@Override
public void run() {
// TODO Auto-generated method stub

randNum = rand.nextInt(colorID.length);
msg.arg1 = randNum;
handler.sendMessageAtFrontOfQueue(msg);



}
}, 5000, 5000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.explore, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_logout:{
SharedPreferences settings = getSharedPreferences("settings", MODE_PRIVATE);
Editor editor = settings.edit();
editor.putBoolean("login_state", false);
editor.commit();
finish();

}
default:
return super.onOptionsItemSelected(item);

}


}





}

我收到这个错误,颜色变化只发生一次然后这个错误就来了

 07-01 08:03:28.364: E/AndroidRuntime(3104): FATAL EXCEPTION: main
07-01 08:03:28.364: E/AndroidRuntime(3104): java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.MessageQueue.removeSyncBarrier(MessageQueue.java:266)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Looper.removeSyncBarrier(Looper.java:242)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:981)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer.doFrame(Choreographer.java:532)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Handler.handleCallback(Handler.java:725)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.os.Looper.loop(Looper.java:137)
07-01 08:03:28.364: E/AndroidRuntime(3104): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-01 08:03:28.364: E/AndroidRuntime(3104): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 08:03:28.364: E/AndroidRuntime(3104): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 08:03:28.364: E/AndroidRuntime(3104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-01 08:03:28.364: E/AndroidRuntime(3104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-01 08:03:28.364: E/AndroidRuntime(3104): at dalvik.system.NativeStart.main(Native Method)

请帮助我以适当的方式做到这一点

最佳答案

您可以使用 AnimationDrawable 轻松实现您的目标.只需在 AnimationDrawable xml 文件中创建一个颜色列表。并在您的 onCreate 方法中,将可绘制对象附加到背景并在屏幕获得焦点时启动动画。

示例 darwable:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@color/red" android:duration="600" />
<item android:drawable="@color/blue" android:duration="600" />

</animation-list>

对于 Activity :

    AnimationDrawable drawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View parent = findViewById(R.id.parent);
drawable = (AnimationDrawable) getResources().getDrawable(R.drawable.animation_drawable);
parent.setBackgroundDrawable(drawable);
}
@Override
public void onWindowFocusChanged(boolean gained){
super.onWindowFocusChanged(gained);
if(gained){
drawable.start();
}
}

关于android - 以固定的时间间隔更改背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17399988/

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