Fresco: force Image fill all part of SimpleDraweeView area

Created on 30 Sep 2016  路  14Comments  路  Source: facebook/fresco

Hi,
When I load an image into SimpleDraweeView it tries to show all image in it so images corners remain emply, How can I force it to scale image to fill whole area?
I tried most of fresco:actualImageScaleType but nothing changes at all.here is my layout

                    <com.facebook.drawee.view.SimpleDraweeView
                        android:id="@+id/img_main"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        fresco:placeholderImage="@drawable/backgrondlayout2"
                        fresco:actualImageScaleType="centerInside" />

where I am doing wrong?
thanks

needs-details question

All 14 comments

You have to set a different scale type. centerCrop should work in your case.

Hi @oprisnik ,
I thing there is somthing wrong with it..This is my layout


`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">



    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="0dp"
        card_view:cardPreventCornerOverlap="false">



        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1"
            android:gravity="center|center_horizontal">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:focusableInTouchMode="false"
                android:focusable="false"
                android:baselineAligned="false">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="190dp"
                    android:layout_weight="0.10">
                    <com.facebook.drawee.view.SimpleDraweeView
                        android:id="@+id/img_main"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        fresco:placeholderImage="@drawable/backgrondlayout2"
                        fresco:actualImageScaleType="centerCrop" />

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:background="#42000000"
                        android:layout_gravity="bottom|right"
                        tools:ignore="RtlHardcoded">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
                            android:text="Header"
                            android:transitionName="title"
                            android:id="@+id/txt_header"
                            android:layout_gravity="left|center_vertical"
                            android:gravity="right"
                            android:padding="@dimen/textPadding"
                            android:paddingRight="@dimen/textPadding"
                            tools:ignore="RtlSymmetry"
                            android:textColor="@color/off_white" />
                    </FrameLayout>

                </FrameLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="90dp"
                    android:weightSum="1"
                    android:layout_weight="0.0001"
                    android:baselineAligned="false">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="109dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.10">
                        <com.facebook.drawee.view.SimpleDraweeView
                            android:id="@+id/img_logo"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                           />

                    </LinearLayout>

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.90">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small"
                            android:text="Discription"
                            android:id="@+id/txt_discription"
                            android:paddingRight="4dp"
                            android:gravity="right"
                            tools:ignore="RtlHardcoded,RtlSymmetry" />
                    </LinearLayout>

                </LinearLayout>
                <ImageView
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:src="@android:drawable/divider_horizontal_textfield"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:paddingBottom="1dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" />
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:weightSum="2"
                    android:layout_weight="0.99"
                    android:gravity="right|center_horizontal"
                    tools:ignore="RtlHardcoded"
                    android:baselineAligned="false">

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:weightSum="1"
                        android:layout_weight="1.75"
                        android:gravity="center_vertical|center_horizontal">

                        <ImageView
                            android:id="@+id/img_distance_icon"
                            android:contentDescription=""
                            android:layout_height="24dp"
                            android:layout_width="24dp"
                            tools:layout_width="24dp"
                            android:background="@drawable/ic_directions_black_24dp" />
                    </LinearLayout>

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="52dp"
                        android:layout_weight="0.35"
                        android:gravity="center|right">

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="0.0 km"
                            android:id="@+id/txt_distance"
                            android:layout_weight="0.77"
                            android:gravity="right"
                            android:paddingRight="@dimen/textPadding"
                            android:textAlignment="viewStart"
                            android:paddingLeft="@dimen/textPadding"
                            android:maxLines="1" />

                        <TextView
                            android:layout_width="100dp"
                            android:layout_height="match_parent"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:text="Indoor"
                            android:id="@+id/txt_indoor"
                            android:gravity="center|right"
                            android:textColor="#e00b0b"
                            android:textSize="18sp"
                            android:paddingRight="4dp"
                            android:textAlignment="viewStart"
                            tools:ignore="RtlHardcoded,RtlSymmetry" />

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout></android.support.v7.widget.CardView>

</LinearLayout>`

as you can see every thing is ok.But this is result
photo545422304011725117

That code works for me. Just made a sample app with the following code:
test.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical">


    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="16dp"
        card_view:cardCornerRadius="2dp"
        card_view:cardPreventCornerOverlap="false"
        card_view:contentPadding="0dp">


        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center|center_horizontal"
            android:orientation="horizontal"
            android:weightSum="1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:orientation="vertical"
                android:weightSum="1">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="190dp"
                    android:layout_weight="0.10">

                    <com.facebook.drawee.view.SimpleDraweeView
                        android:id="@+id/img_main"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        fresco:actualImageScaleType="centerCrop"
                        fresco:placeholderImage="@mipmap/ic_launcher"/>

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_gravity="bottom|right"
                        android:background="#42000000"
                        tools:ignore="RtlHardcoded">

                        <TextView
                            android:id="@+id/txt_header"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="left|center_vertical"
                            android:gravity="right"
                            android:padding="8dp"
                            android:text="Header"
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
                            android:textColor="#ffffff"
                            android:transitionName="title"
                            tools:ignore="RtlSymmetry"/>
                    </FrameLayout>

                </FrameLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="90dp"
                    android:layout_weight="0.0001"
                    android:baselineAligned="false"
                    android:orientation="horizontal"
                    android:weightSum="1">

                    <LinearLayout
                        android:layout_width="109dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.10"
                        android:orientation="vertical">

                        <com.facebook.drawee.view.SimpleDraweeView
                            android:id="@+id/img_logo"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="0.90"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/txt_discription"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:gravity="right"
                            android:paddingRight="4dp"
                            android:text="Discription"
                            android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small"
                            tools:ignore="RtlHardcoded,RtlSymmetry"/>
                    </LinearLayout>

                </LinearLayout>

                <ImageView
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:paddingBottom="1dp"
                    android:scaleType="fitXY"
                    android:src="@android:drawable/divider_horizontal_textfield"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.99"
                    android:baselineAligned="false"
                    android:gravity="right|center_horizontal"
                    android:orientation="horizontal"
                    android:weightSum="2"
                    tools:ignore="RtlHardcoded">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1.75"
                        android:gravity="center_vertical|center_horizontal"
                        android:orientation="horizontal"
                        android:weightSum="1">

                        <ImageView
                            android:id="@+id/img_distance_icon"
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:background="#ffff00"
                            android:contentDescription=""
                            tools:layout_width="24dp"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="52dp"
                        android:layout_weight="0.35"
                        android:gravity="center|right"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/txt_distance"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.77"
                            android:gravity="right"
                            android:maxLines="1"
                            android:paddingLeft="8dp"
                            android:paddingRight="8dp"
                            android:text="0.0 km"
                            android:textAlignment="viewStart"
                            android:textAppearance="?android:attr/textAppearanceSmall"/>

                        <TextView
                            android:id="@+id/txt_indoor"
                            android:layout_width="100dp"
                            android:layout_height="match_parent"
                            android:gravity="center|right"
                            android:paddingRight="4dp"
                            android:text="Indoor"
                            android:textAlignment="viewStart"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="#e00b0b"
                            android:textSize="18sp"
                            tools:ignore="RtlHardcoded,RtlSymmetry"/>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Fresco.initialize(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        SimpleDraweeView sdv = (SimpleDraweeView) findViewById(R.id.img_main);
        sdv.setImageURI("http://lorempixel.com/400/400/sports/2/");
    }
}

Result:

screenshot_1475277400

So it seems that something else is not working for you?

Hi,
Thanks for your response, So it seems my layout it self works fine. At first I thought that the recycleview that I load cards into it produces this problem,so I created a new layout from your provided xml and I changed fresco:placeholderImage="@drawable/backgrondlayout2" and put a big image in it. and in design mode in android studio it shoes with problem. even when I dont load any image into control it does not change scale in runtime and even in design time.
capture

photo545422304011725120

Do you think problem is with android studion or things like that?
I have used this version
compile 'com.facebook.fresco:fresco:0.14.0'
what other information shall I provide?

How do you set the image URL?

Hi,
In this image I set it manually using
fresco:placeholderImage="@drawable/backgrondlayout2"
this image

and in this one I set is using a recycleview adapter
This one
this is my adapter,Both img_mainand img_logohave the same problem

`
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;

import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by GIS on 28/09/2016.
 */

public class mal_list_card_adapter
        extends RecyclerView.Adapter<mal_list_card_adapter.ViewHolder> {


    List<Mall> items;
    Context context; //global
    public mal_list_card_adapter(Context context,List<Mall> malls) {
        super();
        this.context = context;
        items =malls;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.shop_card, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Mall list =  items.get(position);
      //  holder.img_main.setImageURI(list.getCoverURI());

        holder.img_logo.setImageURI(list.getLogoURI());
        holder.txt_header.setText(list.getTitle());
        holder.txt_discriptionn.setText(list.getDescription());
        holder.txt_distance.setText(list.getCityTitle());
        holder.card.setTag(R.id.TAG_MALL_ID,list);
        holder.card.setTag(R.id.TAG_MALL_TITLE,holder.txt_header);
        holder.card.setTag(R.id.TAG_MALL_COVER,holder.img_main);

        holder.card.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(context==null){
                            return;
                        }
                        Intent intent = new Intent(context, ScrollingActivity.class);
                        Bundle b = new Bundle();
                        b.putParcelable("EXTRA_MALL",((Mall)v.getTag(R.id.TAG_MALL_ID)));
                        intent.putExtras(b);

                      //  Pair<View, String> p1 = Pair.create((View)v.getTag(R.id.TAG_MALL_TITLE), "profile");
                        Pair<View, String> p2 = Pair.create((View)v.getTag(R.id.TAG_MALL_COVER), "title");

//                        ActivityOptionsCompat options = ActivityOptionsCompat.
//                                makeSceneTransitionAnimation((Activity)context, p1, p2);
                        ActivityOptionsCompat options =
                                ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context,
                                        (View)v.getTag(R.id.TAG_MALL_COVER),   // Starting view
                                        "profile"    // The String
                                                    );
                        //Start the Intent
                        ActivityCompat.startActivity((Activity) context, intent, options.toBundle());


                    }
                }
        );
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public SimpleDraweeView img_main;
        public SimpleDraweeView img_logo;
        public TextView txt_header;
        public TextView txt_discriptionn;
        public TextView txt_distance;
        public CardView card;

        public ViewHolder(View itemView) {
            super(itemView);
            card = (CardView) itemView.findViewById(R.id.card_view);
            img_logo = (SimpleDraweeView) itemView.findViewById(R.id.img_logo);
            img_main = (SimpleDraweeView) itemView.findViewById(R.id.img_main);
            txt_header = (TextView) itemView.findViewById(R.id.txt_header);
            txt_discriptionn = (TextView) itemView.findViewById(R.id.txt_discription);
            txt_distance = (TextView) itemView.findViewById(R.id.txt_distance);
        }
    }
}
`

I changed it into an image control and it worked fine..It seems there is sth wrong with fresco..

So it doesn't work for the placeholder image? If so, you have to use placeholderImageScaleType instead of actualImageScaleType in XML (and the corresponding method in Java).

@oprisnik thanks for your answer,In fact it does not work either for placeholderimage and when I set is using setImageURI(URI);.I have tried with both of them and thay dont work

Since it still works for me with the sample code above, I would say the problem is somewhere else in your code. A minimal example to reproduce the issue would be nice. Otherwise just go from the example and gradually add functionality until it fails - then you know where the issue is.

I looked a bit more and It seems it is some kind of bugs with android even, if you put your layout into a recycleview adapter it will cause that scale mod does not work

As a workaround, you could try setting an aspect ratio fresco:viewAspectRatio="1.23" or in Java with drawee.setAspectRatio(1.23)?

I assume the problem has been resolved. Please let us know if you're still experiencing this issue.

I got this problem by use Fresco which the version is 1.11.0, event if I set placeholderImage="xxx" and placeholderImageScaleType="centerCrop". And another interesting thing is that I found the problem on Pixel 2 XL everytime, but didn't reproduce on Pixel 2.

Someone said he has fix it by provide all type file in drawable, rather than only in 'drawable-xxhdpi'. I didn't test it until now, but tomorrow I will try it and update this comment.

Same issue #2192

Was this page helpful?
0 / 5 - 0 ratings