gpt4 book ai didi

java - onDraw 方法之外的旋转路径

转载 作者:行者123 更新时间:2023-12-01 16:13:00 27 4
gpt4 key购买 nike

我正在尝试创建一个简单的应用程序,绘制一个简单的图形,然后单击按钮后对图形进行一些仿射变换。你能帮我完成 onClick 方法旋转吗?万分感谢。问题是,如果可能的话,我不知道如何从 CanvasView 中的 onDraw 方法将 Canvas 传递给该方法。

MainActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
Button button_1;
Button button_2;
Button button_3;
Button button_4;
private CanvasView customCanvas;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customCanvas = (CanvasView) findViewById(R.id.signature_canvas);

button_1 = (Button) findViewById(R.id.btn_1);
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customCanvas.translate();
}
});

button_2 = (Button) findViewById(R.id.btn_2);
button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customCanvas.reflect();
}
});

button_3 = (Button) findViewById(R.id.btn_3);
button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customCanvas.scale();
}
});

button_4 = (Button) findViewById(R.id.btn_4);
button_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customCanvas.rotate();//How to pass Canvas from my CanvasView to rotate method?
}
});
}
}

CanvasView.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

class CanvasView extends View {
private Paint redPaint;
private Matrix matrix;
private Path path;

public CanvasView(Context c, AttributeSet attrs) {
super(c, attrs);
redPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
redPaint.setStyle(Paint.Style.STROKE);
redPaint.setColor(0xffff0000);
redPaint.setStrokeWidth(5);
matrix = new Matrix();
path = new Path();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.moveTo(400, 400);
path.lineTo(400, 800);
path.lineTo(800, 700);
path.lineTo(600, 600);
path.lineTo(800, 500);
path.lineTo(400, 400);
path.close();
canvas.drawPath(path, redPaint);
}
public void translate() {

}
public void reflect() {

}
public void scale() {

}
public void rotate(Canvas canvas) {
matrix.reset();
matrix.setRotate(90, 400, 400);
path.transform(matrix);
redPaint.setColor(Color.BLUE);
canvas.drawPath(path, redPaint);
}
}

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom">

<com.example.lab2.CanvasView
android:id="@+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"/>

<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:orientation="horizontal">

<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Перенос"
android:textSize="8dp" />

<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Отражение"
android:textSize="8dp" />

<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Масштабирование"
android:textSize="8dp" />

<Button
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Поворот"
android:textSize="8dp" />

</LinearLayout>

</RelativeLayout>

最佳答案

您不应将 Canvas 传递到 rotate 或任何其他函数中。与修改 Canvas 相关的所有事情都应该在 onDraw 方法中发生。

更新您的 rotate() 方法以调用 Viewinvalidate() 方法,以便系统拾取该方法进行绘制对于下一帧:

    public void rotate() {
matrix.reset();
matrix.setRotate(90, 400, 400);
path.transform(matrix);
redPaint.setColor(Color.BLUE);
invalidate(); // Invalidated Views should be updated
}

为什么invalidate()

如果我们看一下 java 文档,我们会找到 invalidate() 方法(和方法体)的下一个解释:

    /**
* Invalidate the whole view. If the view is visible,
* {@link #onDraw(android.graphics.Canvas)} will be called at some point in
* the future.
* <p>
* This must be called from a UI thread. To call from a non-UI thread, call
* {@link #postInvalidate()}.
*/
public void invalidate() {
invalidate(true);
}

关于java - onDraw 方法之外的旋转路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62487552/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com