android图像不保持纵横比

标签 android bitmap scaling

你好,我是安卓新手。谁能帮我保持图像的纵横比。在我的应用程序中,我从 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/

相关文章:

java - 如何在 Android 中使用 Jackson 从 JSONArray url 获取 java 对象

java - 单击任意位置时关闭 Activity

java - 什么是 setTimeOut() javascript 到 Android 的等价物?

c# - 为什么 FastBitmap 没有被垃圾回收?

android - 在哪个 android 版本中可以使用草书字体?

java - Skia项目运行在Android.4.2上,Android 4.2和Android 2.2中的定义是否存在差异?

java - 检查文件长度后在 Android 中进行位图采样

javascript - 为什么canvas的drawImage缩放比例不一致?

wpf - Visual Studio 2010 - WPF - 背景图像缩放问题

cocoa - 缩放自定义页面后如何避免裁剪?