一,根據(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);