Write text on bitmap

Source: Internet
Author: User
public Bitmap drawTextToBitmap(Context mContext,  int resourceId,  String mText) {    try {         Resources resources = mContext.getResources();            float scale = resources.getDisplayMetrics().density;            Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);            android.graphics.Bitmap.Config bitmapConfig =   bitmap.getConfig();            // set default bitmap config if none            if(bitmapConfig == null) {              bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;            }            // resource bitmaps are imutable,            // so we need to convert it to mutable one            bitmap = bitmap.copy(bitmapConfig, true);            Canvas canvas = new Canvas(bitmap);            // new antialised Paint            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);            // text color - #3D3D3D            paint.setColor(Color.rgb(110,110, 110));            // text size in pixels            paint.setTextSize((int) (12 * scale));            // text shadow            paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);            // draw text to the Canvas center            Rect bounds = new Rect();            paint.getTextBounds(mText, 0, mText.length(), bounds);            int x = (bitmap.getWidth() - bounds.width())/6;            int y = (bitmap.getHeight() + bounds.height())/5;          canvas.drawText(mText, x * scale, y * scale, paint);            return bitmap;    } catch (Exception e) {        // TODO: handle exception        return null;    }  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.