How to scale an Image in ImageView to keep the aspect ratio

前端 未结 25 1806
孤独总比滥情好
孤独总比滥情好 2020-11-22 04:59

In Android, I defined an ImageView\'s layout_width to be fill_parent (which takes up the full width of the phone).

If the imag

相关标签:
25条回答
  • 2020-11-22 05:05

    I use this:

    <ImageView
    android:id="@+id/logo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:scaleType="centerInside"
    android:src="@drawable/logo" />
    
    0 讨论(0)
  • 2020-11-22 05:06
    1. Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)

    2. You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.

      Then as Samuh wrote, you can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.

    0 讨论(0)
  • 2020-11-22 05:06

    I have an image smaller than the screen. To have it stretched proportionally to the max and centered in the view I had to use the following code:

    <ImageView
        android:id="@+id/my_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        android:scaleType="fitCenter" />
    

    Have in mind though, that if you have a relative layout and have some elements set to be above or below the ImageView, they will be most likely overlapped by the image.

    0 讨论(0)
  • 2020-11-22 05:08

    Pass your ImageView and based on screen height and width you can make it

        public void setScaleImage(EventAssetValueListenerView view){
            // Get the ImageView and its bitmap
            Drawable drawing = view.getDrawable();
            Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
            // Get current dimensions
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
    
            float xScale = ((float) 4) / width;
            float yScale = ((float) 4) / height;
            float scale = (xScale <= yScale) ? xScale : yScale;
    
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale);
    
            Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
            BitmapDrawable result = new BitmapDrawable(scaledBitmap);
            width = scaledBitmap.getWidth();
            height = scaledBitmap.getHeight();
    
            view.setImageDrawable(result);
    
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
            params.width = width;
            params.height = height;
            view.setLayoutParams(params);
        }
    

    0 讨论(0)
  • 2020-11-22 05:09

    This is how it worked for me inside a ConstraintLayout:

    <ImageView
        android:id="@+id/myImg"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"/>
    

    Then in code, I set the drawable as:

    ImageView imgView = findViewById(R.id.myImg);
    imgView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.image_to_show, null));
    

    This fits the image nicely according to its aspect ratio and keeps it in centre.

    0 讨论(0)
  • 2020-11-22 05:09

    in case of using cardviewfor rounding imageview and fixed android:layout_height for header this worked for me to load image with Glide

        <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:tools="http://schemas.android.com/tools"
                 android:layout_width="match_parent"
                 android:layout_height="220dp"
                 xmlns:card_view="http://schemas.android.com/apk/res-auto"
                 >
    
        <android.support.v7.widget.CardView
                android:id="@+id/card_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|top"
                card_view:cardBackgroundColor="@color/colorPrimary"
                card_view:cardCornerRadius="10dp"
                card_view:cardElevation="10dp"
                card_view:cardPreventCornerOverlap="false"
                card_view:cardUseCompatPadding="true">
    
            <ImageView
                    android:adjustViewBounds="true"
                    android:maxHeight="220dp"
                    android:id="@+id/iv_full"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="fitCenter"/>
    
        </android.support.v7.widget.CardView>
    </FrameLayout>
    
    0 讨论(0)
提交回复
热议问题