Android Studio带按钮的清晰绘图视图

5lwkijsr  于 2023-01-14  发布在  Android
关注(0)|答案(1)|浏览(106)

我已经为绘制笔划设置了一个带有自定义视图的片段。现在我想创建一个清除笔划的按钮,但这是不可能的。
我有两门课
1.扩充片段类
1.扩展视图类
为了清理画布,我学会了这样做:

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

问题是Canvas对象卡在了onDraw()方法中,但是我需要button.setOnClickListener中的canvas对象来清除画布。
我该怎么做呢?

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.app.ssl.R;
import com.app.ssl.databinding.FragmentDrawviewBinding;

public class DrawviewFragment extends Fragment {
    private  FragmentDrawviewBinding binding;
    private View root;
    private View drawView;
    private Button btnClear;
    private Button btnSave;

    public View onCreateView(
            @NonNull LayoutInflater inflater,
            ViewGroup container,
            Bundle savedInstanceStage) {
        root = inflater.inflate(R.layout.fragment_drawview,container,false);
        drawView = root.findViewById(R.id.DrawingView);
        btnClear = root.findViewById(R.id.btnClean1);
        btnSave = root.findViewById(R.id.btnSave);

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
               System.out.println("CLEAR");

               //drawView.setBackgroundColor(Color.WHITE);

                // CLEAR CANVAS
                //DrawView.canvas.drawColor(Color.WHITE);
                //DrawView.canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                DrawView.canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.MULTIPLY);
                //DrawView.onDraw(new Canvas());

            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                System.out.println("SAVE");
            }
        });
        return root;
    }
}


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class DrawView extends View {
    private Paint drawPaint;
    private Path path = new Path();
    static Canvas canvas;

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setupPaint();
    }

    // Setup paint with color and stroke styles
    private void setupPaint() {
        drawPaint = new Paint();
        drawPaint.setColor(Color.BLACK);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(5);
        drawPaint.setStyle(Paint.Style.STROKE); // draw with stroke
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        DrawView.canvas = canvas;
        canvas.drawPath(path, drawPaint);
    }

    // Get x and y and append them to the path
    public boolean onTouchEvent(MotionEvent event) {
        float pointX = event.getX();
        float pointY = event.getY();
        // Checks for the event that occurs
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // Starts a new line in the path
                path.moveTo(pointX, pointY);
                break;
            case MotionEvent.ACTION_MOVE:
                // Draws line between last point and this point
                path.lineTo(pointX, pointY);
                break;
            default:
                return false;
        }
        postInvalidate(); // Indicate view should be redrawn
        return true; // Indicate we've co
    }

}

相关问题