开发者

Android: Highlight the Image Selected in Gallery

i want to highlight the currently selected image in gallery. is der any way to have that?

开发者_如何学编程

Thanks, Nital


Look at the StateListDrawable

EDIT:

You can use the mergeDrawableStates() method of the View class to provide your custom states:

http://code.google.com/android/reference/android/view/View.html#mergeDrawableStates(int[],%20int[])

Try the steps below to create new custom states:

1) Define the state resources in res/values/attrs.xml

<declare-styleable name="MyCustomState">
        <attr name="state_fried" format="boolean" />
        <attr name="state_baked" format="boolean" />
    </declare-styleable>

2) In your custom view class :

A) Declare the state variables:

private static final int[] FRIED_STATE_SET = {
        R.attr.state_fried
   };

   private static final int[] BAKED_STATE_SET = {
        R.attr.state_baked
   };

B) Override the onCreateDrawableState() method:

 @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
        if (isFried()) {
            mergeDrawableStates(drawableState, FRIED_STATE_SET);
        }
        if (isBaked()) {
            mergeDrawableStates(drawableState, BAKED_STATE_SET);
        }
        return drawableState;
    }

Once this is done, you should be able to use these in ColorStateListDrawable, but you should use your app's namespace to use these new states:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/<my_app_package>">

<item android:drawable="@drawable/item_baked" state_baked="true"
state_fried="false" />
<item android:drawable="@drawable/item_fried" state_baked="false"
state_fried="true" />
<item android:drawable="@drawable/item_overcooked" state_baked="true"
state_fried="true" />
<item android:drawable="@drawable/item_raw" state_baked="false"
state_fried="false" />
</selector>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜