Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android picasso scale photos to be bigger or fit the imageView (no white space)

I have a serious problem with the Picasso library scaling. The problem is that I have a layout consisting of 3 Main views and some buttons. Views are like :

ImageView 50% mapView 20% EditTextView 30%

And I want to load pictures of unknown resolution into that imageView without leaving white spaces. I was trying to use .fit().centerInside but that just left white spaces on the sides. This is how my layout looks like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/photo1"
        android:weightSum="10">

        <ImageView
        android:id="@+id/photo1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="5"
        android:background="#75aea7ab" />

        <com.google.android.gms.maps.MapView
            android:id="@+id/map2"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="2" />

        <EditText
            android:id="@+id/edittext"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:layout_weight="3"
            android:background="#00000000"
            android:gravity="top|left"
            android:inputType="textMultiLine"
            android:textColor="#ff000000"
            android:textSize="15sp" />
    </LinearLayout>
<ImageButton
    android:id="@+id/post"
    android:layout_width="@dimen/fab_button_diameter"
    android:layout_height="@dimen/fab_button_diameter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="end"
    android:layout_marginBottom="300dp"
    android:background="@drawable/fab_shape"
    android:src="@drawable/forward"
    android:text="Post!"
    android:tint="@android:color/white" />

<Button
    android:id="@+id/camera"
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="75dp"
    android:layout_marginTop="100dp"
    android:background="@drawable/camera" />

<Button
    android:id="@+id/gallery"
    android:layout_width="72dp"
    android:layout_height="72dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="75dp"
    android:layout_marginTop="100dp"
    android:background="@drawable/gallery" />

like image 956
Lukas Anda Avatar asked Jul 05 '15 17:07

Lukas Anda


People also ask

How do you fit a picture on a Picasso android?

There are two things to know about fit() . First, calling fit() can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit() with an ImageView as the target (we'll look at other targets later).

How do I resize an image in ImageView to keep 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.

How do I make an image fit in ImageView?

try adding android:scaleType="fitXY" to your ImageView . This will modify the aspect ratio if the original image is not squared. fitXY will almost always change the aspect ratio of the image.

How do I resize an image in Picasso?

The discussed options should cover your needs for functionality regarding image resizing and scaling. There is one last helper functionality of Picasso, which can be very useful: fit (). fit () is measuring the dimensions of the target ImageView and internally uses resize () to reduce the image size to the dimensions of the ImageView.

How to scale an image in imageview to keep the aspect ratio?

This example demonstrates how to scale an Image in ImageView to keep the aspect ratio in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What is the advantage of using fit() over image in Picasso?

First, calling fit () can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit () with an ImageView as the target (we'll look at other targets later). The advantage is that the image is at the lowest possible resolution, without affecting its quality.

What is proportional image resizing in Android?

Proportional image resizing is a fairly common scenario while developing an Android app: there are a number of situations where you might want an image to stre Android: proportionally stretch an ImageView to fit the whole screen width while maintaining its aspect ratio


1 Answers

Have you try to set the ScaleType after the image has been loaded?

   mPicasso.load("yourUrl")
                    .into(yourImageView, new Callback() {
                        @Override
                        public void onSuccess() {
                            yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);//Or ScaleType.FIT_CENTER
                        }
                        @Override
                        public void onError() {
                            //nothing for now
                        }
                    });
like image 125
Joel Avatar answered Sep 25 '22 15:09

Joel