android镜像没有保持纵横比



你好,我是安卓系统的新手。有人能帮我保持图像的纵横比吗。在我的应用程序中,我从SDCARD中选择图像,并通过将意图传递到下一个活动来在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);

在其他活动中,我通过使用以下内容来理解这一点。

    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的Scaling属性在您的xml文件中使用此属性为ImageView 使用缩放类型

 android:adjustViewBounds="true"
 android:scaleType="fitXY" or  android:scaleType="centerCrop"

在xml或活动中将ImageView的ScaleType属性设置为"CenterInside"。

my_image.setScaleType(ScaleType.CENTER_INSIDE);

请参阅ScaleType 的更多信息

显示位图有时会很棘手。

应该非常仔细地遵循本教程:http://developer.android.com/training/displaying-bitmaps/index.html

最新更新