Wednesday, November 18, 2015

android - How to rotate a Bitmap on Canvas center

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#9ebf98"
    >
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcode;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Matrix;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Resources mResources;
    private RelativeLayout mRelativeLayout;
    private Button mButton;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the Resources
        mResources = getResources();

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mButton = (Button) findViewById(R.id.btn);
        mImageView = (ImageView) findViewById(R.id.iv);

        // Set a click listener for Button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Get the source bitmap to draw on canvas
                Bitmap srcBitmap = BitmapFactory.decodeResource(
                        mResources,
                        R.drawable.avatar_girl_small
                );

                // Initialize a new Bitmap
                Bitmap bitmap = Bitmap.createBitmap(
                        700, // Width
                        500, // 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.YELLOW);

                // Initialize a new Paint instance to draw on canvas
                Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setDither(true);
                paint.setFilterBitmap(true);

                /*
                    Matrix
                        The Matrix class holds a 3x3 matrix for transforming coordinates.
                */
                /*
                     Matrix()
                        Create an identity matrix
                */

                // Initialize a new Matrix instance
                Matrix matrix = new Matrix();

                /*
                    public void setRotate (float degrees, float px, float py)
                        Set the matrix to rotate by the specified number of degrees, with a pivot
                        point at (px, py). The pivot point is the coordinate that should remain
                        unchanged by the specified transformation.
                */

                // Set rotation on matrix
                matrix.setRotate(
                        45, // degrees
                        srcBitmap.getWidth() / 2, // px
                        srcBitmap.getHeight() / 2 // py
                );

                /*
                      postTranslate(float dx, float dy)
                            Postconcats the matrix with the specified translation.
                */

                // Draw the bitmap at the center position of the canvas both vertically and horizontally
                matrix.postTranslate(
                        canvas.getWidth() / 2 - srcBitmap.getWidth() / 2,
                        canvas.getHeight() / 2 - srcBitmap.getHeight() / 2
                );

                /*
                    public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)
                        Draw the bitmap using the specified matrix.

                    Parameters
                        bitmap : The bitmap to draw
                        matrix : The matrix used to transform the bitmap when it is drawn
                        paint : May be null. The paint used to draw the bitmap
                */

                // Finally, draw the bitmap on canvas as a rotated bitmap
                canvas.drawBitmap(
                        srcBitmap, // Bitmap
                        matrix, // Matrix
                        paint // Paint
                );

                // Display the newly created bitmap on app interface
                mImageView.setImageBitmap(bitmap);
            }
        });
    }
}
More android examples