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" />
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).
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.
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.
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.
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.
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.
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
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With