美文网首页
低级错误的教训->bitmap转drawable

低级错误的教训->bitmap转drawable

作者: 颤抖的闪电 | 来源:发表于2021-02-03 14:17 被阅读0次

    一,根据字体内容拉伸背景

    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转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));//根据字符串的长度显示view的宽度
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); // 原始大小
            view.buildDrawingCache();
            Bitmap bitmap = view.getDrawingCache();
            return bitmap;
        }
    

    灵魂咆哮:
    一般的转换方式采用如下的方法进行转换:
    BitmapDrawable drawable = new BitmapDrawable(bitmap);
    但这种方式,转换出来的drawable永远比bitmap原来的大小不统一(基本上会小于bitmap)

    出现此种情况时,采用如下方式即可,因为转换中会存在单位换算,需要传入转换环境
    Resources resources = context.getResources();
    BitmapDrawable drawable = new BitmapDrawable(resources, bitmap);

    相关文章

      网友评论

          本文标题:低级错误的教训->bitmap转drawable

          本文链接:https://www.haomeiwen.com/subject/pioptltx.html