如何使用 onDraw/drawRect 基于 mainActivity 中的 editText 制作一个矩形

How to use onDraw/drawRect to make a rectangle based on editText from the mainActivity

基本上我是想从用户那里收集房间的大小..(例如 20x20 大小的房间)。使用在主 Activity 上收集的这些维度,将该数据发送到结果 Activity 并使用 onDraw/onMeasure 创建一个简单的框。

总之,我想从用户那里收集mainActivity中盒子的大小。然后在结果中 activity 显示框的完整图像。

我已经完成了从一个 activity 到另一个 Intent 的数据传递。它使用收集到的数据来绘制我遇到问题的形状。


public class MainActivity extends AppCompatActivity {

    //variables
    Button btnCalculate, btnReset;
    Spinner spinSpeaker;
    EditText editLength, editWidth, editHeight;
    String t1,t2,t3;

    int numLength, numWidth, numHeight, numResult=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //connecting variables to GUI elements
        btnCalculate= findViewById(R.id.btnCalculate);
        btnReset = findViewById(R.id.btnReset2);
        spinSpeaker = findViewById(R.id.spinSpeaker);
        editLength= findViewById(R.id.editLength);
        editWidth = findViewById(R.id.editWidth);
        editHeight = findViewById(R.id.editHeight);




        //adding function to the calculate button
        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //parsing text to integers
                t1=editLength.getText().toString();
                t2=editWidth.getText().toString();
                t3=editHeight.getText().toString();

                if (t3.equals("")) {
                    editHeight.setText("11");
                }else if (t2.equals("")) {
                    editWidth.setText("11");
                }else if (t1.equals("")) {
                    editLength.setText("11");
                }else{
                    numLength = Integer.parseInt(t1);
                    numWidth = Integer.parseInt(t2);
                    numHeight = Integer.parseInt(t3);
                }

                //alert to contact Pro design
                if ((numLength>=999)||(numWidth>=999)||(numHeight>=20)) {

                    Toast.makeText(MainActivity.this, R.string.contactPro,
                            Toast.LENGTH_LONG).show();
                }else {
                    //calculating volume
                    numResult = numHeight * numWidth * numLength;

                    //sending data to other activities
                    Intent intent = new Intent(MainActivity.this, Result.class);
                    intent.putExtra("varHeight", numHeight);
                    intent.putExtra("varWidth", numWidth);
                    intent.putExtra("varLength", numLength);
                    intent.putExtra("varResult", numResult);
                    startActivity(intent);
                }
            }
        });


    }
//Result activity
public class Result extends AppCompatActivity {

    TextView tim;
    MyCanvas mycanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mycanvas = new MyCanvas(this);
        setContentView(R.layout.activity_result);
        setContentView(Jim);


        //Pulling variables from main activity
        Intent variables = getIntent();
        //int length = variables.getIntExtra("varLength",0);
        //int width =variables.getIntExtra("varWidth",0);
        int totalResult = variables.getIntExtra("varResult",0);
        //showing total
        String result = String.valueOf(totalResult);
        tim = findViewById(R.id.txtResult);
        tim.setText(result);


        //Calling the reset button method
        configureResetBtn();

    }

        public void configureResetBtn(){
    Button btnReset=findViewById(R.id.btnReset2);
    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //startActivity(new Intent(Result.this, MainActivity.class));
            finish();
        }
    });
}


}
//MyCanvas activity
public class MyCanvas extends AppCompatActivity {


    public MyCanvas(Result result) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent variables = getIntent();
        final int length = variables.getIntExtra("varLength", 0);
        final int width = variables.getIntExtra("varWidth", 0);


         class Jim extends View {

            Paint paint = new Paint();
            Rect rect = new Rect();

            public Jim(Context context) {
                super(context);
            }

            @Override
            protected void onDraw(android.graphics.Canvas canvas) {
                super.onDraw(canvas);
                paint.setStrokeWidth(3);

                canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
            }

            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);

                setMeasuredDimension(length,width);
            }
        }
    }
}

预期结果是显示一个框,该框的尺寸基于用户(例如 20x20)。但是我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

但是当我删除试图绘制框的代码时,我的按钮起作用了。

而不是两次设置内容视图 在 ResultActivityR.layout.activity_result

中使用 SurfaceView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
//....

</FrameLayout>

在 ResultActivity 中

SurfaceView surfaceView = findViewById(R.id.surface_view);

surfaceView.getHolder().addCallback(new Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        Canvas canvas = holder.lockCanvas();
         // draw
        Paint paint = new Paint();
        paint.setStrokeWidth(3);
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }
});