【问题标题】:Scale Image Size in Kotlin Android Studio在 Kotlin Android Studio 中缩放图像大小
【发布时间】: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


【解决方案1】:

您可以使用Glide 调整位图大小,见下文

 Glide.with(requireActivity())
                .override(THUMBNAIL_SIZE, THUMBNAIL_SIZE)
                .load(yourBitmap)
                .into(yourImageView)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2011-07-18
    • 1970-01-01
    • 2016-05-24
    • 2012-07-13
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多