2014-10-02 3 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

+0

캔버스를 지우려면 버튼을 사용할 수 없습니까? – user3242607

+0

drawcircle 클래스와 같은 드로잉 뷰 및 확장 뷰라는 또 다른 클래스를 추가하고 캔버스를 지우는 것처럼 큰 흰색 원을 그린 ondraw 메서드를 재정의했습니다. 내 클리어 버튼을 위해 그 클래스에 또 다른 addview를 추가했습니다. 그게 나를 위해 일했다. – user3242607