I want to implement a play/pause button which is filling up, as the music is played. My first try was to create a timer which fires at 20 FPS (every 50ms) and call View.invalidate()
on my button. The button itself had a customer onDraw(...)
method which would, only statically for now, draw a bitmap to the canvas.
However, then running this in the simulator (I use GenyMotion) I immediately noticed that the sounds is jiggling, as if the CPU couldn't handle it.
I am a beginner in Android, so before experimenting in different way I would like to know, what is the recommended approach to animate a button as the music plays?
Suggestions I would like to give:
handler.postDelayed()
There is a beautiful sample code from Android on "How to make simple native games using SurfaceView", using threads and pre-loading bitmaps, called LunarLander .
If you look at LunarView.java code, they are using thread named LunarThread
which is created on constructor of LunarView
and started with the surface is created .
It's inside the LunarThread
they are performing all heavy task even drawing (inside thread's run method).
Basic OverView of LunarView:
LunarThread
, which preloads bitmaps and perform related tasks mRun
value is changed. mRun
value changed or when surface is destroyed(stops the thread, inside surfaceDestroyed(SurfaceHolder holder)
method) Use SurfaceView, you will see difference.
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.