如何在Android中进行循环?

时间:2014-02-23 19:18:14

标签: android loops asynchronous

所以,我正在做一个应用程序而且我遇到了问题。我想要的是当在图像按钮上单击一下这个图像按钮将在屏幕上随机移动,直到你关闭de app。所以,我必须使用AsyncTask,因为我修改了一个View,但我不知道如何在AsyncTask类中进行无限循环。我知道我必须使用onPostExecute来改变View位置,但循环怎么样?该应用程序是我想和我的朋友做的一个笑话。

这是主要的活动:

package br.hue.brfernandobr;

import java.util.Random;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;

@SuppressLint("NewApi")
public class BrFernandoBr extends Activity 
{

    public ImageButton imageButtonBrFernando;
    RelativeLayout layout;

    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_br_fernando_br);

        layout = (RelativeLayout) findViewById( R.id.layout );

        setVolumeControlStream( AudioManager.STREAM_MUSIC );

        player = MediaPlayer.create( this, R.raw.brbrhuehue );

        imageButtonBrFernando = (ImageButton) findViewById( R.id.imageViewBrFernando );
        addListenerImageButtonBrFernando();

    }

    private class GameTask extends AsyncTask<Void, Void, Void>
    {

        @Override
        protected Void doInBackground(Void... params) 
        {

            return null;

        }

        @Override
        protected void onPostExecute( Void result )
        {

            moveImage();
            changeImageColors();
            playSong();

        }

    }

    private void addListenerImageButtonBrFernando() 
    {

        imageButtonBrFernando.setOnClickListener( new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) 
            {

                new GameTask().execute();

            }

        });

    }

    private void moveImage()
    {

        Random random = new Random();

        int x = random.nextInt( layout.getWidth() - imageButtonBrFernando.getWidth() / 2 );
        int y = random.nextInt( layout.getHeight() - imageButtonBrFernando.getHeight() / 2 );

        imageButtonBrFernando.setX( x );
        imageButtonBrFernando.setY( y );

    }

    private void changeImageColors()
    {

        Random random = new Random();

        int numColor = random.nextInt( 5 );

        switch ( numColor ) {
        case 0:
            imageButtonBrFernando.getBackground().setColorFilter( Color.RED, PorterDuff.Mode.MULTIPLY );
            layout.setBackgroundColor( Color.RED );
            break;
        case 1:
            imageButtonBrFernando.getBackground().setColorFilter( Color.BLUE, PorterDuff.Mode.MULTIPLY );
            layout.setBackgroundColor( Color.BLUE );
            break;
        case 2:
            imageButtonBrFernando.getBackground().setColorFilter( Color.GREEN, PorterDuff.Mode.MULTIPLY );
            layout.setBackgroundColor( Color.GREEN );
            break;
        case 3:
            imageButtonBrFernando.getBackground().setColorFilter( Color.YELLOW, PorterDuff.Mode.MULTIPLY );
            layout.setBackgroundColor( Color.YELLOW );
            break;
        case 4:
            imageButtonBrFernando.getBackground().setColorFilter( Color.MAGENTA, PorterDuff.Mode.MULTIPLY );
            layout.setBackgroundColor( Color.MAGENTA );
            break;
        }

    }

    private void playSong()
    {

        if( !player.isPlaying() )
        {

            player.start();

        }

    }

}

3 个答案:

答案 0 :(得分:2)

如果我理解您要正确执行的操作,则不需要ASync任务,只需更改图像按钮的onClick处理程序中的位置即可。这样就是事件驱动。

如果您希望按钮在第一次单击后继续移动,只需使用具有短暂超时的计时器来移动按钮 您可以按如下方式使用runOnUiThread:

首先,这是启动流程的onClick监听器

@Override
    public void onClick(View v) {
        timer.schedule(new MyTimerTask(), 500,500);
    }

然后是你的计时器任务

    private class MyTimerTask extends TimerTask{

    @Override
    public void run() {        
    runOnUiThread(new Runnable() {              
        @Override
        public void run() {
                // move your button here
            }
        });
    }       
}

答案 1 :(得分:1)

你可以这样做。

public void doStuff(){
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            handler.post(new Runnable() {

                @Override
                public void run() {
                    // you're in the main thread here
                }
            });
        }
    }, 500, 500);

}

Handler handler = new Handler();

答案 2 :(得分:0)

解决方案是使用计时器。

T = new Timer();
        T.scheduleAtFixedRate(new TimerTask() {         
                @Override
                public void run() {
                    runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {

                            playSong();
                            moveImage();
                            changeImageColors();

                        }
                    });
                }
            }, 1000, 1000);

我不得不使用RunUiThread方法,因此我可以更改视图。