低級錯誤的教訓->bitmap轉(zhuǎn)drawable

一,根據(jù)字體內(nèi)容拉伸背景

package com.msyc.onion.utils

import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import com.blankj.utilcode.util.ConvertUtils.dp2px
import com.blankj.utilcode.util.ConvertUtils.sp2px
import com.msyc.onion.R
import java.lang.Math.abs

/**
 * @author: fangyichao
 * @date: 2021/2/3
 */
class BitmapUtil {
    companion object {
        fun setBitmap(context: Context, text: String): Drawable {
            val textPaint = Paint()
            textPaint.style = Paint.Style.FILL
            textPaint.strokeWidth = 8f
            textPaint.textSize = sp2px(15f).toFloat()
            textPaint.textAlign = Paint.Align.CENTER
            //獲取text 大小
            val tBounds = Rect()
            textPaint.getTextBounds(text, 0, text.length, tBounds)

            // 字體padding
            val padding = dp2px(10f)

            val realTextBounds = Rect(0, 0, abs(tBounds.right - tBounds.left),
                    abs(tBounds.top - tBounds.bottom))

            // 整個畫布范圍
            val canvasBounds = Rect(0, 0, realTextBounds.width() + padding * 2, realTextBounds.height() + 2 * padding)

            // 背景
            val bitmap = Bitmap.createBitmap(canvasBounds.width(), canvasBounds.height(), Bitmap.Config.ARGB_8888)
            val canvas = Canvas(bitmap)

            //圖片背景paint
            val bgPaint = Paint()
            bgPaint.style = Paint.Style.FILL

            val bitmapBG = BitmapFactory.decodeResource(context.resources, R.drawable.ic_bg_group_price)
                    .copy(Bitmap.Config.ARGB_8888, true)
            val srcBounds = Rect(0, 0, bitmapBG.width, bitmapBG.height)
            canvas.drawBitmap(bitmapBG, srcBounds, canvasBounds, bgPaint)

            //計算baseline
            val fontMetrics = textPaint.fontMetrics
            val distance = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
            val baseline: Float = canvasBounds.centerY() + distance
            canvas.drawText(text, canvasBounds.centerX().toFloat(), baseline, textPaint)
            return BitmapDrawable(context.resources, bitmap)
        }
    }
}

二,背景大小不變

 /**
     * 文字繪制在圖片上,并返回bitmap對象
     */
    public static Bitmap setTextToImg(Context context, @DrawableRes int drawId, String text) {
        BitmapDrawable icon = (BitmapDrawable) context.getResources().getDrawable(drawId);
//        int width = DensityUtil.sp2px(context, 34);
//        int hight = DensityUtil.sp2px(context, 12);
        //建立一個空的Bitmap
//        Bitmap bitmap = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888);
        Bitmap bitmap = icon.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        // 抗鋸齒
        paint.setAntiAlias(true);
        // 防抖動
        paint.setDither(true);
        paint.setTextSize(DensityUtil.sp2px(context, 9));
        paint.setColor(Color.parseColor("#ffffff"));
        canvas.drawText(text, 0, 0, paint);
        return bitmap;
    }

三、layout轉(zhuǎn)drawable

    public static Drawable LayoutToDrawable(Context context, int layout_id, String text) {
        View view = LayoutInflater.from(context).inflate(layout_id, null);
        TextView textView = (TextView) view.findViewById(R.id.content);
        textView.setText(text);
        int size = (int) textView.getText().length();
        Bitmap bitmap = convertViewToBitmap(context, view, size);
//        Drawable drawable = (Drawable) new BitmapDrawable(bitmap);
        BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
        return drawable;
    }

    private static Bitmap convertViewToBitmap(Context context, View view, int size) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        int width = (int) (size * DeviceUtil.spToPx(12f, context));//根據(jù)字符串的長度顯示view的寬度
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); // 原始大小
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

靈魂咆哮:
一般的轉(zhuǎn)換方式采用如下的方法進行轉(zhuǎn)換:
BitmapDrawable drawable = new BitmapDrawable(bitmap);
但這種方式,轉(zhuǎn)換出來的drawable永遠比bitmap原來的大小不統(tǒng)一(基本上會小于bitmap)

出現(xiàn)此種情況時,采用如下方式即可,因為轉(zhuǎn)換中會存在單位換算,需要傳入轉(zhuǎn)換環(huán)境
Resources resources = context.getResources();
BitmapDrawable drawable = new BitmapDrawable(resources, bitmap);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容