I'm showing a DialogFragment
when the user taps on a row in a ListView
. I'd like to animate the showing of the dialog so that it grows from the center of the row. A similar effect can be seen when opening a folder from the launcher.
One idea that I've had is a combination of TranslateAnimation
and ScaleAnimation
. Is there another way?
Have you looked at Android Developers Training on Zooming a View? Might be a good starting point.
You probably want to create a custom class extending
DialogFragment
to get this working.Also, take a look at Jake Whartons NineOldAndroids for Honeycomb Animation API compatibility all the way back to API Level 1.
DialogFragment has a public getTheme() method that you can over ride for this exact reason. This solution uses less lines of code:
If you want to work over APIs you have to do inside your DialogFragemnt->onStart and not inside onCreateDialog
Being
DialogFragment
a wrapper for theDialog
class, you should set a theme to your baseDialog
to get the animation you want:Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
Now add the animation files in the res/anim folder:
( the
android:pivotY
is the key )anim_in.xml
anim_out.xml
Finally, the tricky thing here is to get your animation grow from the center of each row. I suppose the row is filling the screen horizontally so, on one hand the
android:pivotX
value will be static. On the other hand, you can't modify theandroid:pivotY
value programmatically.What I suggest is, you define several animations each of which having a different percentage value on the
android:pivotY
attribute (and several themes referencing those animations). Then, when the user taps the row, calculate the Y position in percentage of the row on the screen. Knowing the position in percentage, assign a theme to your dialog that has the appropriateandroid:pivotY
value.It is not a perfect solution but could do the trick for you. If you don't like the result, then I would suggest forgetting the
DialogFragment
and animating a simpleView
growing from the exact center of the row.Good luck!
To get a full-screen dialog with animation, write the following ...
Styles:
res/anim/slide_up.xml
res/anim/slide_down.xml
Java code:
In DialogFragment, custom animation is called onCreateDialog. 'DialogAnimation' is custom animation style in previous answer.