首页 > 解决方案 > 单击按钮使用画布绘制连续线

问题描述

拜托,伙计们,我想通过单击一个按钮在我的 android 项目上画一条连续的线,到目前为止我已经尝试使用 invalidate() 重绘,但是我看到当我调用 invalidate 方法时,而不是保持旧线并从旧线的末端绘制,它重新绘制一条全新的线并擦除旧线。我现在的问题是如何通过单击按钮来保持画线。

下面是我的 MainActivity.java

package com.example.drawlines;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    LinearLayout device;
    LinearLayout UIControls;
    LinearLayout UIControls1;
    LinearLayout UIControls2;
    LinearLayout UIControls3;
    RelativeLayout UILast;

    TextView lineThick, lineColor, arrowKeys;
    Spinner lineThickness;
    RadioGroup rgb_Group;
    RadioButton red, yellow, cyan;

    public static int startX;
    public static int startY;
    public static int endX = 0;
    public static int endY = 0;

    Button clear, reset;
    ImageButton up, down, left, right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        device = new LinearLayout(this);
        LinearLayout.LayoutParams deviceParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        device.setLayoutParams(deviceParameters);
        device.setOrientation(LinearLayout.VERTICAL);

        UIControls = new LinearLayout(this);
        LinearLayout.LayoutParams UIControlsParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        UIControls.setLayoutParams(UIControlsParameters);
        UIControls.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams UIControls1ParametersAlign = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,1.0f);


        /**clear = new Button(this);
        clear.setText("Clear");
        clear.setLayoutParams(UIControls1ParametersAlign);
        reset = new Button(this);
        reset.setText("Reset");
        reset.setLayoutParams(UIControls1ParametersAlign);**/







        UIControls1 = new LinearLayout(this);
        LinearLayout.LayoutParams UIControls1Parameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);

        UIControls1.setLayoutParams(UIControls1Parameters);
        UIControls1.setOrientation(LinearLayout.VERTICAL);
        lineThick = new TextView(this);
        lineThick.setText("Line Thickness");


        List<Integer> thicknessDigits = new ArrayList<Integer>();
        thicknessDigits.add(10);
        thicknessDigits.add(20);
        thicknessDigits.add(30);
        thicknessDigits.add(40);
        thicknessDigits.add(50);

        ArrayAdapter<Integer> thicknessAdapter = new ArrayAdapter<Integer>(
                this,android.R.layout.simple_spinner_dropdown_item,thicknessDigits
        );
        lineThickness = new Spinner(this);
        //lineThickness.setScrollBarSize(6);
        lineThickness.setAdapter(thicknessAdapter);
        LinearLayout.LayoutParams spinnerSize = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        lineThickness.setLayoutParams(spinnerSize);
        lineThick.setLayoutParams(spinnerSize);
        UIControls1.addView(lineThick);
        UIControls1.addView(lineThickness);
        UIControls1.setBackgroundColor(Color.RED);
        //UIControls1.addView(clear);
        //UIControls1.addView(reset);

        UIControls2 = new LinearLayout(this);
        LinearLayout.LayoutParams UIControls2Parameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,1.0f);
        UIControls2.setLayoutParams(UIControls2Parameters);
        UIControls2.setOrientation(LinearLayout.VERTICAL);
        lineColor = new TextView(this);
        lineColor.setText("Line Color");
        lineColor.setLayoutParams(UIControls1ParametersAlign);
        rgb_Group = new RadioGroup(this);
        rgb_Group.setOrientation(LinearLayout.VERTICAL);
        red = new RadioButton(this);
        red.setText("Red");
        rgb_Group.addView(red);
        yellow = new RadioButton(this);
        yellow.setText("Yellow");
        rgb_Group.addView(yellow);
        cyan = new RadioButton(this);
        cyan.setText("Cyan");
        rgb_Group.addView(cyan);
        rgb_Group.setLayoutParams(UIControls1ParametersAlign);
        UIControls2.addView(lineColor);
        UIControls2.addView(rgb_Group);
        UIControls2.setBackgroundColor(Color.GREEN);



        UIControls3 = new LinearLayout(this);
        LinearLayout.LayoutParams UIControls3Parameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
        UIControls3.setLayoutParams(UIControls3Parameters);
        UIControls3.setOrientation(LinearLayout.VERTICAL);
        arrowKeys = new TextView(this);
        arrowKeys.setText("Arrow Keys");
        UIControls3.addView(arrowKeys);
        UIControls3.setBackgroundColor(Color.RED);

        UILast = new RelativeLayout(this);
        RelativeLayout.LayoutParams relativeAlign = new RelativeLayout.LayoutParams(350,ViewGroup.LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams arrowAlign = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams arrowAlignRight = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams arrowAlignLeft = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams arrowAlignDown = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        UILast.setLayoutParams(relativeAlign);
        UILast.setBackgroundColor(Color.BLUE);

        /**arrowKeys = new TextView(this);
        arrowKeys.setText("Arrow Keys");
        arrowKeys.setLayoutParams(arrowAlign);
        UILast.addView(arrowKeys);
        clear = new Button(this);
        clear.setText("^");
        clear.setLayoutParams(arrowAlign);**/

        //arrowAlign.width = 50;
        arrowAlign.addRule(RelativeLayout.CENTER_HORIZONTAL);
        DrawerApp.upp = new ImageButton(this);
        DrawerApp.upp.setImageResource(R.drawable.up18);
        DrawerApp.upp.setBackgroundColor(Color.WHITE);
        DrawerApp.upp.setPadding(20,20,20,20);
        DrawerApp.upp.setLayoutParams(arrowAlign);
        UILast.addView(DrawerApp.upp);

        arrowAlignDown.addRule(RelativeLayout.CENTER_HORIZONTAL);
        arrowAlignDown.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        DrawerApp.downn = new ImageButton(this);
        DrawerApp.downn.setImageResource(R.drawable.down18);
        DrawerApp.downn.setBackgroundColor(Color.WHITE);
        DrawerApp.downn.setPadding(20,20,20,20);
        DrawerApp.downn.setLayoutParams(arrowAlignDown);
        UILast.addView(DrawerApp.downn);

        /**arrowAlignRight.addRule(RelativeLayout.CENTER_IN_PARENT);
        arrowAlignRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        right = new ImageButton(this);
        right.setImageResource(R.drawable.right18);
        right.setBackgroundColor(Color.WHITE);
        right.setPadding(20,20,20,20);
        right.setLayoutParams(arrowAlignRight);
        UILast.addView(right);**/

        arrowAlignRight.addRule(RelativeLayout.CENTER_IN_PARENT);
        arrowAlignRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        right = new ImageButton(this);
        right.setImageResource(R.drawable.right18);
        right.setBackgroundColor(Color.WHITE);
        right.setPadding(20,20,20,20);
        right.setLayoutParams(arrowAlignRight);
        UILast.addView(right);

        arrowAlignLeft.addRule(RelativeLayout.CENTER_IN_PARENT);
        arrowAlignLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        left = new ImageButton(this);
        left.setImageResource(R.drawable.left18);
        left.setBackgroundColor(Color.WHITE);
        left.setPadding(20,20,20,20);
        left.setLayoutParams(arrowAlignLeft);
        UILast.addView(left);

        /**
        arrowAlignRight.addRule(RelativeLayout.CENTER_IN_PARENT);
        arrowAlignRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        right = new ImageButton(this);
        right.setImageResource(R.drawable.right18);
        right.setBackgroundColor(Color.WHITE);
        right.setPadding(20,20,20,20);
        right.setLayoutParams(arrowAlignRight);
        UILast.addView(right);
         **/


        UIControls3.addView(UILast);


        clear = new Button(this);
        clear.setText("Clear Canvas");
        clear.setLayoutParams(spinnerSize);
        UIControls.addView(UIControls1);
        UIControls.addView(UIControls2);
        UIControls.addView(UIControls3);



        device.addView(UIControls);
        device.addView(clear);
        device.addView(new DrawerApp(this));

        setContentView(device);
    }
}

这是我的 DrawerApp.java 类

package com.example.drawlines;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class DrawerApp extends View {


    public int startX = 0;
    public int startY = 0;
    public int enderX = 0;
    public int enderY = 0;
    public static ImageButton upp;
    public static ImageButton downn;
    public Canvas can;
    Paint paint;
    Path linePath = new Path();


    public DrawerApp(final Context context) {
        super(context);
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStrokeWidth(8);
        paint.setStyle(Paint.Style.STROKE);

        setBackgroundColor(Color.BLUE);
        //setOrientation(HORIZONTAL);
        //Button clear = new Button(context);
        upp.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                String str = "Up Clicked";
                Toast.makeText(context.getApplicationContext(),str, Toast.LENGTH_SHORT).show();

                startX = enderX;
                startY = enderY;
                enderX += 130;
                enderY += 120;

                invalidate();
                //startX = enderX;
                //startY = enderY;


            }
        });

        downn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                String str = "Down Clicked";
                Toast.makeText(context.getApplicationContext(),str, Toast.LENGTH_SHORT).show();
                startX = enderX;
                startY = enderY;
                enderX += 20;
                enderY += 98;
                invalidate();
                //drawLine(enderX, enderY);

            }
        });

    }

    public void drawLine(int endx, int endy)
    {
        startX = enderX;
        startY = enderY;
    }
    public void draw()
    {
        can.drawLine(10,30,120,400, paint);
    }



    @Override
    protected void onDraw(Canvas canvas) {
        //Paint paint =
        //Drawing a Line (StartX, StartY, EndX, EndY, PaintObj)
        canvas.drawLine(startX,startY,enderX,enderY, paint);
        //linePath.reset();
        //linePath.moveTo(0,0);
        //linePath.lineTo(120, 200);
        //canvas.drawPath(linePath, paint);
        //canvas.drawLine(0,0,120,120, paint);
        //invalidate();
    }

}


当我运行我的代码时,它看起来像下面

当我单击向上箭头时,它会从中创建一个新行并删除旧行

当我单击向上箭头时,它会从中创建一个新行并删除旧行

但下面实际上是我想要实现的。

在此处输入图像描述

标签: javaandroidcanvas

解决方案


推荐阅读