带有毕加索库的状态按下图像视图不起作用

state pressed imageview with picasso library not working

我在使用 android:state_pressed 和 picasso

时遇到问题

这是我在 Activity 上的代码: 感谢 Mahmoud Elmorabea **更新我的最终代码是**

final StateListDrawable stateListDrawable = new StateListDrawable();
    final Picasso picasso = Picasso.with(this.getApplicationContext());
target_selected = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Drawable drawImage2 = new BitmapDrawable(
                    getApplicationContext().getResources(), bitmap);
            stateListDrawable
                    .addState(new int[] { android.R.attr.state_pressed},
                            drawImage2);
            stateListDrawable.addState(
                    new int[] { android.R.attr.state_activated },
                    drawImage2);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };
    picasso.with(getApplicationContext()).load(R.drawable.akadblack)
            .into(target_selected);
    target_normal = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            Drawable drawImage = new BitmapDrawable(getApplicationContext()
                    .getResources(), bitmap);
            stateListDrawable.addState(StateSet.WILD_CARD, drawImage);

        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    picasso.with(getApplicationContext()).load(R.drawable.akad)
            .into(target_normal);

    imgAkad.setImageDrawable(stateListDrawable);

这里是 btnStart XML:

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@drawable/resepsi" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/resepsiblack" android:state_pressed="true"/>
<item android:drawable="@drawable/resepsi" android:state_pressed="false" android:state_selected="true"/>

最后在我的布局上 xml 我放了一个像这样的图像视图:

<ImageView
        android:id="@+id/imgStart"
        android:layout_width="150dp"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/start" />

我已经从这里尝试了一些答案,但仍然无法正常工作,甚至图像视图也无法呈现。

任何人都可以给我一些示例,说明如何将带有选择器状态的 picasso 用于 imageview 吗?

提前致谢

同步加载要用作选择器的两个图像。

然后使用以下动态设置选择器:

Bitmap bmpPressed = Picasso.with(context).get(url);
Bitmap bmpNotPressed = Picasso.with(context).get(url);
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    bmpPressed);
states.addState(new int[] { -android.R.attr.state_pressed},
    bmpNotPressed);

我不知道 Picasso 中是否存在方法 get(),我之前用 UIL

做过