你好,我是安卓新手。谁能帮我保持图像的纵横比。在我的应用程序中,我从 SDCARD 中选择图像,并通过将 Intent 传递给下一个 Activity 来在 ImageView 中显示它。但是大多数时候,当图像变大或变小时,图像无法在我的 ImageView 中正确显示。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16 * 1024];
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(mImageCaptureUri));
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 120,
120, true);
newbitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
Intent photointent = new Intent(MethinkzActivity.this,
DisplayImage.class);
photointent.putExtra("photo", bs.toByteArray());
startActivity(photointent);
在其他 Activity 中,我通过以下方式捕捉到这一点。
bitmap = BitmapFactory.decodeByteArray(getIntent()
.getByteArrayExtra("photo"), 0, getIntent()
.getByteArrayExtra("photo").length);
final double viewWidthToBitmapWidthRatio = (double) my_image
.getWidth() / (double) bitmap.getWidth();
my_image.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
my_image.setImageBitmap(bitmap);
请帮我找到这个。
最佳答案
您应该为 ImageView 使用 ImageView 的缩放属性,在您的 xml 文件中使用此属性,为 ImageView 使用一种缩放类型
android:adjustViewBounds="true"
android:scaleType="fitXY" or android:scaleType="centerCrop"
关于android图像不保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11681630/