gpt4 book ai didi

java - Bitmap.decodeResource 调整图像大小

转载 作者:行者123 更新时间:2023-11-30 11:39:57 25 4
gpt4 key购买 nike

我有这种方法,可以在图像上绘制一些文本:

public BitmapDrawable writeOnDrawable(int drawableId, String text){

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);

Typeface mFace = Typeface.createFromAsset(this.getAssets(),"fonts/progbot.ttf");

Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
paint.setTypeface(mFace);
paint.setTextSize(30);

Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);

return new BitmapDrawable(bm);
}

我是这样调用这个方法的:

lineIconView.setImageDrawable(writeOnDrawable(R.drawable.icon_line_numbre, linea.getNumero()));

此 ImageView (lineIconView) 已具有 R.drawable.icon_line_numbre 资源集。如果我不调用 writeOnDrawable 函数,那么图像将以其原始大小显示,但在调用它之后图像的大小会大大减小。

这是正常行为吗?

最佳答案

原来我为 BitmapDrawable 使用了错误的构造函数。

官方文档对此事的说明如下:

BitmapDrawable(Bitmap bitmap) This constructor is deprecated. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.

所以我最终使用了:

return new BitmapDrawable(bm);

关于java - Bitmap.decodeResource 调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12975444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com