Android Picasso is not loading image when i use fit() - android

Without fit() it works fine.
Picasso.with(getApplicationContext()).load(url.concat(featureImage)).into(imageView);
ImageView imageView = (ImageView)findViewById(R.id.featureImage);
TextView textTitle = (TextView)findViewById(R.id.title);
TextView textDetail = (TextView)findViewById(R.id.detail);
Picasso.with(getApplicationContext()).load(url.concat(featureImage)).fit().into(imageView);
textTitle.setText(title);
textDetail.setText(detail);

fit() - Attempt to resize the image to fit exactly into the target ImageView's bounds.
So if you set layout_width or layout_height of your ImageView "wrap_content" and don't set minimal possible sizes, height or width of the view will be initially 0 and your image will be resized accordingly.

try this way
Picasso.with(mContext).load(url).centerCrop().fit()
.placeholder(R.mipmap.bg_screen)
.error(R.mipmap.bg_screen)
.into(mImageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Log.i(TAG, "onSuccess: TRUE");
}
#Override
public void onError() {
Log.i(TAG, "onError: TRUE");
}
});
your imageview in xml should like ,
<ImageView
android:id="#+id/img_movie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:layout_gravity="center"
/>

Eventually the following code fulfilled my requirement.
Picasso.with(getApplicationContext()).load(url.concat(featureImage)).centerCrop().fit()
.placeholder(R.mipmap.nrum_logo)
.error(R.mipmap.nrum_logo)
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Log.i("Picasso", "onSuccess: TRUE");
}
#Override
public void onError() {
Log.i("Picasso", "onError: TRUE");
}
});
My imageView:
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitCenter"
android:id="#+id/featureImage" />

Related

ImageView not displaying image set programmatically

I have an ImageView:
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iconIv"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:adjustViewBounds="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"
tools:srcCompat="#sample/icon[1]" />
// I've also tried usual ImageView with no success
It's fully visible and I see its bounds in UiAutomatorViewer.
When I set image from XML, for example, app:srcCompat="#drawable/ic_telephone" it's displayed normally.
If I set the same image programmatically iconIv.setImageResource(R.drawable.ic_telephone) it does not display image.
It also does not work with Picasso:
Picasso
.with(iconIv.context)
.load(R.drawable.ic_telephone)
.into(iconIv)
or
Picasso
.with(iconIv.context)
.load(appTask.iconUrl)
.into(iconIv)
or with Glide:
Glide
.with(iconIv.context)
.load(appTask.iconUrl)
.into(iconIv)
Why?
You need get()
https://square.github.io/picasso/
Picasso.get()
.load(R.drawable.ic_telephone)
.into(iconIV);
Hmm, try this, AppCompatImageView you need that.
I just tested FYI
private AppCompatImageView mImv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImv = findViewById(R.id.iconIv);
mImv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG, "onClick: ");
}
});
mImv.setImageResource(R.drawable.ic_save);

View width is zero when set to MATCH_PARENT

I have an ImageView inside of a LinearLayout which is an item of a RecyclerView. I have all these views' width as MATCH_PARENT.
Now, I'm trying to calculate the heigth using the aspect ratio before the image is loaded so there's no resizing.
But, what I've seen is that before the image is loaded, its width is zero.
The thing is that I'm doing similar things in other parts of the app and it's working fine, I don't really know what I'm missing here.
RecyclerView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<LinearLayout
...
</LinearLayout>
<!-- This is the image -->
<ImageView
android:id="#+id/descubre_shop_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorLightText"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="8dp"/>
</LinearLayout>
ViewHolder
mBannerTarget = new Target()
{
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
{
mShopBannerImageView.setImageBitmap(bitmap);
// Wrap content when the images is loaded.
mShopBannerImageView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
mShopBannerImageView.setBackgroundColor(-1);
mShopBannerImageView.setAlpha(1.0f);
Animation fadeOut = new AlphaAnimation(0, 1);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(250);
mShopBannerImageView.startAnimation(fadeOut);
LOADED = true;
}
#Override
public void onBitmapFailed(Drawable errorDrawable)
{
mShopBannerImageView.setBackgroundColor(
mContext.getResources().getColor(android.R.color.holo_red_dark));
mShopBannerImageView.setAlpha(0.2f);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable)
{
// This is 0
Log.d(Properties.TAG, "" + mShopBannerImageView.getWidth());
// Remove the old bitmap
mShopBannerImageView.setImageBitmap(null);
// Set the height using the width and the aspect ratio.
mShopBannerImageView.getLayoutParams().height =
(int) (mShopBannerImageView.getWidth() * shop.getAspectRatio());
// Set a color while loading.
mShopBannerImageView.setBackgroundColor(
mContext.getResources().getColor(R.color.colorAccent));
mShopBannerImageView.setAlpha(0.25f);
}
};
I tried to set the width of the ImageView and its parent as MATCH_PARENT in code but no luck. Any clue of what's going on?
Regards,
The views dimensions is always 0 before being fully created, if you wanted to know the actual width, you have to use the method: post():
view.post(new Runnable() {
#Override
public void run() {
// This is where width would be different than 0
}
});
That being said, you should actually use an other method to keep the ratio than actually calculating it yourself!
See this thread for further information How to scale an Image in ImageView to keep the aspect ratio

picasso and glide are not loading the image from web

picasso and glide are not loading the image from web. Below is the jave and xml. First i use picasso which is commented out now, then i use the glide library, but the problem is same. nothing appears on screen. if i ad image source from assets it appears on preview of android studio but i want to load image from web and show it in the image view. please correct my mistake if any..? Thanks.
JAVA:
ImageView imageBanner1 = (ImageView) findViewById(R.id.imageBanner1);
ImageView imageBanne2 = (ImageView) findViewById(R.id.imageBanner2);
Glide.with(getApplicationContext()).load("http://example.com/appBanner1.jpg")
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageBannerl);
/*Picasso.with(getApplicationContext())
.load("http://example.com/appBanner1.jpg")
.skipMemoryCache()
.into(imageBannerl);*/
imageBanner1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent inttv=new Intent(MainActivity.this,Play.class);
inttv.putExtra("url","http://example.com");
startActivity(inttv);
}
});
Glide.with(getApplicationContext()).load("http://example.com/appBanner2.jpg")
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageBanner2);
/*Picasso.with(getApplicationContext())
.load("http://example.com/appBanner2.jpg")
.skipMemoryCache()
.into(imageBanner2);*/
imageBannerAd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent imageBannerBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivity(imageBannerBrowserIntent);
}
});
XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#+id/tabHost"
android:layout_centerHorizontal="true"
android:id="#+id/imageBannerLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageBanner1"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:background="#color/app_background" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageBanner2"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:background="#color/app_background" />
</LinearLayout>
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("url").into(imageView);
I think you need to check your ImageView variable name.
You have used ImageView imageBanner1 and ImageView imageBanne2 as variable name but never used it.Instead you have used imageBannerChannel and imageBannerAd as variable name. Once Check your code again. And make sure your url is fine.

Something wrong happens with RecyclerView

New items are added to RecyclerView each time when Adapter have created.
In photo I showed a process where darken dish images appear, though I managed image darking with a condition if(holder.mDish.getPicurl()!=null), but it doest work.
If I go into darken dish image details and go back to list, there will by three darken images. Is it problem with RecyclerView or it is my fault?
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mDish = DISHES.get(position);
final int id = holder.mDish.getId();
if(holder.mDish.getPicurl()!=null) {
Picasso.with(mContext)
.load(holder.mDish.getPicurl())
.fit()
.transform(new RoundedTransformation(20, 0))
.into(holder.mIvBackground);
holder.mIvBackground.setColorFilter(Color.parseColor("#aaaaaa"), PorterDuff.Mode.MULTIPLY);
} else {
holder.mIvBackground.setImageResource(R.drawable.vector_drawable_dish);
}
holder.mTvName.setText(holder.mDish.getName());
//holder.mTvDescription.setText(holder.mDish.getDescription());
holder.mTvPrice.setText("T " + String.valueOf(holder.mDish.getPrice()));
holder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mListener!=null){
mListener.onAddToBasketInteraction(id);
}
}
});
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onStartDetailActivity(id);
}
});
}
public void addDishes(List<Dish> dishes){
DISHES.clear();
DISHES.addAll(dishes);
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public Dish mDish;
public final View mView;
public final ImageView mIvBackground;
public final TextView mTvName;
//public final TextView mTvDescription;
public final TextView mTvPrice;
public final ImageButton mBtnAdd;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
mIvBackground = (ImageView) itemView.findViewById(R.id.dishBackground);
mTvName = (TextView) itemView.findViewById(R.id.name);
//mTvDescription = (TextView) itemView.findViewById(R.id.description);
mTvPrice = (TextView) itemView.findViewById(R.id.price);
mBtnAdd = (ImageButton) itemView.findViewById(R.id.btnAdd);
}
}
xml of item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal"
android:padding="2dp">
<ImageView
android:id="#+id/dishBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Плов узбекский"
android:textColor="#color/colorAccent2"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:textSize="14sp" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="T 3000"
android:textColor="#android:color/holo_orange_light" />
<ImageButton
android:id="#+id/btnAdd"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/ic_add_shopping_cart_black_24dp" />
</RelativeLayout>
You don't undo view changes in conditional statement
The culprit is this piece of code:
if(holder.mDish.getPicurl()!=null) {
Picasso.with(mContext)
.load(holder.mDish.getPicurl())
.fit()
.transform(new RoundedTransformation(20, 0))
.into(holder.mIvBackground);
holder.mIvBackground.setColorFilter(Color.parseColor("#aaaaaa"), PorterDuff.Mode.MULTIPLY);
} else {
holder.mIvBackground.setImageResource(R.drawable.vector_drawable_dish);
}
The way RecyclerView works is it reuses the same views with a different data (hence recycler), so the onBindViewHolder method may be called multiple times for each position, but the ViewHolder might have been already created and previously bound to a different piece of data. That way some changes applied to the view inside the onBindViewHolder method might persist through bind calls to different pieces of data.
In your case, the following call:
holder.mIvBackground.setColorFilter(Color.parseColor("#aaaaaa"), PorterDuff.Mode.MULTIPLY);
alters the view with the color filter within one call to onBindViewHolder. Then, when another piece of data is bound to the same ViewHolder, the condition holder.mDish.getPicurl()!=null might be false, but the effect still persists, because there is no removal of this color filter in the else branch of your statement.
Fix
You need to explicitly set and unset any changes to a single view in your RecyclerView for every separate condition you wish to implement:
if(holder.mDish.getPicurl()!=null) {
holder.mIvBackground.setColorFilter(Color.parseColor("#aaaaaa"), PorterDuff.Mode.MULTIPLY);
} else {
holder.mIvBackground.setColorFilter(null); // removes color filter
}
You don't see any problems with your mIvBackground because you set a different one in each branch of your if-else statement, so no unwanted state persists.

Layout issue while loading an image on Galaxy Tab 2

Loading an image that is larger than the width of a Galaxy Tab 2 P5100 (running 4.1.2) into an ImageView adds some sort of top/bottom padding to the loaded image.
Here's a screenshot with Show layout boundaries turned on:
Here's how it should look (from a Nexus 10 running 4.4.2):
The code I use (for both examples above) is
public class ImageBugActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bug);
// This bug is still reproducible if I use the
// Universal-Image-Loader library or if I change the dimensions of
// the image to a different width
loadImage("http://placehold.it/1600x1000", (ImageView)findViewById(R.id.image));
}
private void loadImage(final String url, final ImageView view) {
new Thread(new Runnable() {
#Override
public void run() {
try {
final Bitmap bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
Log.e("loadImage", e.getMessage(), e);
}
}
}).start();
}
}
And the layout file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="On a Galaxy Tab 2 the image below it is pushed to the center of the remaining space." />
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</LinearLayout>
Does this seem to be an Android/Samsung bug or am I making a dumb mistake?
Setting the android:scaleType of the ImageView to "fitStart" should do the trick.
You should use "match_parent" for layout_height of the ImageView.
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top" />

Categories

Resources