Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在相对布局中限制自定义视图_Java_Android_Canvas_Paint - Fatal编程技术网

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

Java 在相对布局中限制自定义视图,java,android,canvas,paint,Java,Android,Canvas,Paint,我刚接触安卓系统,有点被卡住了。我正在尝试创建一个简单的绘图应用程序,它在页面顶部显示一个示例,在页面下方显示一个正方形空间。目的是展示一个字母,孩子需要练习复制它 我有麻烦,包括绘图类的布局,需要限制其边界 这是主要布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns: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>
这是在主活动中的调用方式

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

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

没有必要采取正确的方法,但这是我所能做到的


提前感谢

将customview附加到片段。 这是一种完全不同的方法,因此您必须对代码进行大量更改

另一种方法是以编程方式将视图添加到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);
}

将customview附加到片段。 这是一种完全不同的方法,因此您必须对代码进行大量更改

另一种方法是以编程方式将视图添加到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)

拆下那条线

改为在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>
要通过活动代码与
PaintView
交互,可以执行以下操作:

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

在“活动”中,仅使用绘制视图覆盖内容视图:
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>
要通过活动代码与
PaintView
交互,可以执行以下操作:

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


您对
setContentView()
的实现是完全错误的。第二次调用该方法不会将视图添加到以前的布局中,而是替换它。无论如何,为什么不在主布局中添加
PaintView
?您对
setContentView()
的实现是完全错误的。第二次调用该方法不会将视图添加到以前的布局中,而是替换它。不管怎样,为什么不在主布局中添加
PaintView
?不起作用或者我错过了什么。但它并没有崩溃,忘记了关键的一步。膨胀自定义视图。请看我的最新答案。我现在对项目的这一部分有点厌倦了,而且进度很紧。总有一天我会试一试,但现在我没有时间。谢谢你的回答和努力!!!没有工作,或者我错过了什么。但它并没有崩溃,忘记了关键的一步。膨胀自定义视图。请看我的最新答案。我现在对项目的这一部分有点厌倦了,而且进度很紧。总有一天我会试一试,但现在我没有时间。谢谢你的回答和努力!!!谢谢!它现在起作用了。你知道如何通过点击按钮来移除绘制的东西吗?我已经设法添加了代码,我能够找到当按钮从主布局按下,但我不能删除绘制的东西@重写受保护的void onDraw(画布画布){if(cc){//CLEAR!!!!!cc=false;}否则{Canvas.drawPath(path,brush);}使用@Override protected void onDraw(Canvas Canvas){if(cc){Canvas.drawColor(Color.WHITE);//使用电路板的颜色。cc=false;}否则{Canvas.drawPath(path,brush);}}我已经尝试过这个方法,它只会更改背景,但图形仍然存在。让我们改为这样做吧@重写受保护的void onDraw(Canvas Canvas){if(cc){path=new path();Paint clearPaint=new Paint();clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));Canvas.drawRect(0,0,0,0,clearPaint);cc=false;}else{Canvas.drawPath(path,brush);}我稍微碰了一下代码,代码就工作了。它也适用于此now path.reset();path.moveTo(pointX,pointY);非常感谢你的帮助!谢谢!它现在起作用了。你知道如何通过点击按钮来移除绘制的东西吗?我已经设法添加了代码,我能够找到当按钮从主布局按下,但我不能删除绘制的东西@重写受保护的void onDraw(画布画布){if(cc){//CLEAR!!!!!cc=false;}否则{Canvas.drawPath(path,brush);}使用@Override protected void onDraw(Canvas Canvas){if(cc){Canvas.drawColor(Color.WHITE);//使用电路板的颜色。cc=false;}否则{Canvas.drawPath(path,brush);}}我已经尝试过这个方法,它只会更改背景,但图形仍然存在。让我们改为这样做吧@重写受保护的void onDraw(Canvas Canvas){if(cc){path=new path();Paint clearPaint=new Paint();clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));Canvas.drawRect(0,0,0,0,clearPaint);cc=false;}else{Canvas.drawPath(path,brush);}我稍微碰了一下代码,代码就工作了。它也适用于此now path.reset();path.moveTo(pointX,pointY);非常感谢你的帮助!