简体   繁体   English

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

[英]restrict a custom view in a relative layout

I am new to android and I am a bit stuck.我是 android 新手,我有点卡住了。 I am trying to create a simple drawing app which shows an example on the top of the page and a square space for it in below it.我正在尝试创建一个简单的绘图应用程序,它在页面顶部显示一个示例,并在其下方显示一个方形空间。 Aim is to display a letter and a kid needs to practice in replicating it.目的是展示一封信,孩子需要练习复制它。

I have troubles including the drawing class in the layout which needs to restrict its boundaries.我在需要限制其边界的布局中包含绘图类时遇到了麻烦。

this is the main layout这是主要布局

<?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>

this is the PaintView class这是 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);
    }

this is how it is called in the Main Activity这就是它在主活动中的调用方式

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

    }

I need to fit the drawing board into the "drawingBoard".我需要将绘图板装入“绘图板”。

It is not necessary the right approach, but this is as far as I got.没有必要采用正确的方法,但就我所知。

Thanks in advance提前致谢

Attach the customview to a fragment.将自定义视图附加到片段。 This is a completely different approach so you will have to make a lot of changes to your code.这是一种完全不同的方法,因此您必须对代码进行大量更改。

The other approach would be adding the view programmatically to the ConstraintLayout.另一种方法是以编程方式将视图添加到 ConstraintLayout。 In 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(), 
       ConstraintSet.TOP, 60);
        // ... similarly add other constraints
         set.applyTo(parentLayout);
}

In the activity you are overwriting the content view with just the paint view: setContentView(paintView);在活动中,您仅用绘制视图覆盖内容视图: setContentView(paintView);

Remove that line.删除该行。

Add the PaintView in XML instead: 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>

With this approach you have to inflate the custom view in its constructors:使用这种方法,您必须在其构造函数中扩展自定义视图:

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, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

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

To interact with the PaintView from the activity code you can do:要从活动代码与PaintView交互,您可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM