2014-10-02 100 views
1

我无法让我的清除按钮在我的程序中工作。我的圆形按钮的作品显示随机的圆形,但我清晰的按钮什么都不做。我迷路了。我知道我应该使用drawColor.Color.TRANSPARENT或mode.clear,但没有任何工作。创建一个清除按钮清除画布android

package com.example.randomcircles; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.graphics.*; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.*; 
import android.content.*; 

public class DisplayRandomCircles extends Activity 
{ 
FrameLayout f1, f2; 
@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); 
    DrawCircle c = new DrawCircle(getApplicationContext()); 
    Canvas d = new Canvas(); 
    Paint p = new Paint(); 
    switch (v.getId()) 
    { 
     case (R.id.btn1): 
      f1.addView(c); 
      break; 

     case (R.id.btn2): 
      d.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
      break; 
    } 
} 
} 

package com.example.randomcircles; 
import java.util.Random; 
import android.annotation.SuppressLint; 
import android.content.Context; 
import android.graphics.*; 
import android.view.*; 

public class DrawCircle extends View 
{ 
Random random = new Random(); 
int red = (int) (Math.random() * 255); 
int green = (int) (Math.random() * 255); 
int blue = (int) (Math.random() * 255); 
int randX = random.nextInt(700); 
int randY = random.nextInt(1000); 
int randR = random.nextInt(200); 
int color = Color.rgb(red, green, blue); 

public DrawCircle(Context con) 
{ 
    super(con); 
} 
@SuppressLint("DrawAllocation") 
@Override 
public void onDraw(Canvas c) 
{ 
    super.onDraw(c); 
    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(randX, randY, randR, p); 

} 

} 

<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> 

回答

0

您正在创建画圆的新实例每次doIt被调用。

旧的实例仍然存在。因此,您需要从f1中删除旧视图,或者获取与之前创建的完全相同的DrawCircle实例,设置新颜色,然后在DrawCircle上调用invalidate()以强制重绘。