如何在Android中的位图图像上添加字符串? [英] How to add String on Bitmap Image in Android?

查看:143
本文介绍了如何在Android中的位图图像上添加字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在位图图像上添加一个字符串.我有一个方法drawTextToBitmap,该方法可以成功在位图图像上放置字符串.但是我的位图图像像pinmark图像一样很小.此函数根据位图的高度和宽度.我要放置的字符串超过位图的图像.因此,请帮助我解决问题.

I want to add a string on bitmap image.I have a metod drawTextToBitmap,this method working success place string on bitmap image.But my bitmap image is very small like pinmark image.This function set the string based on the bitmap height and width.I want to place the string exceed than the bitmap image.So Please help me to solve the problem.

以下我用来获取位图的方法:

Following method i am using to get bitmap :

public Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    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.BLACK);
    // text size in pixels
    paint.setTextSize((int) (70 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int m = (bitmap.getWidth() - bounds.width()) / 2;
    int l = (bitmap.getHeight() + bounds.height()) / 2;

    canvas.drawText(gText, 1000, l, paint);

    return bitmap;
}

推荐答案

尝试一下:

public static Bitmap drawStringonBitmap(Bitmap src, String string, Point location, int color, int alpha, int size, boolean underline,int width ,int height) {

    Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAlpha(alpha);
    paint.setTextSize(size);
    paint.setAntiAlias(true);
    paint.setUnderlineText(underline);
    canvas.drawText(string, location.x, location.y, paint);

    return result;
}

这篇关于如何在Android中的位图图像上添加字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆