Android loading images from url doesnt update fragments directly - android

I am using a pager to controll multiple fragments, count depends on data input. Each fragment displays infos about a person, also a image. I am loading the image per Picasso library in each fragment. But the fragment doesnt update after the image was loaded and set to the ImageView, so it doesnt appear on the screen. If I am closing the fragments and open them again, all images are shown.
Is it possbile to update the fragments after loaded the images, so the images appear directly on the screen?
private void setHeaderImage() {
if (stolperstein.isImageHeaderUrl()) {
Picasso.with(getContext()).load(stolperstein.getImageHeaderUrl()).into(new Target() {
#Override
public void onPrepareLoad(Drawable arg0) {
}
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
user_header_photo.setBackground(drawable);
}
#Override
public void onBitmapFailed(Drawable arg0) {
}
});
}
}

I'm not exactly sure what you're doing in the last part of your code , but if user_header_photo is an ImageView you can load it directly into it:
Picasso.with(getContext()).load(stolperstein.getImageHeaderUrl()).into(user_header_photo)

Related

How to use Glide's SimpleTarget with databinding?

I use Glide to load my image. I need to modify my image with a SimpleTarget callback, however when the image loaded to my list, and I scroll the list shows first always an other image, and than animate the right image after 1 second to the place. Without image modification and SimpleTarget everything works just fine. Here is my code.
#BindingAdapter("imageSrc")
public static void setImage(ImageView imageView, String url) {
Glide.with(imageView.getContext()).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//the bitmap modified here
imageView.setImageBitmap(bmp);
}
});
}
is there any solution to avoid the flickering?

How to delay Picasso image loading?

I have implemented a shared element activity transition animation between MainActivity (GridView) and DetailActivity. When item is clicked, the image in Grid item will zoom-in to become the background image in DetailActivity. I have made the transition very smooth by saving the image on the MainActivity into a file, then use it as placeholder image in DetailActivity before the higher quality image is downloaded via picasso. When picasso finished its task the higher quality image will replace the placeholder one very neatly.
Code from onCreateView() in DetailActivityFragment.java
ImageView iv = (ImageView) mRootview.findViewById(R.id.movie_poster);
try {
// Prepare "InterimPoster" jpeg file to be placeholder image
Bitmap bitmap = BitmapFactory.decodeStream(c.openFileInput("InterimPoster"));
Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
.placeholder(new BitmapDrawable(getResources(), bitmap))
.fit()
.centerCrop()
.noFade()
.into(iv);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
As for animation, I implement it like in the link here step 1-3 https://guides.codepath.com/android/Shared-Element-Activity-Transition
However, sometimes there's flicker when the higher quality image finished download before the transition animation is completed. The zooming-in image will be replaced with new image while moving which is an unpleasant animation.
So I wonder how can I fix this flicker? One thing I can think of is to delay the image download because I already have lower quality image as a placeholder. How can I do that?
Here the video.
Usually, on my test device it's smooth 80% of the time, but luckily in this video it flicker most of the time.
what's animation ? load image while animation end
TranslateAnimation translateAnimation = new TranslateAnimation(0,1.5f,0,1.5f);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//load image when animation end
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
and
LayoutTransition.TransitionListener transitionListener = new LayoutTransition.TransitionListener() {
#Override
public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
}
#Override
public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
//load image when animation end
}
};
Now I have found a way to delay Picasso image loading (or any other task). It's very simple really with the use of Handler and Runnable.
Now my code looks like this. The image replacement is pretty smooth in any case now.
// Set Background Poster in Try-catch in case file cannot be open
try {
// Get/Prepare "InterimPoster" jpeg file to be placeholder image
final ImageView iv = (ImageView) mRootview.findViewById(R.id.movie_poster);
final Bitmap bitmap = BitmapFactory.decodeStream(c.openFileInput("InterimPoster"));
final Drawable lowResPoster = new BitmapDrawable(getResources(), bitmap);
iv.setImageDrawable(lowResPoster);
// Download higher resolution image with 0.8 sec delay to avoid load complete before
// animation finishes (causing some flicker/overflow image problem).
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
// still need placeholder here otherwise will flash of white image
.placeholder(lowResPoster)
.error(lowResPoster)
.fit()
.centerCrop()
.noFade() // without this image replacement will not be smooth
.into(iv);
}
};
handler.postDelayed(runnable, 800);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Try the below code:
First use picasso normally to load placeholder into the imageview
iv.setImageBitmap(bitmap);
Then use SharedElement Listener like this
Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();sharedElementEnterTransition.addListener(new TransitionListenerAdapter() {
#Override
public void onTransitionEnd(android.support.transition.Transition transition) {
super.onTransitionEnd(transition);
Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
.placeholder(new BitmapDrawable(getResources(), bitmap))
.fit()
.centerCrop()
.noFade()
.into(iv);
}
});
To delay some process, you can use Handler().
Example.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// write your codes here.. this will delay 2 seconds...
startActivity(new Intent(this,MySecondActivity.class);
}
},2000);

How to set the imageview as the background image of my LinearLayout from Picasso image loader programmatically?

I'm using Picasso class/library to load an image from an url and display the image to a ImageView. Is it possible for me to set the imageview loaded by the picasso image loader from an url as the background image of a linearlayout programmatically?
I've already found this issue - might be useful for you:
How do i set background image with picasso in code
According to that, Use callback of Picasso
Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})
Read also
Set background resource using Picasso
but there would you find the same solution.

How to add textview on Imageview using bitmap drawable in android?

Task
1) Download the image from repository using picasso library
2) Add text and Image on specific coordinates on downloaded image. I don't want to use RelativeLayout. Can we do it using bitmap , canwas, drawable?
3) After adding Text and Image on specific coordinates load the Image on ImageView specified in Picasso.load() section [Please have a look at the code below].
How can I do that?
Example (Look at the heart and amount)
I am using picasso to download images:
private Target loadtarget;
public void loadBitmap(String url) {
if (loadtarget == null) loadtarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// do something with the Bitmap
handleLoadedBitmap(bitmap);
}
#Override
public void onBitmapFailed() {
}
}
Picasso.with(this).load(url).into(loadtarget);
}
public void handleLoadedBitmap(Bitmap b) {
// do something here
}
my ImageView.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>
Now I want to add text or image on above image (loaded by picasso) using bitmap and canvas. How can I do that?

Google Maps Cluster Item Marker Icon with Picasso

I'm using Google Map SDK 7.3.0 with android-maps-utils 0.3.4 because I need clusters for my Markers on the map.
Ok, so here the problem is, I shouldn't have a red marker. Only green+blue markers.
I subclassed DefaultClusterRenderer to create my custom marker view but sometimes it just doesn't work.
I'm using picasso to get the green icon because it's coming from an API. But the problem is, when picasso has loaded the bitmap it's too late, the icon has already been set to the default one (red).
Here's my onBeforeClusterItemRenderer :
Picasso.with(getApplicationContext()).load(item.url).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
FrameLayout icon = (FrameLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.marker, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
icon.findViewById(R.id.bg).setBackground(new BitmapDrawable(getResources(), bitmap));
} else {
icon.findViewById(R.id.bg).setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
Bitmap b = createDrawableFromView(Home.this, icon);
if (marker != null) {
marker.icon(BitmapDescriptorFactory.fromBitmap(b));
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
--- EDITED ---
When downloading the image inside onBeforeClusterItemRendered you are actually downloading the image every time the Cluster Manager tries to load a marker, so if you have, for example, 100 markers you will download the image 100 times.
You should download the image inside onCreate, save it in a static variable, call mClusterManager.cluster(); after saving the image, and finally inside onBeforeClusterItemRendered wrtie marker.icon(BitmapDescriptorFactory.fromBitmap(YourActivity.b));

Categories

Resources