如何每2秒生成一次圆圈

时间:2017-08-26 15:16:18

标签: android multithreading android-studio canvas geometry

我想每2秒产生一次圆圈,在这种情况下为5但我无法得到它。应用程序等待10秒并将5个圆圈绘制在一起,而不是每2秒创建一个圆圈。我究竟做错了什么?谢谢。

public class Juego extends SurfaceView{

boolean isItOK = false;

Paint paint;
int CantidadDeEsferas = 5;
int radio, alto, ancho;

public Juego(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    paint = new Paint();
}

public void onDraw(Canvas canvas){

    paint.setColor(Color.WHITE);
    canvas.drawRect(0, 0, getWidth(), getHeight(), paint);

    paint.setColor(Color.BLACK);

    for (int i=0; i<CantidadDeEsferas; i++) {

        Random r = new Random();
        alto = r.nextInt(canvas.getHeight());
        ancho = r.nextInt(canvas.getWidth());
        radio = r.nextInt(101 - 50) + 50;
        canvas.drawCircle(ancho, alto, radio, paint);
        run();
        isItOK = true;
    }
}

public void run(){
    while (isItOK){
        try
        {
            Thread.sleep(2000);
            isItOK = false;
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    }
}

}

3 个答案:

答案 0 :(得分:0)

通过调用onDraw中的run,您可以暂停主线程。绘制不会出现,直到主线程返回到框架中的事件循环。你永远不应该在主线程上调用sleep或其他阻塞函数(基本上,你的应用程序似乎会冻结)。如果您想在2秒内执行某些操作,请创建一个处理程序并使用postDelayed()向其发送消息。然后让消息“runnable”增加视图设置为绘制的圆圈数,然后使视图无效并在接下来的2秒内发布另一条消息。然后视图应该在其onDraw中绘制自己,检查变量以获取要绘制的圆圈数。

答案 1 :(得分:0)

那是什么

for (int i=0; i<CantidadDeEsferas; i++) {

    Random r = new Random();
    alto = r.nextInt(canvas.getHeight());
    ancho = r.nextInt(canvas.getWidth());
    radio = r.nextInt(101 - 50) + 50;
    canvas.drawCircle(ancho, alto, radio, paint);
    run();
    while(isItOK);
    isItOk = true;
}

Run()方法

public void run(){
    try
    {
        Thread.sleep(2000);
        isItOK = false;
    } catch (InterruptedException e) {

        e.printStackTrace();
    }   
 }

但我不喜欢你用过的方式

注意:此类必须由另一个线程运行。(在后台)

答案 2 :(得分:0)

public class MyView extends View {

private Paint paint;
private List<Point> points;
private Handler handler;
private Random random;


public MyView(Context context) {
    super(context);
    init();
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    paint = new Paint();
    paint.setColor(Color.BLACK);
    handler = new Handler();
    points = new ArrayList<>();
    random = new Random();
    drawCircles();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    for (Point point : points) {
        canvas.drawCircle(point.x, point.y, 10, paint);
    }
}

private void drawCircles() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                Point point = new Point();
                point.set(random.nextInt(getHeight()), random.nextInt(getWidth()));
                points.add(point);
            }
            invalidate();
            handler.postDelayed(this, 2000);
        }
    }, 2000);
}}
相关问题