如何解决如何使用 Kotlin 在 Android 应用程序中显示图像的整个部分而不会裁剪或丢失纵横比?
我设置了一个可以上传产品图片的活动。 android:id="@+id/iv_add_update_product"
用作上传图片的按钮,android:id="@+id/iv_product_image"
是我希望我的图片出现的位置。它工作正常,除了我的图像不完美。我的意思是部分图像被裁剪。我想要的是在不裁剪或丢失纵横比的情况下完全显示我的图像。
注意:我使用 XML 代码添加的图像完全显示在 imageView @+id/iv_product_image
中,没有裁剪或丢失纵横比。
我在 XML 中和上传时使用了相同的图像。
以下是我的 XML 代码的一部分。我想为 iv_product_image 做。
<FrameLayout
android:id="@+id/fl_product_image"
android:layout_width="match_parent"
android:layout_height="@dimen/add_product_header_image_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/iv_product_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image_boat"
android:contentDescription="@string/content_description"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>
<ImageView
android:id="@+id/iv_add_update_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:contentDescription="@string/content_description"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:padding="@dimen/add_update_product_icon_padding"
android:src="@drawable/ic_vector_add_photo" />
</FrameLayout>
添加ProductActivity.kt
override fun onClick(v: View?) {
if (v != null) {
when (v.id) {
R.id.iv_add_update_product -> {
if (ContextCompat.checkSelfPermission(
this,Manifest.permission.READ_EXTERNAL_STORAGE
)
== PackageManager.PERMISSION_GRANTED
) {
Constants.showImageChooser(this@AddProductActivity)
} else {
ActivityCompat.requestPermissions(
this,arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),Constants.READ_STORAGE_PERMISSION_CODE
)
}
}
R.id.btn_submit -> {
if (validateProductDetails()) {
uploadProductImage()
}
}
}
}
}
常量.kt
fun showImageChooser(activity: Activity) {
val galleryIntent = Intent(
Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI
)
activity.startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST_CODE)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。