下面是我在处理程序中使用的代码,以创建单独的线程。 我有一个存储所有方块的列表,我只希望显示10个(用于测试目的)使用.size()来获取列表中的方块数。Android中的处理程序逻辑
这个想法是随机生成x和y值,然后.addSquare将它添加到列表中,但也画在我的画布上。
我的问题是,我将如何推迟每个单独的方形绘制的时间,以便所有10个不会同时出现?
编辑:由此,我的意思是,延迟显示每个人。
谢谢。
handler = new Handler();
Runnable runnable = new Runnable()
{
@Override
public void run() {
while (sModel.getSquares().size() < 10)
{
int width = 980;
int height = 1280;
// create random
Random rand = new Random();
// float values for storing random x, y coords
float randX, randY;
randX = rand.nextInt(width); // random x
randY = rand.nextInt(height); // random y
sModel.addSquare(randX, randY, Color.BLUE);
handler.postDelayed(this, 2000);
}
}
};handler.postDelayed(runnable,1000);
编辑: logcat中的要求:(错误从试图执行的wait()和notify())
01-15 07:22:47.811: W/asset(3951): Copying FileAsset 0x6a1eeb58 (zip:/data/app/ce0941a.coursework.Int.BoxesGame-1.apk:/resources.arsc) to buffer size 5184 to make it aligned.
01-15 07:22:47.901: I/Adreno-EGL(3951): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL3907963)
01-15 07:22:47.901: I/Adreno-EGL(3951): OpenGL ES Shader Compiler Version: 17.01.09
01-15 07:22:47.901: I/Adreno-EGL(3951): Build Date: 09/26/13 Thu
01-15 07:22:47.901: I/Adreno-EGL(3951): Local Branch:
01-15 07:22:47.901: I/Adreno-EGL(3951): Remote Branch:
01-15 07:22:47.901: I/Adreno-EGL(3951): Local Patches:
01-15 07:22:47.901: I/Adreno-EGL(3951): Reconstruct Branch:
01-15 07:22:47.991: D/qdmemalloc(3951): ion: Mapped buf base:0x6c932000 size:8355840 offset:0 fd:54
01-15 07:22:48.001: D/qdmemalloc(3951): ion: Mapped buf base:0x6c874000 size:4096 offset:0 fd:55
01-15 07:22:52.526: D/qdmemalloc(3951): ion: Mapped buf base:0x6d26d000 size:8355840 offset:0 fd:57
01-15 07:22:52.526: D/qdmemalloc(3951): ion: Mapped buf base:0x6c886000 size:4096 offset:0 fd:58
01-15 07:22:52.636: D/qdmemalloc(3951): ion: Mapped buf base:0x6da65000 size:8355840 offset:0 fd:59
01-15 07:22:52.636: D/qdmemalloc(3951): ion: Mapped buf base:0x6c887000 size:4096 offset:0 fd:60
01-15 07:22:52.636: W/dalvikvm(3951): threadid=1: thread exiting with uncaught exception (group=0x415d4970)
01-15 07:22:52.636: E/AndroidRuntime(3951): FATAL EXCEPTION: main
01-15 07:22:52.636: E/AndroidRuntime(3951): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{ce0941a.coursework.Int.BoxesGame/ce0941a.coursework.controller.GameActivity}: java.lang.InstantiationException: can't instantiate class ce0941a.coursework.controller.GameActivity; no empty constructor
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2435)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.os.Looper.loop(Looper.java:158)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.main(ActivityThread.java:5789)
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.reflect.Method.invoke(Method.java:525)
01-15 07:22:52.636: E/AndroidRuntime(3951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-15 07:22:52.636: E/AndroidRuntime(3951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
01-15 07:22:52.636: E/AndroidRuntime(3951): at dalvik.system.NativeStart.main(Native Method)
01-15 07:22:52.636: E/AndroidRuntime(3951): Caused by: java.lang.InstantiationException: can't instantiate class ce0941a.coursework.controller.GameActivity; no empty constructor
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.Class.newInstanceImpl(Native Method)
01-15 07:22:52.636: E/AndroidRuntime(3951): at java.lang.Class.newInstance(Class.java:1130)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
01-15 07:22:52.636: E/AndroidRuntime(3951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
01-15 07:22:52.636: E/AndroidRuntime(3951): ... 11 more
01-15 07:22:59.263: D/Process(3951): killProcess, pid=3951
01-15 07:22:59.263: D/Process(3951): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:123 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
你可以在当前线程中调用wait并在10秒后调用notifyAll –
@Karan_Rana我对Android编程非常陌生,请你举个例子吗?谢谢。 – RevXaisks
http://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example 此链接可能会清除您的主题概念 –