Animation not triggering after press on ImageView, but working fine after press on TextView
In an Activity I have three Views (among others): a TextView, an ImageView, and a LinearLayout--the latter I call controlsView
. There is a method (call it toggleControls()
to toggle the visibility of controlsView
with an animation. The animation is fairly simple and is created each time the method is called, like this:
private void toggleControls () {
if (controlsView.getAnimation () != null) {
return; // already animating
}
if (controlsView.getVisibility () == View.GONE) {
Animation animation = new ScaleAnimation (1, 1, 0, 1);
animation.setAnimationListener (new AnimationListener () {
public void onAnimationEnd (Animation animation) { controlsView.setAnimation (null); }
public void onAnimationRepeat (Animation animation) {}
public void onAnimationStart (Animation animation) { controlsView.setVisibility (View.VISIBLE); }
});
animation.setDuration (500);
controlsView.setAnimation (animation);
animation.startNow ();
} else {
// hide
Animation animation = new ScaleAnimation (1, 1, 1, 0);
animation.setAnimationListener (new AnimationListener () {
public void onAnimationEnd (Animation animation) { controlsView.setAnimation (null); controlsView.setVisibility (View.GONE); }
public void onAnimationRepeat (Animation animation) {}
public void onAnimationStart (Animation animation) {}
});
animation.setDuration (500);
controlsView.setAnimation (animation);
animation.startNow ();
}
}
This seems to work fine when called after a touch on the TextView, but when I call it after a touch on the ImageView, I never see the animation play. Instead, the state of the view (as displayed) does not change... until I touch somewhere on the screen, at which point the controlsView
will all-at-once appear or disappear. (This is BTW on a Xoom tablet running Android 3.0.1.)
For completeness' sake, the mildly-simplified XML for the Views which are clicked:
<TextView android:id="@+id/first"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:onClick="pressedFirst" android:clickable="true"
android:text="xxxxxxxxxxxxxxxxxxxxxx"></TextView>
<ImageView android:id="@+id/second"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:onClick="pressedSecond" android:clickable="true"
android:src="@drawable/something"></ImageView>
...and for the controlsView:
<LinearLayout android:id="@+id/controls"
android:layout_width="match_parent" android:layout_height="wrap_cont开发者_如何转开发ent"
android:visibility="visible" android:orientation="horizontal">
<Button android:id="@+id/importantButton"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:onClick="pressedFilterButton" android:text="Important"></Button>
</LinearLayout>
The functions referenced in the above XML merely call the toggleControls()
method.
I suspect I'm misunderstanding something fundamental here. Would someone toss me a clue please?
Just throwing out a guess, but perhaps you need to call Drawable.invalidateSelf()? I know I needed to do that when an Animation wasn't occurring, but not sure if you're hitting the exact same problem.
精彩评论