Wednesday, November 18, 2015

android - How to draw a Bitmap on a Canvas

MainActivity.java

package com.cfsuman.androidtutorials;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends Activity {
    private Resources mResources;

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

        // Get the Resources
        mResources = getResources();

        // 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 -> {
            // Bitmap to draw on the canvas
            Bitmap bitmap = BitmapFactory.decodeResource(
                    mResources,
                    R.drawable.flower
            );

            // Define an offset value between canvas and bitmap
            int offset = 100;

            // Initialize a new Bitmap to hold the source bitmap
            Bitmap dstBitmap = Bitmap.createBitmap(
                    bitmap.getWidth() + offset * 2, // Width
                    bitmap.getHeight() + offset * 2, // Height
                    Bitmap.Config.ARGB_8888 // Config
            );

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

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

            //Finally, Draw the source bitmap on the canvas
            canvas.drawBitmap(
                    bitmap, // Bitmap
                    offset, // Left
                    offset, // Top
                    null // Paint
            );

            // Display the newly created bitmap on app interface
            imageView.setImageBitmap(dstBitmap);
        });
    }
}
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 Bitmap"
        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>