简体   繁体   English

创建一个清除按钮以清除Android画布

[英]Create a clear button to clear canvas android

I can't get my clear button to work in my program. 我无法在程序中使用清除按钮。 My circle button works which displays random circles but my clear button does nothing. 我的圈子按钮可以显示随机的圈子,但我的清除按钮却不起作用。 I'm lost. 我迷路了。 I know i'm supposed to use drawColor.Color.TRANSPARENT or the mode.clear but nothing is working. 我知道我应该使用drawColor.Color.TRANSPARENT或mode.clear,但没有任何作用。

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>

You are creating a new instance of DrawCircle every time doIt is called. 每次调用doIt您都在创建DrawCircle的新实例。

The old instance is still there. 旧实例仍然存在。 So you need to either remove the old view from f1, or get the exact same instance of DrawCircle you created before, set the new color, and then call invalidate() on the DrawCircle to force the redraw. 因此,您需要从f1中删除旧视图,或者获取与之前创建的DrawCircle完全相同的实例,设置新颜色,然后在DrawCircle上调用invalidate()以强制重绘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM