2014-10-02 148 views
2

我在這個任務上取得了很多進展。每次點擊一個按鈕(圓形)後,最終產品需要在隨機顏色/隨機大小/隨機位置添加一個圓圈。我有另一個按鈕(清除),需要清除畫布。這是我現在面臨的兩個問題。我的圈子沒有顯示在隨機位置。他們都從屏幕的左上角開始。第二個問題是我不知道如何讓我清晰的按鈕工作。其他一切都在工作,隨機圓圈大小的隨機圓圈顏色。 (因爲你有高分辨率的設備最有可能的),你必須讓與和視圖的高度來繪製你的圈子就可以隨機在隨機地圖中隨機繪製隨機圈子Android

package com.example.randomcircles; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.FrameLayout.LayoutParams; 

public class DisplayRandomCircles extends Activity 
{ 
FrameLayout f1; 
@Override 
public void onCreate(Bundle b) 
{ 
    super.onCreate(b); 
    setContentView(R.layout.activity_display_random_circles); 
    Button btn1 = (Button) findViewById(R.id.btn1); 
    Button btn2 = (Button) findViewById(R.id.btn2); 
    f1 = (FrameLayout) findViewById(R.id.frame); 


} 
@SuppressLint("WrongCall") 
public void doit(View v) 
{ 

    int rand = (int) (Math.random() * 60); 
    switch (v.getId()) 
    { 
     case (R.id.btn1): 
      DrawCircle c = new DrawCircle(getApplicationContext()); 
      f1.addView(c); 
      break; 

     case (R.id.btn2): 
      DrawCircle d = new DrawCircle(getApplicationContext()); 
      f1.addView(d); 
      break; 
    } 
} 
} 

package com.example.randomcircles; 

import java.util.Random; 

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 

public class DrawCircle extends View 
{ 
int red = (int) (Math.random() * 255); 
int green = (int) (Math.random() * 255); 
int blue = (int) (Math.random() * 255); 
public DrawCircle(Context con) 
{ 
    super(con); 
} 
@SuppressLint("DrawAllocation") 
@Override 
public void onDraw(Canvas c) 
{ 
    super.onDraw(c); 
    int color = Color.rgb(red, green, blue); 
    int rand = (int)(Math.random() * 200); 
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 
    p.setAntiAlias(true); 
    p.setStyle(Paint.Style.STROKE); 
    p.setStrokeWidth(100); 
    p.setColor(color); 
    p.setStyle(Paint.Style.FILL); 
    c.drawCircle(rand, rand, rand, p); 
    clear(c); 

} 
public void clear(Canvas c) 
{ 
    c.drawColor(Color.TRANSPARENT); 
} 
public void drawColor(int transparent) { 
    // TODO Auto-generated method stub 

} 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<FrameLayout 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight=".75" 
    android:orientation="vertical" > 


</FrameLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight=".25" 
    android:gravity="bottom|center" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start|bottom" 
     android:layout_weight=".50" 
     android:onClick="doit" 
     android:text="@string/Circle" /> 

    <Button 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight=".50" 
     android:layout_gravity="end|bottom" 
     android:onClick="doit" 
     android:text="@string/Clear" /> 
</LinearLayout> 

</LinearLayout> 

回答

2

你的隨機函數不能有效地借鑑了當前視圖圈。

int minRadius = 100; 
    Random random = new Random();//define this outside you onDraw fucntion 
    int w = getWidth(); 
    int h = getHeight(); 

    int randX = random.nextInt(w); 
    int randY = random.nextInt(h); 
    int randR = minRadius + random.nextInt(100); 
    ... 
    c.drawCircle(randX, randY, randR, p); 

也重置您對您繪製顏色整個畫布

canvas.drawColor(Color.WHITLE); 
+0

我需要一個對象設置爲的getWidth()方法查看。我也可以在我的清除按鈕中設置drawColor.transparent? – user3242607 2014-10-02 21:10:32

+0

我只是不能讓按鈕來清除畫布工作? – user3242607 2014-10-02 23:38:53

+0

我添加了另一個名爲繪製視圖和擴展視圖的類,比如我的drawcircle類,並且覆蓋了繪製一個大的白色圓圈的ondraw方法,就像清理畫布一樣。爲我的清除按鈕添加了另一個addview到該類。這對我有效 – user3242607 2014-10-03 00:29:41