Вращающийся контур вне метода onDraw - PullRequest
0 голосов
/ 20 июня 2020

Я пытаюсь создать простое приложение, которое отображает aws простую фигуру, а затем после нажатия кнопки выполняет некоторое аффинное преобразование с фигурой. Можете ли вы помочь мне закончить onClick метод поворота? Большое тебе спасибо. Проблема в том, что я не знаю, как передать Canvas этому методу из onDraw метода в моем CanvasView, если это возможно.

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>

1 Ответ

0 голосов
/ 20 июня 2020

Вы не должны передавать Canvas в rotate или любые другие функции. Все, что связано с изменением холста, должно происходить в методе onDraw.

Обновите свой метод rotate(), чтобы вызвать invalidate() метод View, чтобы он был подхвачен системой для последующего рисования. frame:

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