I am making a custom ProgressBar which has a bitmap that just rotates. if I use the View.invalidate the FPS will stutter; the deltaTime will go up 40+ many times. The logic and render is almost instantaneous but the invalidate call makes it slow. So I am trying the SurfaceView, problem is I have had problems earlier assemble SurfaceViews with other Views and I would like not to use it. Are there any alternative to SurfaceView and View.invalidate or am I stuck with them?
EDIT:
To clarify some more, I am making a custom View that is an intermediate ProgressBar. So the View is the ProgressBar. I am also not downloading anything now, I am only trying to get max FPS for my View. Some code:
MyProgressBar extends View implements Runnable
And the run()
calculateNewRotation();
invalidate();
post(this);
Where calculateNewRotation()
long now = SystemClock.uptimeMillis();
int deltaTime = (int) (now - mLastRender);
mLastRender = now;
mSpinRotation += deltaTime * SPIN_SPEED_PER_MILLISECOND_CLOCKWISE;
Post(this) refer to MyProgressBar and it will loop for all eternity until I say otherwise.
In my onDraw()
mCamera.save();
mCamera.rotateZ(mSpinRotation);
mCamera.getMatrix(mMatrix);
mCamera.restore();
mMatrix.preTranslate(-mTranslatePivotX, -mTranslatePivotY);
mMatrix.postTranslate(mTranslatePivotX + mCenterX, mTranslatePivotY + mCenterY);
canvas.drawBitmap(mSpinBitmap, mMatrix, mSpinBitmapPaint);
Time for calculateNewRotation() = 1 ms.
Time for onDraw() = 1 ms.
Problem is the overall time is often above 40 ms. My solution is to then use a SurfaceView, but I do not like the idea to have a View with it’s surface drawn with transparent pixels so the underlying surface will be shown instead. What I am looking for is a View that I may get the canvas whenever I want, preferably every 16 ms to get 60 FPS. Now, is there such a View?
What I did in the end was when I have drawn the image I update the rotation value by 1. On fast and slow phones it will look smooth, but the speed will be different. On a fast phone it might draw 60 frames per second which is 60 degrees a second, when a slow phone with 10 frames per second will make the spin go 10 degrees a second. So in the end it is all about if I want them to rotate at the same speed or make it look good, I choose the latter one.
EDIT
I suppose I could put in a max frames per second so fast phones will be throttled and thus making the gap between fast and small phones smaller.