簡體   English   中英

在相對布局中限制自定義視圖

[英]restrict a custom view in a relative layout

我是 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);

    }

我需要將繪圖板裝入“繪圖板”。

沒有必要采用正確的方法,但就我所知。

提前致謝

將自定義視圖附加到片段。 這是一種完全不同的方法,因此您必須對代碼進行大量更改。

另一種方法是以編程方式將視圖添加到 ConstraintLayout。 在主活動中,

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);
}

在活動中,您僅用繪制視圖覆蓋內容視圖: setContentView(paintView);

刪除該行。

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

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

要從活動代碼與PaintView交互,您可以執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM