Andriod Paint Draw Text Size

Android – How to draw text on a bitmap

28. 04. 2021

ALERT: This commodity might exist outdated and not fit for your needs. In society to bask its updated version, visit Android Guide To: Drawing Text Over Bitmap

Photo by Alexander Andrews on Unsplash

This short commodity shows you how to draw text on bitmap loaded from resources. You can see also multiline version.

Commencement we read bitmap from resources. This bitmap is immutable, and then we create mutable re-create. So we create Paint object and set them text size, color, shadow, etc.

Now nosotros are ready to draw! Merely where? We need to calculate position of the bottom left corner of drawn text. That's all. Enjoy!

public Bitmap drawTextToBitmap(Context gContext,    int gResId,    String gText) {   Resources resources = gContext.getResources();   float scale = resource.getDisplayMetrics().density;   Bitmap bitmap =        BitmapFactory.decodeResource(resources, gResId); 	   android.graphics.Bitmap.Config bitmapConfig =       bitmap.getConfig();   // set default bitmap config if none   if(bitmapConfig == aught) {     bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;   }   // resources bitmaps are imutable,    // so we need to convert it to mutable ane   bitmap = bitmap.re-create(bitmapConfig, true); 	   Canvas canvas = new Sail(bitmap);   // new antialised Paint   Paint paint = new Pigment(Paint.ANTI_ALIAS_FLAG);   // text color - #3D3D3D   paint.setColor(Colour.rgb(61, 61, 61));   // text size in pixels   paint.setTextSize((int) (14 * calibration));   // text shadow   pigment.setShadowLayer(1f, 0f, 1f, Color.WHITE); 	   // draw text to the Canvas center   Rect bounds = new Rect();   pigment.getTextBounds(gText, 0, gText.length(), premises);   int 10 = (bitmap.getWidth() - premises.width())/2;   int y = (bitmap.getHeight() + premises.elevation())/2; 	   canvas.drawText(gText, x, y, paint); 	   return bitmap; }            

EDIT 2013-eleven-16: Fixed problems in canvas.drawText() call. Variables x and y are no longer multiplied past scale.

EDIT 2015-xi-xiv: Added link to multiline version.

Do you demand more than detailed help? Book a consultation:
Let's conversation about it

Do you want more?

We share the earth of software development on our Instagram. Join us 🙂

We likewise tend to look for Android dev (iOS every bit well), simply there are also chances for a project manager or a tester. Ship us an e-mail:skoumal@skoumal.net.

brownrithand.blogspot.com

Source: https://www.skoumal.com/en/android-how-to-draw-text-on-a-bitmap/

0 Response to "Andriod Paint Draw Text Size"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel