在相对布局中限制自定义视图

问题描述

我是android新手,有点卡住。我正在尝试创建一个简单的绘图应用程序,该应用程序在页面顶部显示一个示例,在页面下方显示一个正方形空间。目的是展示一封信,一个孩子需要练习以复制它。

我在布局中包括绘图类时遇到麻烦,需要限制其边界。

这是主要布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/exampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        enter code here` android:id="@+id/drawingBoard"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/exampleText">

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这是PaintView类

import android.app.ActionBar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;



public class PaintView extends View {

    public ViewGroup.LayoutParams param;
    private Path path = new Path();
    private Paint brush = new Paint();


    public PaintView(MainActivity context) {
        super(context);
        brush.setAntiAlias(true);
        brush.setColor(Color.RED);
        brush.setStyle(Paint.Style.stroke);
        brush.setstrokeJoin(Paint.Join.ROUND);
        brush.setstrokeWidth(8f);

        param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float pointX = event.getX();
        float pointY = event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path.moveto(pointX,pointY);
                return true;

            case MotionEvent.ACTION_MOVE:
                path.lineto(pointX,pointY);
                break;
            default:
                return  false;


        }
        postInvalidate();
        return  false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path,brush);
    }

这是在“主要活动”中的调用方式

  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PaintView paintView = new PaintView(this);
        setContentView(paintView);

    }

我需要将绘图板安装到“ drawingBoard”中。

这不一定是正确的方法,但这是我所知。

预先感谢

解决方法

将customview附加到一个片段。 这是一种完全不同的方法,因此您将不得不对代码进行很多更改。

另一种方法是将视图以编程方式添加到ConstraintLayout中。 在MainActivity中,

protected void onCreate(Bundle  savedInstanceStatee) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
    //remove those lines.
   /*
    PaintView paintView = new PaintView(this);
    setContentView(paintView);*/
     ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.drawingBoard);
      ConstraintSet set = new ConstraintSet();

      PaintView paintView = new PaintView(this);
      // set view id,else getId() returns -1
      paintView.setId(View.generateViewId());
      layout.addView(paintView,0);

      set.clone(parentLayout);
      // connect start and end point of views,in this 
       case top of child to top of parent.

       set.connect(paintView.getId(),ConstraintSet.TOP,parentLayout.getId(),60);
        // ... similarly add other constraints
         set.applyTo(parentLayout);
}
,

在活动中,您仅用绘画视图覆盖内容视图: setContentView(paintView);

删除该行。

改为在XML中添加PaintView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/exampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- TODO: Replace with your package name -->
    <com.example.PaintView
        android:id="@+id/drawingBoard"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/exampleText" />

</androidx.constraintlayout.widget.ConstraintLayout>

使用这种方法,您必须在其构造函数中膨胀自定义视图:

public class PaintView extends ConstraintLayout {
    public PaintView(@NonNull Context context) {
        super(context);
        init();
    }

    public PaintView(@NonNull Context context,@Nullable AttributeSet attrs) {
        super(context,attrs);
        init();
    }

    public PaintView(@NonNull Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
        super(context,attrs,defStyleAttr);
        init();
    }

    public PaintView(@NonNull Context context,int defStyleAttr,int defStyleRes) {
        super(context,defStyleAttr,defStyleRes);
        init();
    }

    public void init() {
        inflate(getContext(),R.layout.layout_product_item,this);
    }
}

要与活动代码中的PaintView进行交互,您可以执行以下操作:

PaintView paintView = (PaintView) findViewById(R.id.drawingBoard);