How to update the selector(StateListDrawable) images using picasso - android

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);

Related

Drawable to bitmap efficiently using Picasso onBitmapFailed() method

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();
}

Holding strong reference to Picasso Target does not work

I am using Picasso to add an icon to my actionbar. I've read that in order to load the image every time I need to make my Target of strong reference and for that I've used final. But, again, it goes into onPrepareLoad and never reaches onBitmapLoaded.
What am I doing wrong?
private void setLogoToActionBar() {
final Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.d("DEBUG", "onBitmapLoaded");
Drawable d = new BitmapDrawable(getResources(), bitmap);
actionBar.setIcon(d);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.d("DEBUG", "onBitmapFailed");
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.d("DEBUG", "onPrepareLoad");
actionBar.setIcon(placeHolderDrawable);
}
};
Picasso.with(getContext()).load(mShop.getClientLogo()).placeholder(R.drawable.shop_asset).resize(100, 100).into(target);
}

How to load a circular appcompat actionbar logo using glide

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)
{
}
});

Unable to download the bitmap to a target using Picasso?

I am trying to load the bitmaps to an arraylist as follows:
Picasso.with(context).load(url).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
//mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
photos.add(bitmap);
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
});
But why is the onBitmapLoaded method never called?
Picasso, doesn't hold strong references to new Target() it will be garbage collected
look over this

android get Drawable image after picasso loaded

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() {
// ...
}
});
}
});

Categories

Resources