生成一个圆,随机在屏幕上,并使其绿色或红色 [英] Generate a circle randomly on the screen and make it green or red

查看:181
本文介绍了生成一个圆,随机在屏幕上,并使其绿色或红色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直都想做一个游戏的应用程序,无论是显示一个红色按钮与文字或一个绿色按钮与随机对Android屏幕上的文字。如果任何人都可以帮我这个我就要AP preciate它。也在一个侧面说明,我想慢慢的生成速度更快的酷颠倒,如果有人知道如何做到这一点。谢谢!

  @燮pressLint(DrawAllocation)
@覆盖
保护无效的OnDraw(帆布油画){

    字符串str =每日笑话;
    super.onDraw(画布);
    油漆=新的油漆();
    随机随机=新的随机();
    随机randomTwo =新的随机();

    //矩形ourRect =新的矩形();
    RECT至preCT =新的矩形();
    矩形背景=新的矩形();

    paint.setColor(Color.BLACK);
    backGround.set(0,0,canvas.getWidth(),canvas.getHeight());
    canvas.drawRect(背景,漆);
    的for(int i = 0; I< = 900;我++){

    }

    如果(空白==时间){
        paint.setColor(Color.RED);
        canvas.drawCircle(随机,randomTwo,230,油漆);
    }其他 {
        paint.setColor(Color.GREEN);
        canvas.drawCircle(随机,randomTwo,230,油漆);
    }
}
 

解决方案

您只需要一个随机实例。

声明专用长LASTUPDATED = 0; 私人INT lastColor = Color.BLACK; 中的OnDraw之外。

更新的底部到:

 最终浮动半径= 230F;
如果(System.currentTimeMillis的()> LASTUPDATED + 1000){
    lastColor = random.nextInt(2)== 1? Color.RED:Color.GREEN;
    = LASTUPDATED的System.currentTimeMillis();
}
paint.setColor(lastColor);
canvas.drawCircle(random.nextInt(canvas.getWidth() - 半径/ 2)+半径/ 2F,random.nextInt(canvas.getHeight() - 半径/ 2)+半径/ 2F,半径,油漆);
 

这将绘制的红色或绿色圆圈随机位置,每秒一次。

您需要半径/ 2,因为坐标是从圆的中心。

至于你的问题,你的第二部分的也在一个侧面说明,我想慢慢的生成速度更快的酷上攻的。你得弄清楚你是什么意思。

编辑: 这里提供一个更完整的(正确)示例: https://gist.github.com/mshi/8287fd3956c9a917440d

So ive been trying to make a game app that either displays a red button with text or a green button with text randomly on the android screen. If anyone can help me with this i would appreciate it. also on a side note i want to slowly generate faster cool upside if anyone knows how to do that. Thanks!

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas){

    String str = "Joke of the day";
    super.onDraw(canvas);
    paint = new Paint();
    Random random = new Random();
    Random randomTwo = new Random();

    //Rect ourRect = new Rect();
    Rect topRect = new Rect();
    Rect backGround = new Rect();

    paint.setColor(Color.BLACK);
    backGround.set(0,0,canvas.getWidth(),canvas.getHeight());
    canvas.drawRect(backGround, paint);
    for(int i = 0; i <= 900; i++;){

    }

    if(blank == time){
        paint.setColor(Color.RED);
        canvas.drawCircle(random, randomTwo, 230, paint);
    }else {
        paint.setColor(Color.GREEN);
        canvas.drawCircle(random, randomTwo, 230, paint);
    }
}

解决方案

You only need one Random instance.

Declare private long lastUpdated = 0; and private int lastColor = Color.BLACK; outside of the onDraw.

Update the bottom portion to:

final float radius = 230f;
if(System.currentTimeMillis() > lastUpdated + 1000){
    lastColor = random.nextInt(2) == 1 ? Color.RED : Color.GREEN;
    lastUpdated = System.currentTimeMillis();
}
paint.setColor(lastColor);
canvas.drawCircle(random.nextInt(canvas.getWidth()-radius/2) + radius/2f, random.nextInt(canvas.getHeight()-radius/2) + radius/2f, radius, paint);

This will draw a circle of either red or green at random location every second.

You need the radius/2 because the coordinates are from the center of the circle.

As for your second part of your question, also on a side note i want to slowly generate faster cool upside. You'd have to clarify what you mean.

Edit: Provided a more complete (and correct) sample here: https://gist.github.com/mshi/8287fd3956c9a917440d

这篇关于生成一个圆,随机在屏幕上,并使其绿色或红色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆