Skip to content

Instantly share code, notes, and snippets.

@ChaosPower
Last active November 20, 2021 05:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaosPower/8232164 to your computer and use it in GitHub Desktop.
Save ChaosPower/8232164 to your computer and use it in GitHub Desktop.
BMP Converter Utils
//You need to change this to your package
package com.example.xxx;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
/**
*
* Utility for converting views and fonts to bitmaps. May be used to center spannable strings
* To Buttons and Views.
*
* This utils is a composition of found references to solve text align to the center middle of the
* view.
*
*/
public class ConvUtils {
//SEE : http://stackoverflow.com/questions/20780685/how-set-margin-between-spannable-textview
public static Object convertViewToDrawable(View view, Context context) {
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
view.setDrawingCacheEnabled(true);
Bitmap cacheBmp = view.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
view.destroyDrawingCache();
return new BitmapDrawable(context.getResources(), viewBmp);
}
//http://stackoverflow.com/questions/8799290/convert-string-text-to-bitmap
//Converted return type to BitmapDrawable in order to set as background of a view (i.e. drawableLeft property)
public BitmapDrawable textAsBitmap(Activity ac, String text, float textSize, int textColor, int type) {
Typeface font;
switch(type)
{
case 1:
font = Typeface.createFromAsset(ac.getAssets(), "fontawesome-webfont.ttf");
break;
case 2:
font = Typeface.DEFAULT_BOLD;
break;
default:
font = Typeface.DEFAULT;
}
Paint paint = new Paint();
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.LEFT);
paint.setTypeface(font);
int width = (int) (paint.measureText(text) + 0.5f); // round
float baseline = (int) (-paint.ascent() + 0.5f); // ascent() is negative
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
canvas.drawText(text, 0, baseline, paint);
return new BitmapDrawable(ac.getApplicationContext().getResources(), image);
}
//Utility for making button icons from fonts
public void createButtonIcon(Activity ac, View view, int vId, String icon, float iconSize, String label, float fontSize, String iconColor, int labelFontType, String fontColor, String subLabel) {
Button xBtn = (Button) view.findViewById(vId);
//Init Spannable
Spannable xx = new SpannableString(label);
BitmapDrawable bt = (BitmapDrawable) textAsBitmap(ac, Html.fromHtml(icon).toString(), iconSize, Color.parseColor(iconColor), 1);
//Dynamically set drawableLeft
xBtn.setCompoundDrawablesWithIntrinsicBounds(bt, null,null,null);
xBtn.setText(xx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment