因此,我一直试图制作一个在Android屏幕上随机显示带有文本的红色按钮或带有文本的绿色按钮的游戏应用程序。如果有人可以帮助我,我将不胜感激。另外,如果有人知道该怎么做,我想慢慢产生更快的降温空间。谢谢!
@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); } }
您只需要一个Random实例。
Random
声明private long lastUpdated = 0;和private int lastColor = Color.BLACK;对的onDraw之外。
private long lastUpdated = 0;
private int lastColor = Color.BLACK;
将底部更新为:
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);
这将每秒在随机位置绘制一个红色或绿色的圆圈。
您需要半径为2,因为坐标是从圆心开始的。
至于您的问题的第二部分, 我也想在旁注中慢慢产生更快的冷静 。您必须澄清您的意思。
编辑:在此处提供了更完整(更正确)的示例:https : //gist.github.com/mshi/8287fd3956c9a917440d