2017-12-12 14 views
0

私はそのキャンバスが画面の外に円を描いていないことをどのように設定することができますか?スクリーンショットでは、それはこのようになります円の外に出ている画面のサイズ

- あなたが見ることができるように Click here to see image

、円の一部が画面の外に半分であるが、私は、円のすべてが画面内にあることを望みます。

@Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Random random = new Random(); 
     int minRadius = 50; 
     int w = this.getWidth(); 
     int h = this.getHeight(); 
     Paint paint = new Paint(); 

     for (int i=0; i<resultInt; i++) { 
      int red = (int) (Math.random() * 255); 
      int green = (int) (Math.random() * 255); 
      int blue = (int) (Math.random() * 255); 
      int randX = random.nextInt(w); 
      int randY = random.nextInt(h); 
      int color = Color.rgb(red, green, blue); 
      paint.setColor(color); 
      canvas.drawCircle(randX, randY, minRadius, paint); 
     } 
    } 
} 
} 
+0

int w = this.getWidth() - 2 * minRadius; int h = this.getHeight() - 2 * minRadius; 

そしてがランダム点を修正?いいえ – mac229

+0

@ mac229編集、追加コード、申し訳ありません – Mee

答えて

0

あなたは二重の半径を減算する必要があります:あなたのコードがなければ

int randX = random.nextInt(w) + minRadius; 
int randY = random.nextInt(h) + minRadius; 
+0

あなたの答えは私のものと一致するように編集するので、最初に投稿されたようですか?私はそのトリックを覚えておく必要があります。 –

+0

男性はいません。私は間違いに気づいた。投稿の編集が終わったら、あなたの答えは同じでした。あなたの答えは正しいとマークすることができます。私は気にしない – mac229

0
int w = this.getWidth() - minRadius * 2; 
int h = this.getHeight() - minRadius * 2; 
... 
int randX = random.nextInt(w) + minRadius; 
int randY = random.nextInt(h) + minRadius; 
関連する問題