开发者

Android: Animating Images?

I have a bar with several ImageViews, each of them is used as a button (by using the OnClickListener). Now what I want to achieve is when a user pressed the image, some kind of a glow effect will appear around the image (much like when you press barButton on th开发者_如何学编程e iphone).

Is this possible?


This is possible. The correct way to achieve this is to define a StateList Drawable.
The statelist defines a separate drawable for each state.

For example the following xml defines a drawable that shows the blue_button_pressed drawable if the button is pressed and the blue_button_active drawable in all other cases.

<?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/blue_button_pressed"/>

 <item
  android:drawable="@drawable/blue_button_active"/>

</selector>

You put this file in the drawable folder like any other image and also reference it in the layout files like any other drawable.


Most Views don't actually animate animatable Drawables though, for some reason. To do this, you have to manually start the animation by calling the start() method on the AnimationDrawable (or Animatable, for that matter).

You can't do this immediately after setting the Drawable to a View though, for some weird reason. Instead, you can try to post the start call to a Handler, so that there is some time between the initial setting of the Drawable and the start of its animation.

Didn't have time to find out why it behaves like this yet, but it is what I ended up having to do.


You could manually generate this glow effect as a background picture and make it appear as a parent view behind the actual images when clicked. But I don't think there is any native function for that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜