Android 上的多种形状
你好 我正在尝试构建一个布局,其中每隔 2 就会弹出一些形状 秒。如果用户单击这些形状之一,他们必须 消失。
这样做的正确方法是什么?我想过一个线程,但我 错过了。 这是我目前的代码(不起作用):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
l = new LinearLayout(this);
setContentView(l);
int counter = 1;
View v = new CustomDrawableView(this,20,50);
l.addView(v);
Thread t = new Thread() {
public void run() {
while (true) {
Log.i("THREAD","INSIDE");
View h = new CustomDrawableView(c,
(int)Math.round(Math.random()*100),
(int)Math.round(Math.random()*100));
SystemClock.sleep(2000);
l.addView(h);
}
}
};
t.start();
}
Hi
I'm trying to build a layout where some shapes will popup every 2
seconds. If the user will click one of these shapes, they have to
disappear.
What is the correct way of doing this? I thought about a thread, but i
missed out.
Here's my code at the moment (is not working):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
l = new LinearLayout(this);
setContentView(l);
int counter = 1;
View v = new CustomDrawableView(this,20,50);
l.addView(v);
Thread t = new Thread() {
public void run() {
while (true) {
Log.i("THREAD","INSIDE");
View h = new CustomDrawableView(c,
(int)Math.round(Math.random()*100),
(int)Math.round(Math.random()*100));
SystemClock.sleep(2000);
l.addView(h);
}
}
};
t.start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在单独的线程中操作屏幕。您应该使用处理程序,因为它是在 UI 线程上调用的。
You can't manipulate the screen in a separate thread. You should use a handler since that gets called on the UI thread.