java - 创建一个清除按钮来清除 Canvas android

标签 java android xml canvas paint

我无法让清除按钮在我的程序中工作。我的圆圈按钮可以显示随机圆圈,但我的清除按钮什么也不做。我迷路了。我知道我应该使用 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>

最佳答案

每次调用 doIt 时,您都会创建一个新的 DrawCircle 实例。

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

关于java - 创建一个清除按钮来清除 Canvas android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171016/

相关文章:

android - 分享帖子后如何自动关闭 InAppBrowser?

xml - 使用 Xpath 找到共同的 parent

java - 佳能相机在一段时间后卡住 - 1200D EOS sdk java

java - 在 Spring 表达式语言 (SpEL) 中评估属性

java - Maven 不会使用公钥进行部署

Android Firebase - setPersistenceEnabled(true) 使应用程序崩溃

android - 通过语音识别发送消息?

xml - TestNG Xml 文件 :I want to run same class for multiple time with different parameters

java - 如何修复空指针异常? (Android SDK、Eclipse)

java - Tomcat 8.5.45 启动后抛出大量 NullPointerException-s 但继续工作