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

前端 未结 25 1838
孤独总比滥情好
孤独总比滥情好 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:02

    this solved my problem

    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    
    0 讨论(0)
  • 2020-11-22 05:02

    Try using android:layout_gravity for ImageView:

    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_weight="1"
    

    The example above worked for me.

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

    If image quality decreases in: use

    android:adjustViewBounds="true"
    

    instead of

    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    
    0 讨论(0)
  • 2020-11-22 05:04
    imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));
    
    0 讨论(0)
  • 2020-11-22 05:05

    Below code Working for scale image as aspect ratio:

    Bitmap bitmapImage = BitmapFactory.decodeFile("Your path");
    int nh = (int) ( bitmapImage.getHeight() * (512.0 / bitmapImage.getWidth()) );
    Bitmap scaled = Bitmap.createScaledBitmap(bitmapImage, 512, nh, true);
    your_imageview.setImageBitmap(scaled);
    
    0 讨论(0)
  • 2020-11-22 05:05

    Use these properties in ImageView to keep aspect ratio:

    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        />
    
    0 讨论(0)
提交回复
热议问题