I have a custom view that looks like a spinning wheel with numbers from 0 to 9. Basically it scrolls from 0 to 9 while I load something from the server, and when the value returns, the animation stops on it. The animation is created by updating the Y value of the text I draw
Some relevant codes:
public class TimeSpinner extends View
{
.....
private void initialize()
{
.....
handler = new Handler();
repetitiveRunnable = new Runnable() {
@Override
public void run() {
updateData();
}
};
}
public void updateData() {
float delta = 3;
mDigitY += delta;
mDigitAboveY += delta;
mDigitBelowY += delta;
if (mCurrentDigit == animateToDigit) {
handler.removeCallbacks(repetitiveRunnable);
} else {
handler.postDelayed(repetitiveRunnable, 5);
}
invalidate();
}
public void startLoadingDigit() {
handler.postDelayed(repetitiveRunnable, 5);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawText(mDigitString, mDigitX, mDigitY, mDigitPaint);
canvas.drawText(mDigitAboveString, mDigitX, mDigitAboveY, mDigitPaint);
canvas.drawText(mDigitBelowString, mDigitX, mDigitBelowY, mDigitPaint);
}
}
The problem is that there is some kind of drawing for other views in the user interface stream, so depending on the speed of the phone, the animation is not fluid. This is similar to an unsuccessful frame rate, and sometimes for half a second. On powerful devices reasonably good.
, , , ? , SurfaceView. -? .
.
AsycTask Y
public class TimeSpinnerTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
while (animationRunning) {
updateData();
publishProgress();
try {
Thread.sleep(35);
} catch (InterruptedException e) {
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
invalidate();
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
invalidate();
super.onPostExecute(result);
}
}
public void updateData() {
mDigitY += delta;
mDigitAboveY += delta;
mDigitBelowY += delta;
if (mDigitAboveY > findCenterY(mCurrentDigit)) {
setCurrentDigit(mDigitAbove);
}
if (mCurrentDigit == animateToDigit) {
animationRunning = false;
setCurrentDigit(animateToDigit);
}
}
, , 1 , .
executeOnExecutor, "" Android Maps V2. , .
, . , :
?