我從圖庫中選擇圖片如下android:如何以編程方式固定大小的圖像視圖?
@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"/>
請參閱[此](http://stackoverflow.com/a/41038654/5460053)調整圖像大小。 –