Wednesday, November 18, 2015

android - How to rotate a Canvas

MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        Button button = findViewById(R.id.button);
        ImageView imageView = findViewById(R.id.imageView);


        // Set a click listener for Button widget
        button.setOnClickListener(view -> {
            // Initialize a new Bitmap
            Bitmap bitmap = Bitmap.createBitmap(
                    1200, // Width
                    600, // Height
                    Bitmap.Config.ARGB_8888 // Config
            );

            // Initialize a new Canvas instance
            Canvas canvas = new Canvas(bitmap);

            // Draw a solid color on the canvas as background
            canvas.drawColor(Color.LTGRAY);

            // Initialize a new Paint instance to draw on canvas
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.parseColor("#676767"));
            paint.setAntiAlias(true);

            // Set an offset value in pixels to draw rectangle on canvas
            int offset = 200;

            // Save the canvas state
            canvas.saveLayer(
                    0, // left
                    0, // top
                    canvas.getWidth(),
                    canvas.getHeight(),
                    null // paint
            );

            // Rotate the canvas
            canvas.rotate(
                    25, // degrees
                    (float) canvas.getWidth() / 2, // px, center x
                    (float) canvas.getHeight() / 2 // py, center y
            );

            // Draw the rounded corners rectangle object on the canvas
            // The rectangle will be drawn as a 25 degrees rotated rectangle
            canvas.drawRect(
                    offset, // left
                    offset, // top
                    canvas.getWidth() - offset, // right
                    canvas.getHeight() - offset, // bottom
                    paint // Paint
            );

            // Finally, restore the canvas state
            canvas.restore();

            // Display the newly created bitmap on app interface
            imageView.setImageBitmap(bitmap);
        });
    }
}
activity_main.xml

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>