原创

图片内加字

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/DengDongQi/article/details/80880435
    /**
     * 图片内加字
     */
    public static Bitmap addWordToPicture(Context context, int resourcesId, String content, int testSize, @ColorInt int color) {
        Drawable mCounterDrawable = null;
        Paint mPaint = null;
        if (mCounterDrawable == null) {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mCounterDrawable = context.getResources().getDrawable(resourcesId);
            mPaint.setTextAlign(Paint.Align.CENTER);
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
            mPaint.setColor(color);
            mPaint.setTextSize(Utils.sp2px(context, testSize));
        }
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        Bitmap bitmapDrawable = ((BitmapDrawable) mCounterDrawable).getBitmap().copy(Bitmap.Config.ARGB_8888, true);
        int bitmapX = bitmapDrawable.getWidth();
        int bitmapY = bitmapDrawable.getHeight();
        Canvas canvas = new Canvas(bitmapDrawable);
        canvas.drawText(content, bitmapX / 2, bitmapY / 2 + (fontMetrics.bottom - fontMetrics.top) / 4,
                mPaint);
        canvas.save();
        return bitmapDrawable;
    }

 

文章最后发布于: 2018-07-02 09:50:11
展开阅读全文
0 个人打赏

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览