【发布时间】:2020-01-22 11:15:00
【问题描述】:
我从 192x192 大小的 api 接收 base64 图像。我正在使用以下代码将 base64 数据更改为位图。
fun Base64ToBitmap(myImageData: String): Bitmap {
val imageAsBytes = Base64.decode(myImageData.toByteArray(), Base64.DEFAULT)
return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.size)
}
然后我用下面的代码来替换我之前的ImageView的内容。
ImageHeader.setImageBitmap(img)
该 ImageHeader 的 xml 如下。
<ImageView
android:id="@+id/ImageHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:contentDescription="@string/ImageHeader1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/text_home"
app:srcCompat="@drawable/defaultpng" />
正如预期的那样,这会在应用程序上生成一个非常小的图像。无论如何我可以缩放它的大小 x2 或 x4 而不必从我的基本 php api 创建一个更大的图像?通过 API 发送额外数据似乎是一种浪费。
【问题讨论】:
-
查看这个函数 >>> private Drawable resizeDrawable(Drawable drawable, int dstWidth, int dstHeight) { Drawable resizedDrawable = null;尝试 { 位图位图 = ((BitmapDrawable) 可绘制).getBitmap();位图 bitmapResized = Bitmap.createScaledBitmap(bitmap, dstWidth, dstHeight, false); resizedDrawable = new BitmapDrawable(context.getResources(), bitmapResized).mutate(); } 捕捉(异常 e){ e.printStackTrace(); resizedDrawable = new ColorDrawable(Color.TRANSPARENT); } 返回 resizedDrawable;}
标签: android android-studio kotlin png