简体   繁体   中英

Best way to add animation to custom view in Android

I have a custom view where I've created a game. When a player earns some points, I'd like to add an animation of, say, "+500" bubbling up from an arbitrary x,y location relative to the custom view.

I'm thinking worst-case scenario, I could use different images for each screen size and animate them by having a thread change their location within my view's code in its onDraw, repeatedly calling invalidate. That just seems like I'm reinventing the wheel though.

How would one animate over a custom view in a better way, using something Android has that looks like its part of an existing view?

Use ObjectAnimator . You can read about it here . If you only want to animate text (like +500) then use a TextView and apply ValueAnimator on it. Something like this:

ObjectAnimator anim = ObjectAnimator.ofFloat(textView, "translationY", 0,100f);
anim.setDuration(2000);
anim.start();

You can get the y position of you custom view, and start the animation from there:

`ObjectAnimator anim = ObjectAnimator.ofFloat(textView, "translationY",y,y-100f);`

Please find the following links if helpful,

You can make your textView animate using this tutorial https://proandroiddev.com/how-to-create-a-bubble-selection-animation-on-android-627044da4854

and you can use this library to create animating bubble background.

https://github.com/glomadrian/Grav

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM