Home Why won't my ImageView scale to fill the layout as much as the width permits?
Reply: 3

Why won't my ImageView scale to fill the layout as much as the width permits?

K_7
1#
K_7 Published in 2017-12-07 21:33:35Z

What I want is for the ImageView to scale with the same ratio to fill the layout without being cropped. Since the width is going to match first, effectively, this means scaling until the width is maximized.

My code is:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    ImageView img = findViewById(R.id.displayImg);
    img.setImageBitmap(bmp); // bmp is a public static Bitmap set by another class
    img.setRotation(90);
    img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}

My layout is:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>

As per Android ImageView ScaleType: A Visual Guide CENTER_INSIDE should

Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

The black image is what I'm currently, and I've drawn what I believe I should be seeing instead (as you can tell, I'm the modern-day Michelangelo).

DroiDev
2#
DroiDev Reply to 2017-12-07 21:43:10Z

wrap_content if the reason why.... change that to fill_parent or match_parent.

Take a look at this: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

CENTER_INSIDE Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

If the ratio of your image is off, it wont stretch.

Java

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

XML

android:scaleType="fitCenter"

side note: fillXY might be better.

petey
3#
petey Reply to 2017-12-08 14:59:28Z

You may acheive your desired result by making the dimensions of the ImageView match to the parent layout and apply fitCenter as a scaleType so it fills the view without losing aspect ratio:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:id="@+id/displayImg" />
</android.support.constraint.ConstraintLayout>
Turgay Mutlu
4#
Turgay Mutlu Reply to 2017-12-07 22:32:21Z

I think you use wrong value. Try android:scaleType="fitXY" on xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/displayImg"
        android:scaleType="fitXY" />

</android.support.constraint.ConstraintLayout>

You don't need set again scaleType on activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    ImageView img = findViewById(R.id.displayImg);
    img.setImageBitmap(bmp); // bmp is a public static Bitmap set by another class
    img.setRotation(90);
    //img.setScaleType(ImageView.ScaleType.ImageView.ScaleType.FIT_XY); You don't need this 
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313616 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO