Android 上的多种形状

发布于 2024-08-25 15:31:32 字数 882 浏览 6 评论 0原文

你好 我正在尝试构建一个布局,其中每隔 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2024-09-01 15:31:32

您无法在单独的线程中操作屏幕。您应该使用处理程序,因为它是在 UI 线程上调用的。

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

    ShapeHandler handler = new ShapeHandler();
    handler.sendEmptyMessage(0);
}

private class ShapeHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        View h = new CustomDrawableView(c,
            (int)Math.round(Math.random()*100),
            (int)Math.round(Math.random()*100));
        l.addView(h);
        this.sendEmptyMessageDelayed(0, 2000);
    }
}

You can't manipulate the screen in a separate thread. You should use a handler since that gets called on the UI thread.

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

    ShapeHandler handler = new ShapeHandler();
    handler.sendEmptyMessage(0);
}

private class ShapeHandler extends Handler
{
    @Override
    public void handleMessage(Message msg)
    {
        View h = new CustomDrawableView(c,
            (int)Math.round(Math.random()*100),
            (int)Math.round(Math.random()*100));
        l.addView(h);
        this.sendEmptyMessageDelayed(0, 2000);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文