How to use Glide's SimpleTarget with databinding? - android

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?

Related

Glide image from gallery to bitmap variable

I have a variable Bitmap image. I have imageButton in my fragment. I can change image of imageButton with Glide.with().load().into(). But I want not to just change image but to save it into Bitmap variable so I can use it for some other task. I tried this
Bitmap image = Glide.with(getContext()).asBitmap().load(imagePath).submit().get();
imageButton.setImageBitmap(image);
But image of imageButton does not change. What's wrong with first line then? Because I am pretty sure that problem is there but don't understand yet what is exactly wrong.
A solution is to do
Bitmap image = BitmapFactory.
decodeStream(getContext().
getContentResolver().
openInputStream(selectedImage));
imageButton.setImageBitmap(image);
Where selectedImage is Uri selectedImage = data.getData() of onActivityResult method, but it will be total mess because of size of bitmap, so we have to use createScaledBitmap, I don't like it. For some reason Bitmap image = BitmapFactory.decodeFile(imagePath) doesn't work, imageButton just becomes tiny grey square, and I didn't find a solution for this.
first of all why are using the imageButton..use imageView it will good than
imagebutton and it will work
I don't know if you should even think about doing this in some serious project but for now it did what needs to be done for me. Earlier I declared private Bitmap jopa.
Glide.with(getContext()).asBitmap().
load(selectedImage).override(500,500).
into(new CustomTarget<Bitmap>()
{
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
imgShare.setImageBitmap(resource);
jopa = ((BitmapDrawable)imgShare.getDrawable()).getBitmap();
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Now Bitmap jopa can be used for other tasks. I hope. Tested it to change picture of another imageview, it worked, so I'll pray it will work further. Full onActivityResult code here. Don't want question to get too messy

Save Glide image onClick in ViewPager

I'm showing images from the server with RecyclerView. When a user clicks in RecyclerView item it open with ViewPager and FullImage fragment. There is a button that user can save that image.
For now, when user click on a button, I get the image position and save it with another glide request like this -
GlideApp.with(container.getContext())
.asBitmap()
.load(image.getUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, Transition<? super Bitmap> transition) {
// I want here the trigger option. When user click btn
// saveImage(resource, image.getTitle());
imageViewPreview.setImageBitmap(resource);
}
});
It takes time and data cause it sending another req. I want that when a user clicks on a button, It should save from that original Glide resource. Now how can I trigger that option?
GlideApp.with(container.getContext())
.asBitmap()
.load(image.getUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, Transition<? super Bitmap> transition) {
// I want here the trigger option. When user click btn
// saveImage(resource, image.getTitle());
imageViewPreview.setImageBitmap(resource);
}
});
You can use a global Bitmap variable, and after first load (when user switch to the current picture) set that bitmap. So, onResourceReady you will do something like currentBitmap = resource.
Eventually, if you are on the ViewPager, you can get the image based on current position using getAdapterPosition(); (which I suppose you already do)

Android: Glide not showing large image from URL

I'm trying to load a large hi-res (3225x4800) image from URL into glide for a newspaper company. The image I wanted to load is this High Res Image.
String url = "http://www.businessweekmindanao.com/content/wp-content/uploads/2015/10/fitness-and-wellness-poster-final.jpg";
Glide.with(getActivity()).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
I'm using Mike Ortiz' TouchImageView from the solution offered here: https://github.com/MikeOrtiz/TouchImageView/issues/135 when loading resource from url. But it seems to only work on small resolution image and it does not load high quality image.
There is an alternative solution I tried from stackoverflow: Android: not displayed ImageView with UIL and TouchImageView which tries to modify the onMeasure() method of Mike's library. It works with:
Glide.with(getActivity()).load(url)
.into(imageViewPreview);
The problem is it loads the image in very low resolution.
Is there a way to load high resolution image like: 7480x3740 for instance using Glide and TouchImageView? How do I do that?
I have found a solution that worked for my requirement. The override() method does the trick. Setting higher numbers for the target resolution seems to be the final workaround, but the bigger the numbers, the longer the time it would take to display the image(s), so it would be wise to implement a preloader/progressbar.
Glide.with(getContext())
.asBitMap() //[for new glide versions]
.load(url)
//.asBitmap()[for older glide versions]
//.placeholder(R.drawable.default_placeholder)
.override(1600, 1600) // Can be 2000, 2000
.into(new BitmapImageViewTarget(imageViewPreview) {
#Override
public void onResourceReady(Bitmap drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
progressBar.setVisibility(View.GONE);
}
});
Custom centered preloader/progressbar placeholder indicator on relativeLayout xml:
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
This is neat because another issue related about loading image asBitmap() in Glide will not show placeholders for error and preloader/progress indicator:
Glide.with(getActivity())
.load(url).asBitmap()
.placeholder() //<== will simply not work:
.error() // <== is also useless
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
In my research, I have also tried a Picasso solution to handle high quality image:
Picasso.with(getContext())
.load(url)
.resize(1500, 0)
.placeholder(R.drawable.default_placeholder)
.error(R.drawable.download_error)
.into(imageViewPreview);
In the end, I was happy with the Glide version.
I hope this will be helpful to anyone that might be facing the same challenge.
I had issue while loading image from url into TouchImageView using Glide. It would show a black screen.
I found a fix which works like this. Pass width and height in Glide override method and after setting bitmap call setZoom of TouchImageView with value 1.
Here is the complete code:
Glide.with('context')
.asBitmap()
.load('url')
.apply(new RequestOptions().override(1600, 1600)) //This is important
.into(new BitmapImageViewTarget('imageview') {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
'imageview'.setImageBitmap(resource);
'imageview'.setZoom(1); //This is important
}
});
Add android:usesCleartextTraffic="true"
Glide will start loading images in android 10
I have same issue and it is due to large image size. resolved it by following way:
Glide
.with(imageView.context)
.load(url)
.placeholder(R.drawable.placeholder)
.apply(RequestOptions().transform(CenterCrop(), RoundedCorners(if (applyCorner) 32 else 1)))
.into(object : CustomTarget<Drawable>(200, 200) {
override fun onLoadCleared(placeholder: Drawable?) {
// called when imageView is cleared. If you are referencing the bitmap
// somewhere else too other than this imageView clear it here
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
imageView.setImageDrawable(resource)
}

Picasso loads image with triangle in corner of image

I'm using picasso library for loading images from server into my application. my problem is when image loaded it has a triangle in top-left corner of image with color(like blue,green,red).
this is my code for loading image:
public static void loadDynamicImage(final String url, final Context context, final ImageView imageView, final int width, final int height){
Picasso.with(context).load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.resize(width,height)
.onlyScaleDown()
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context).load(url).resize(width,height).onlyScaleDown().into(imageView);
}
});
}
the image shown is :
You have enabled debug indicators on your Picasso instance (see official website). Look for setIndicatorsEnabled(true) in your code and remove it.
You have setIndicatorsEnabled set to true
Picasso picasso = Picasso.with(this);
picasso.setIndicatorsEnabled(false); //Or remove picasso.setIndicatorsEnabled(true);
Check this: Is there any way from which we can detect images are loading from cache in picasso?

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.

Categories

Resources