我正在通过从图库中选择图像来设置帐户的个人资料图像,如下所示
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.profile_image :
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY && resultCode != 0) {
profileImage.setImageBitmap(null);
if (Image != null)
Image.recycle();
Uri mImageUri = data.getData();
try {
Image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageUri);
profileImage.setImageBitmap(Image);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这很完美,但有一个问题,图像的高度和宽度会随着不同的图像而变化。我想要的是将图像大小和宽度固定为 150 dp? 以下是我的 xml 代码
<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="@android:drawable/sym_def_app_icon"
android:clickable="true"/>
最佳答案
您可以使用“centerCrop”scaleType来实现此目的。
<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="centerCrop"/>
其他比例类型有:
中心
将图像置于 View 中央,但不执行缩放。
CENTER_CROP
均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)等于或大于 View 的相应尺寸(减去填充)。
CENTER_INSIDE
均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)等于或小于 View 的相应尺寸(减去填充)。
FIT_CENTER
使用 CENTER 缩放图像。
FIT_END
使用 END 缩放图像。
FIT_START
使用 START 缩放图像。
FIT_XY
使用 FILL 缩放图像。
矩阵
绘图时使用图像矩阵进行缩放。
关于java - 安卓 :how to fixed size of image view programatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41342358/