Android的:如何以编程方式创建StateListDrawable方式、Android、StateListDrawable

2023-09-04 03:28:37 作者:逍遥自在唯我独

我有一个GridView显示一些对象,并且在视觉上每个对象将有一个图像图标,文本标签。我也想图像图标有一些push和pop的效果单击时,即当pressed,图像将移动一小段距离底部正确的方向,并在发布的回到原来的位置。

的对象(及其图像图标)是从一些动态源。我的直觉是创建一个StateListDrawable为每个项目,这将有两种状态:pressed与否。对于GridView的项目来看,我会用一个按钮,它可以容纳一个可绘制和标签,完美地满足了我的需求量的。

我定义项目类的收官之原始对象:

I have a GridView to display some objects, and visually each of the objects will have an image icon and a text label. I also want the image icon to have some "push and pop" effect when clicked, that is, when pressed, the image will move a small distance to the bottom right direction, and when released get back to its original position.

The objects (and their image icons) are from some dynamic sources. My intuition is to create a StateListDrawable for each item, which will have two states: pressed or not. For GridView item view, I would use a Button, which can accomodate a Drawable and a label, that perfectly satisfies my requirment.

I defined an item class to wrap up the original object:

public class GridItem<T> {

    public static final int ICON_OFFSET = 4;

    private StateListDrawable mIcon;
    private String mLabel;
    private T mObject;

    public Drawable getIcon() {
        return mIcon;
    }

    public void setIcon(Drawable d) {
        if (null == d) {
            mIcon = null;
        }else if(d instanceof StateListDrawable) {
            mIcon = (StateListDrawable) d;
        } else {
            InsetDrawable d1 = new InsetDrawable(d, 0, 0, ICON_OFFSET, ICON_OFFSET);
            InsetDrawable d2 = new InsetDrawable(d, ICON_OFFSET, ICON_OFFSET, 0, 0);
            mIcon = new StateListDrawable();
            mIcon.addState(new int[] { android.R.attr.state_pressed }, d2);
            mIcon.addState(StateSet.WILD_CARD, d1);
            //This won't help either: mIcon.addState(new int[]{}, d1);
        }
    }

    public String getLabel() {
        return mLabel;
    }

    public void setLabel(String l) {
        mLabel = l;
    }

    public T getObject() {
        return mObject;
    }

    public void setObject(T o) {
        mObject = o;
    }

}

Android安卓编程入门 新建一个安卓程序

现在的问题是,当我触摸网格项,图标动作相当如我所料,但是当我的手指抬起离开该项目将不能恢复其原来的位置。

我的问题是:如何以编程方式创建一个StateListDrawable相当于一个从XML资源充气像

Now the problem is, when I touch a grid item, the icon "moves" quite as I have expected, but it won't restore its original position when my finger lifts up leaving the item.

My question is: how to programmatically create a StateListDrawable equivalent to one inflated from an XML resource like

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/image_pressed" />  
    <item android:drawable="@drawable/image_normal" />
</selector>

推荐答案

如果你的可绘制只是位图,你可以借鉴他们的编程方式,现在它应该帮助,但我不知道什么是问题InsetDrawable 的用法这里,基本上用prepared BitmapDrawables 被编程绘制,你就需要修改你的方法接受的位图 B

if your drawables are just bitmaps, you could draw them programmatically, for now it should help, however I wonder what is the problem with InsetDrawable usage here, basically use prepared BitmapDrawables that are drawn programatically, you would need to modify your method to accept bitmaps b

        Bitmap bc1 = Bitmap.createBitmap(b.getWidth() + ICON_OFFSET, b.getHeight() + ICON_OFFSET, Bitmap.Config.ARGB_8888);
        Canvas c1 = new Canvas(bc1);
        c1.drawBitmap(b, 0, 0, null);
        Bitmap bc2 = Bitmap.createBitmap(b.getWidth() + ICON_OFFSET, b.getHeight() + ICON_OFFSET, Bitmap.Config.ARGB_8888);
        Canvas c2 = new Canvas(bc2);
        c2.drawBitmap(b, ICON_OFFSET, ICON_OFFSET, null);

        mIcon = new StateListDrawable();
        mIcon.addState(new int[] { android.R.attr.state_pressed },  new BitmapDrawable(bc2));
        mIcon.addState(StateSet.WILD_CARD, new BitmapDrawable(bc1));
 
精彩推荐
图片推荐