如何使用毕加索更新选择器(StateListDrawable)图像



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

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

但它不起作用。

我认为您无法在运行时写入apk的可绘制文件夹。但是,您可以在代码中动态执行所需的操作。

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

使用以下行中的代码从毕加索那里获取位图。

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

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

相关内容

  • 没有找到相关文章

最新更新