状态压缩imageview与毕加索图书馆不工作

时间:2015-03-04 14:56:22

标签: android xml picasso

我在使用android:state_pressed和picasso

时遇到问题

这是我的活动代码: 感谢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:

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

我已经从这里尝试了一些答案,但仍然无法正常工作,即使是不能渲染的imageview。

任何人都可以举一些例子来说明如何将picasso与选择器状态一起用于imageview吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

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

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

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