具体如何在ImageView中显示图片

How to display picture in the ImageView in the specific way

我有一个 ImageView(正方形),我需要以特定方式显示图片,如果图片是垂直方向拍摄的,则左右两侧应该有 space广场的(图1),如果照片是横向拍摄的,广场的顶部和底部应该有space。怎么可能那样做?也许为此存在一些特殊的 ScaleType?

图片1:

图片二:

希望这个例子能帮到你:

//OnActivityResult方法

                    Uri selectedImage = data.getData();
                    if (selectedImage == null) {
                        imagePath = data.getStringExtra(GOTOConstants.IntentExtras.IMAGE_PATH);

                        Bitmap my_bitmap_camera = BitmapFactory.decodeFile(imagePath);

                        ExifInterface exif = new ExifInterface(imagePath);
                        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

                        switch (orientation){
                            case ExifInterface.ORIENTATION_ROTATE_90:
                                Matrix matrix = new Matrix();
                                matrix.postRotate(90);
                                my_bitmap_camera = Bitmap.createBitmap(my_bitmap_camera, 0, 0, my_bitmap_camera.getWidth(), my_bitmap_camera.getHeight(), matrix, true);
                                break;
                        }

                        ivScreenshot1.setImageBitmap(my_bitmap_camera);
                    }

//XML

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/ivScreenshot1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>

            </RelativeLayout>

//你也可以检查这个post: How to get the Correct orientation of the image selected from the Default Image gallery