Monday, September 28, 2015

android - How to convert a drawable to a bitmap

activity_main.xml

<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="10dp"
    tools:context=".MainActivity"
    android:background="#d7e2fb"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Convert Drawable To Bitmap"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


public class MainActivity extends Activity {

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

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

        final ActionBar ab = getActionBar();
        ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFE290EB")));

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                    Drawable
                        A Drawable is a general abstraction for "something that can be drawn."
                        Most often you will deal with Drawable as the type of resource retrieved
                        for drawing things to the screen; the Drawable class provides a generic
                        API for dealing with an underlying visual resource that may take a variety
                        of forms. Unlike a View, a Drawable does not have any facility
                        to receive events or otherwise interact with the user.
                */

                /*
                    BitmapFactory
                        Creates Bitmap objects from various sources, including
                        files, streams, and byte-arrays.
                */

                /*
                    public static Bitmap decodeResource (Resources res, int id)
                        Synonym for decodeResource(Resources, int,
                        android.graphics.BitmapFactory.Options) with null Options.

                        Parameters
                            res : The resources object containing the image data
                            id : The resource id of the image data
                        Returns
                            The decoded bitmap, or null if the image could not be decoded.
                */

                /*
                    Resources
                        Class for accessing an application's resources.
                        The Android resource system keeps track of all non-code assets associated
                        with an application. You can use this class to access your application's
                        resources. You can generally acquire the Resources instance
                        associated with your application with getResources().
                */

                /*
                    public abstract Resources getResources ()
                        Return a Resources instance for your application's package.
                */

                // Create bitmap from drawable
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.panda);

                // Set the ImageView image from a bitmap
                iv.setImageBitmap(bitmap);

               // Notify the user that drawable converted to bitmap
                Toast.makeText(getApplicationContext(), "Bitmap created.",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

More android examples