Hamza Kalash
Hamza Kalash

Reputation: 11

Center crop scale type not working in android image view

I'm trying to make the img to centerCrop but it doesn't work what ever i do !! I used both Picasso and Glide and the result is the same, And i searched google, github and stackoverflow and i find many cases like mine but no solution work for me .. any help pleas! this is my xml code :

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="6dp"
    app:cardCornerRadius="15dp"
    app:cardElevation="0dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <androidx.cardview.widget.CardView
            android:id="@+id/item_audio_iv_cv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="10dp"
            app:cardCornerRadius="15dp"
            app:cardElevation="7dp">

            <ImageView
                android:id="@+id/item_audio_iv"
                android:layout_width="55dp"
                android:layout_height="55dp"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher" />

        </androidx.cardview.widget.CardView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_toEndOf="@id/item_audio_iv_cv"
            android:layout_toRightOf="@id/item_audio_iv_cv"
            android:orientation="vertical"
            android:paddingRight="12dp"
            android:paddingLeft="12dp"
            android:paddingTop="5dp">

            <TextView
                android:id="@+id/item_audio_title_tv"
                style="@style/TextAppearance.AppCompat.Body1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/item_audio_artist_tv"
                style="@style/TextAppearance.AppCompat.Widget.ActionMode.Subtitle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"
                android:paddingTop="3dp"
                android:textSize="13sp" />

        </LinearLayout>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

and this is my adapter code where i used both Glide and Picasso and they didnot work :

package com.abc.ump.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.abc.ump.R;
import com.abc.ump.classes.Audio;
import com.abc.ump.utils.AppUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class AudiosAdapter extends RecyclerView.Adapter<AudiosAdapter.ViewHolder> {
    private Context mContext;
    private ArrayList<Audio> mAudios;
    private long[] mAudioIds;

    public AudiosAdapter(Context context, ArrayList<Audio> audios) {
        this.mContext = context;
        this.mAudios = audios;
        this.mAudioIds = getAudioIds();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_audio, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.audioTitle.setText(mAudios.get(position).getTitle());
        holder.audioArtist.setText(mAudios.get(position).getArtist());

        Picasso.get()
                .load(AppUtils.getAlbumArtUri(mAudios.get(position).getAlbumId()))
                .placeholder(R.mipmap.ic_launcher)
                .centerCrop().fit()
                .into(holder.audioArt);

//        Glide.with(mContext)
//                .applyDefaultRequestOptions(new RequestOptions()
//                        //TODO:set default image
//                        .placeholder(R.mipmap.ic_launcher)
//                        .error(R.mipmap.ic_launcher))
//                .asBitmap()
//                .load(AppUtils.getAlbumArtUri(mAudios.get(position).getAlbumId()))
//                .centerCrop()
//                .into(holder.audioArt);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView audioArt;
        private TextView audioTitle, audioArtist;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            audioArt = itemView.findViewById(R.id.item_audio_iv);
            audioTitle = itemView.findViewById(R.id.item_audio_title_tv);
            audioArtist = itemView.findViewById(R.id.item_audio_artist_tv);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            // TODO : change destination fragment type and send some info
//            FragmentManager fragmentManager = ((AppCompatActivity) mContext).getSupportFragmentManager();
//            AudiosFragment audiosFragment = new AudiosFragment();
//            fragmentManager.beginTransaction().hide(fragmentManager.findFragmentById(R.id.base_container))
//                    .add(R.id.base_container, audiosFragment).addToBackStack(null).commit();
        }
    }

    public void updateDataSet(ArrayList<Audio> newAudios) {
        mAudios = newAudios;
        mAudioIds = getAudioIds();
    }
    public long[] getAudioIds() {
        long[] temp = new long[mAudios.size()];
        for (int i = 0; i < getItemCount(); i++) {
            temp[i] = mAudios.get(i).getId();
        }

        return temp;
    }
}

and this is what i got every time :

pic1

but this is what i want to do :

pic2

sorry for my bad lang i hope you understand my problem and can help me..

and thanks!

Upvotes: 1

Views: 665

Answers (2)

Hamza Kalash
Hamza Kalash

Reputation: 11

It was just because i'm using api less than 21 so the clipToOutline not working for old versions so that the image view was not able to take the corner radios from the cardView

Thanks for every one 3>

Upvotes: 0

Ali Moghadam
Ali Moghadam

Reputation: 1156

Pay attention to use image without white space

Note : @mipmap/ic_launcher has white space in runtime

Try to load this url with Picasso or Glide

url -> https://i.picsum.photos/id/1074/536/354.jpg?hmac=zonZY4ypdNxP4jnidSxsqNsr8Vi0jnHezdA_kZkghSc

Upvotes: 2

Related Questions