How to update the selector(StateListDrawable) images using picasso

烂漫一生 提交于 2019-12-11 20:22:18

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/27010349/how-to-update-the-selectorstatelistdrawable-images-using-picasso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!