在android中移动屏幕上的imagebutton

在android中移动屏幕上的imagebutton,android,android-layout,imagebutton,Android,Android Layout,Imagebutton,我在安卓系统中遇到了一个问题,首先我想有一个按钮,每隔2秒我就会将它移动到屏幕上的不同位置,但我不能这样做(如果有人知道这会有多大帮助的话)。 不管怎样,我的另一种方法是在不同的位置上制作5个不同的按钮,然后使用StVistIbIILY()函数移动,但是它在中间崩溃了,我不知道为什么,下面是代码: final ImageButton[] face = new ImageButton[5]; face[0] = (ImageButton) findViewById(R.id.ImageB

我在安卓系统中遇到了一个问题,首先我想有一个按钮,每隔2秒我就会将它移动到屏幕上的不同位置,但我不能这样做(如果有人知道这会有多大帮助的话)。 不管怎样,我的另一种方法是在不同的位置上制作5个不同的按钮,然后使用StVistIbIILY()函数移动,但是它在中间崩溃了,我不知道为什么,下面是代码:

final ImageButton[] face = new ImageButton[5];

    face[0] = (ImageButton) findViewById(R.id.ImageButton1);
    face[1] = (ImageButton) findViewById(R.id.ImageButton2);
    face[2] = (ImageButton) findViewById(R.id.ImageButton3);
    face[3] = (ImageButton) findViewById(R.id.ImageButton4);
    face[4] = (ImageButton) findViewById(R.id.ImageButton5);

    for(int i=0;i<5;i++)
    {
        face[i].setVisibility(View.GONE);
    }



    Thread timer=new Thread() {
        public void run(){
            for(int i=0;true;i++)
            {
                if(i==5)
                {
                    i=0;
                }
                Log.v("VISIBLE AT I = ",Integer.toString(i));
                face[i].setVisibility(View.VISIBLE);
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    Log.v("CATCH","CATCH");
                    e.printStackTrace();
                }
                //Log.v("SLEPT","SLEPT");
                face[i].setVisibility(View.INVISIBLE);  // IT CRASHES HERE
                Log.v("INVISIBLE AT I = ",Integer.toString(i));
            }
        }
    };

    timer.start();
final ImageButton[]面=新的ImageButton[5];
面[0]=(ImageButton)findViewById(R.id.ImageButton1);
面[1]=(ImageButton)findViewById(R.id.ImageButton2);
面[2]=(ImageButton)findViewById(R.id.ImageButton3);
面[3]=(ImageButton)findViewById(R.id.ImageButton4);
face[4]=(ImageButton)findViewById(R.id.ImageButton5);

对于(int i=0;i你想过用动画来做这件事吗?如果你的目标是蜂巢,你可以使用;对于早期的平台,你可以使用

对于崩溃,您只能从UI线程修改UI元素,而不能从单独的线程修改UI元素。您需要回发信号,或者使用一个(非常好的)帮助程序来处理此问题,例如AsyncTask


请参阅:

Lorne是对的,崩溃是因为您试图使用后台线程中的UI元素。您可以使用handler或AsyncTask,也可以将线程中的所有内容都放在主线程中进行处理。@Lorne,我还是android的初学者,我还不确定如何使用动画,如果有关于动画的教程的话r虚拟人在线(特别是视频)在android中实现动画其实很容易…这个框架非常强大,为你做了很多事情。蜂巢中基于属性的动画更加优雅和强大,但旧的蜂巢前的方法并不难。网上有很多动画教程…我在谷歌找到了这本,它看起来已经退出了e初学者友好型:这真是太棒了,洛恩,谢谢,但这有点限制了我,还有一些更具体的问题我不明白,我不想成为一个负担,但是没有更多的教程可以让我理解a到Z的全部内容吗?顺便说一句,谢谢:)而且,我不知道如何用动画移动按钮,从教程中,动画没有移动文本,因为它在布局中的相同位置结束