圖片內加字

    /**
     * 圖片內加字
     */
    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;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章