重新初始化Android中ImageButton的位置

时间:2013-08-22 13:47:48

标签: android imagebutton

在这个问题中我想经常改变ImageButton的位置。第一次初始化后,ImageButton不会改变其位置。 这是代码......

runOnUiThread(new Runnable() {
        public void run()
        {
            for(int i=0;i<60;i++)
            {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
                img.refreshDrawableState();

                Random r=new Random();
                int a=r.nextInt();
                int b=r.nextInt();

                if(a<0)
                    a=a*(-1);
                if(b<0)
                    b=b*(-1);

                w=(a%(width-50))-72;
                h=(b%(height-50))-30;



                img.refreshDrawableState();
                img.setLeft(w);
                img.setTop(h);

                Toast.makeText(getApplicationContext(), "width : "+w+"\theight : "+h, Toast.LENGTH_SHORT).show();
            }
        }
    });

变量高度&amp;宽度是高度&amp;屏幕宽度

1 个答案:

答案 0 :(得分:0)

您应该使用绝对布局并设置布局参数。当我需要做类似的事情时,我发现这个question/answer很有用。

相关问题