如何使用picasso更新选择器(StateListDrawable)图像

时间:2014-11-19 06:25:27

标签: android picasso statelist

我想使用毕加索从他们自己的网址加载两张图片,并将它们用作状态列表,如:

<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"/>

我该怎么做?

更新

感谢Maddy,我尝试了他的答案,现在我就像这样:

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

            }
        };

但它没有用。

2 个答案:

答案 0 :(得分:1)

我认为你不能在运行时写入apk的drawable文件夹。但是你可以在代码中动态地做你想做的事。

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

使用以下行代码从毕加索获取BitMap。

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

答案 1 :(得分:1)

感谢Maddy最终代码如下所示:

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