create Drawable by text size - Android Graphics

Android examples for Graphics:Drawable Size

Description

create Drawable by text size

Demo Code


//package com.java2s;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Main {
    public static Drawable createDrawable(Context context,
            Drawable drawable, int textSize) {
        if (drawable == null) {
            return null;
        }//from  w w w. jav  a  2  s.  co m
        int allWidth = (int) (textSize * 1.2 + 0.5);
        int allHeight = allWidth;

        Bitmap image = Bitmap.createBitmap(allWidth, allHeight,
                Config.ARGB_8888);
        Canvas canvas = new Canvas(image);

        drawable.setBounds(0, 0, textSize + 2, textSize + 2);
        drawable.draw(canvas);
        image.setDensity(Bitmap.DENSITY_NONE);

        return new BitmapDrawable(context.getResources(), image);
    }
}

Related Tutorials