Monday, November 16, 2015

android - Draw border and shadow around a bitmap

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    android:padding="24dp"
    tools:context=".MainActivity">

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

    <ImageView
        android:id="@+id/iv"
        android:layout_width="0dp"
        android:layout_height="250dp"
        android:layout_marginTop="24dp"
        android:scaleType="fitCenter"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java

package com.cfsuman.androidtutorials;

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


public class MainActivity extends Activity {
        private ImageView mImageView;
        private Context mContext;
        private Resources mResources;
        private Bitmap mBitmap;

        @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
                Button btn = findViewById(R.id.button);
                mImageView = findViewById(R.id.iv);

                // Get the bitmap resource id
                final int bitmapResourceID =R.drawable.flower;

                // Set an image to ImageView
                mImageView.setImageBitmap(
                        BitmapFactory.decodeResource(
                                mResources, bitmapResourceID
                        )
                );

                btn.setOnClickListener(v -> {
                        // Get the bitmap from drawable resources
                        mBitmap = BitmapFactory.decodeResource(
                                mResources, bitmapResourceID
                        );

                        // Add a border around the bitmap
                        mBitmap = addBorderToBitmap(
                                mBitmap, 50,
                                Color.parseColor("#F0F8FF")
                        );

                        // Add a border around the bitmap as shadow
                        mBitmap = addBorderToBitmap(
                                mBitmap, 10,
                                Color.LTGRAY
                        );

                        // Set the ImageView image as drawable object
                        mImageView.setImageBitmap(mBitmap);
                });
        }


        // Custom method to add a border around bitmap
        protected Bitmap addBorderToBitmap(
                Bitmap srcBitmap, int borderWidth, int borderColor){

                // Initialize a new Bitmap to make it bordered bitmap
                Bitmap dstBitmap = Bitmap.createBitmap(
                        srcBitmap.getWidth() + borderWidth*2, // Width
                        srcBitmap.getHeight() + borderWidth*2, // Height
                        Bitmap.Config.ARGB_8888 // Config
                );

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

                // Initialize a new Paint instance to draw border
                Paint paint = new Paint();
                paint.setColor(borderColor);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(borderWidth);
                paint.setAntiAlias(true);

                Rect rect = new Rect(
                        borderWidth / 2,
                        borderWidth / 2,
                        canvas.getWidth() - borderWidth / 2,
                        canvas.getHeight() - borderWidth / 2
                );

                // Draw a rectangle as a border/shadow on canvas
                canvas.drawRect(rect,paint);

                // Draw source bitmap to canvas
                canvas.drawBitmap(
                        srcBitmap, borderWidth, borderWidth, null
                );

                srcBitmap.recycle();

                // Return the bordered bitmap
                return dstBitmap;
        }
}