Till now I have done the following, if I omit the circular image creation part it works fine, but I must show a circular image in actionbar
here is what I have tried so far, any help will be highly appreciated
Glide.with(mContext)
.load(doctorDetailsList.get(0).getDoc_imgurl().replace("200x200", Measuredwidth + "x" + Measuredwidth))
.placeholder(R.drawable.no_image)
.override(Measuredwidth, Measuredwidth)
.into(new Target<GlideDrawable>()
{
#Override
public void onLoadStarted(Drawable placeholder)
{
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable)
{
}
#Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation)
{
// GlideDrawable dr = resource;
Bitmap bitmap = ((com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable) resource).getBitmap();
String filename = doctorDetailsList.get(0).getDoc_imgurl().trim().substring(doctorDetailsList.get(0).getDoc_imgurl().trim().lastIndexOf("/") + 1);
filename = filename.replaceAll(".jpg", "");
int resID = getResources().getIdentifier(filename, "data", getPackageName());
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), resID);
//Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 200, 200, true));
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), icon);
circularBitmapDrawable.setCircular(true);
// GlideDrawable gd = new GlideDrawable(resource,)
getSupportActionBar().setLogo(circularBitmapDrawable);
}
#Override
public void onLoadCleared(Drawable placeholder)
{
}
#Override
public void getSize(SizeReadyCallback cb)
{
}
#Override
public void setRequest(com.bumptech.glide.request.Request request)
{
}
#Override
public com.bumptech.glide.request.Request getRequest()
{
return null;
}
#Override
public void onStart()
{
}
#Override
public void onStop()
{
}
#Override
public void onDestroy()
{
}
});
could not find a way, switched to Picasso and changed the code as following
Picasso.with(mContext)
.load(doctorDetailsList.get(0).getDoc_imgurl())
.resize(120, 120)
.centerCrop()
.placeholder(R.drawable.no_image)
.into(new com.squareup.picasso.Target()
{
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)
{
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), bitmap);
circularBitmapDrawable.setCircular(true);
getSupportActionBar().setLogo(circularBitmapDrawable);
}
#Override
public void onBitmapFailed(Drawable errorDrawable)
{
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable)
{
}
});
Related
I am using Glide v4 to load a bitmap that can then be used to as a marker on the map. When I use the deprecated SimpleTarget like so everything works fine.
GlideApp.with(getContext()).asBitmap().load(url)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
// load bitmap as marker
}
});
When I try removing the deprecated code and using Target<Bitmap> like given below I can see the onLoadStarted gets called but the onResourceReady is never called neither is the onLoadFailed.
GlideApp.with(getContext()).asBitmap()
.load(UrlHelper.createUrl(poi.getMapMarker()))
.into(marketBitmap);
private Target<Bitmap> marketBitmap = new Target<Bitmap>() {
#Override
public void onLoadStarted(#Nullable Drawable placeholder) {
Log.d("GlideMar", "marker load started");
}
#Override
public void onLoadFailed(#Nullable Drawable errorDrawable) {
Log.e("GlideMar", "marker load failed");
}
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
Log.d("GlideMar", "onResourceReady");
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
Log.d("GlideMar", "marker onLoadCleared");
}
#Override
public void getSize(#NonNull SizeReadyCallback cb) {
}
#Override
public void removeCallback(#NonNull SizeReadyCallback cb) {
}
#Override
public void setRequest(#Nullable Request request) {
}
#Nullable
#Override
public Request getRequest() {
return null;
}
#Override
public void onStart() {
Log.d("GlideMar", "marker onStart");
}
#Override
public void onStop() {
Log.d("GlideMar", "marker onStop");
}
#Override
public void onDestroy() {
Log.d("GlideMar", "marker onDestroy");
}
};
From Glide Custom Targets documentation.
If you’re using a custom Target and you’re not loading into a View
that would allow you to subclass ViewTarget, you’ll need to implement
the getSize() method.
So in your case just put the below code in getSize method
#Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}
Now the onResourceReady method will be called when you run the app.
I'm using target as callback mechanism (with Picasso).
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// loading of the bitmap was a success
// TODO do some action with the bitmap
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
// loading of the bitmap failed
// TODO do some action/warning/error message
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso
.with(context)
.load(...)
.into(target);
I want to load placeholder if bitmap fails to load, here:
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
My drawable placeholder is located locally in res/drawable folder.
Which is the best way to do the convertion from DRAWABLE to BITMAP?
FIRST WAY, (Alot of people suggests on SO this way):
Bitmap placeholderIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.placeholder);
SECOND WAY, (I think it should be more memory effiecient)
#Override
public void onBitmapFailed(Drawable errorDrawable) {
errorDrawable = getResources().getDrawable(R.drawable.poster_placeholder);
Bitmap placeholderIcon = ((BitmapDrawable) errorDrawable).getBitmap();
}
I have link for picture and I use Picasso for download and show it. Can I display ProgressBar to show that picture is loading?
here is my code:
Picasso.with(mContext)
.load(MYurl.BASE_URL + "/" + getItem(position).getImgThumb())
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
finalViewHolder.asanaImg.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
you have to start your progress bar before you load the image with Picasso. later you can dismiss it in loaded or failed callbacks.
//start progressbar here
Picasso.with(mContext)
.load(MYurl.BASE_URL + "/" + getItem(position).getImgThumb())
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
finalViewHolder.asanaImg.setImageBitmap(bitmap);
//stop progressbar here
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
//stop progressbar here
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
I want to load a two images from their own url using picasso and use them as a statelist like:
<item android:drawable="#drawable/sidebar_news_selected" android:state_selected="true"/>
<item android:drawable="#drawable/sidebar_news_selected" android:state_activated="true"/>
<item android:drawable="#drawable/sidebar_news_normal"/>
how can i do that?
Update:
Thanks to Maddy, i tried his answer and now i stock in that like this:
final StateListDrawable drawable = new StateListDrawable();
final Picasso picasso = Picasso.with(this.context);
target_normal = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawImage = new BitmapDrawable(context.getResources(), bitmap);
drawable.addState(new int[]{android.R.attr.state_enabled}, drawImage);
picasso.load(context.getString(R.string.server_address)+dItem.getIconNormal()).into
(target_normal);
target_selected = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawImage = new BitmapDrawable(context.getResources(), bitmap);
drawable.addState(new int[]{android.R.attr.state_selected}, drawImage);
drawable.addState(new int[]{android.R.attr.state_checked}, drawImage);
picasso.load(context.getString(R.string.server_address)+dItem.getIconSelected())
.into(target_selected);
drawerHolder.icon.setImageDrawable(drawable);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
but it doesn't work.
I think you can't write to drawable folder of apk at runtime. But you can do what you want dynamically in code.
# Convert Picasso's Bitmap to Drawable
Drawable d = new BitmapDrawable(getResources(),bitmap);
#Create StateListDrawable
StateListDrawable stateList = new StateListDrawable();
stateList.addState(new int[] {android.R.attr.state_pressed},drawable1);
stateList.addState(new int[] {android.R.attr.state_focused},drawable2);
#Add Background
MyButton.setBackgroundDrawable(stateList);
Use code on following lines to get the BitMap from Picasso.
//To Load image from Picasso
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
private void someMethod() {
Picasso.with(this).load("url").into(target);
}
#Override
public void onDestroy() { // could be in onPause or onStop
Picasso.with(this).cancelRequest(target);
super.onDestroy();
}
Thanks to Maddy final code looks like this:
final StateListDrawable stateListDrawable = new StateListDrawable();
final Picasso picasso = Picasso.with(this.context);
// selected and checked state
target_selected = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawImage = new BitmapDrawable(context.getResources(), bitmap);
stateListDrawable.addState(new int[]{android.R.attr.state_selected}, drawImage);
stateListDrawable.addState(new int[]{android.R.attr.state_activated}, drawImage);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
picasso.load(context.getString(R.string.server_address_http) + dItem.getIconSelected())
.into(target_selected);
target_normal = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawImage = new BitmapDrawable(context.getResources(), bitmap);
stateListDrawable.addState(StateSet.WILD_CARD, drawImage);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
picasso.load(context.getString(R.string.server_address_http) + dItem.getIconNormal())
.into(target_normal);
drawerHolder.icon.setImageDrawable(stateListDrawable);
I am using Picasso library to load image from url. The code I used is below.
Picasso.with(getContext()).load(url).placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder).into(imageView);
What I wanna do is to get the image that loaded from url. I used
Drawable image = imageView.getDrawable();
However, this will always return placeholder image instead of the image load from url. Do you guys have any idea? How should I access the drawable image that it's just loaded from url.
Thanks in advance.
This is because the image is loading asynchronously. You need to get the drawable when it is finished loading into the view:
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
Drawable image = imageView.getDrawable();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
Picasso.with(this).load("url").into(target);
mImageView.post(new Runnable() {
#Override
public void run() {
mPicasso = Picasso.with(mImageView.getContext());
mPicasso.load(IMAGE_URL)
.resize(mImageView.getWidth(), mImageView.getHeight())
.centerCrop()
.into(mImageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
Drawable drawable = mImageView.getDrawable();
// ...
}
#Override
public void onError() {
// ...
}
});
}
});