Android - 从图库中选择一张图片,然后调整它的大小以适应 ImageView
Android - Choose an image from gallery and then resize it to fit into ImageView
我正在尝试将图像放入 ImageView 以保持纵横比,它会加载图像但我不知道如何很好地调整它的大小。如果图像太大我想裁剪它,如果它太小我想适合 ImageView,我的代码到目前为止是这样的:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imageButtonFotoPerfil);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
和 imageView 的 XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButtonFotoPerfil"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:onClick="loadImagefromGallery"
android:src="@drawable/perfil_persona_b"
android:background="@null"/>
将 layout_width
、hegiht
更改为 100dp
、120dp
您想要什么尺寸,或者您可以根据屏幕尺寸计算图像尺寸。
我正在尝试将图像放入 ImageView 以保持纵横比,它会加载图像但我不知道如何很好地调整它的大小。如果图像太大我想裁剪它,如果它太小我想适合 ImageView,我的代码到目前为止是这样的:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imageButtonFotoPerfil);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
和 imageView 的 XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButtonFotoPerfil"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:onClick="loadImagefromGallery"
android:src="@drawable/perfil_persona_b"
android:background="@null"/>
将 layout_width
、hegiht
更改为 100dp
、120dp
您想要什么尺寸,或者您可以根据屏幕尺寸计算图像尺寸。