圖片內加數字

    /**
     * 圖片內加數字
     *
     * @param resourcesId .
     * @param count       .
     */
    private Bitmap addNumbersToPicture(Context context,int resourcesId, int count) {
        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.WHITE);
        }
        if (count < 10) {
            mPaint.setTextSize(Utils.sp2px(context,16));
        } else {
            mPaint.setTextSize(Utils.sp2px(context,12));
        }
        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(String.valueOf(count), bitmapX / 2, bitmapY / 2 + (fontMetrics.bottom - fontMetrics.top) / 4,
                mPaint);
        canvas.save();
        return bitmapDrawable;

    }

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