Skip to content Skip to sidebar Skip to footer

Battery Consumption With Custom Animated Drawable

Loooong time viewer, finally getting round to signing up here at StackOverflow! After a very long time searching for a way to do a scrolling background of a ViewGroup in Android, I

Solution 1:

After some time with the drawing board, I simplified the functions down. Essentially, it was sending an invalidate() call on every SystemClock.uptimeMillis(), doing one redraw for each step change.

So, I removed the Drawable.Callback interface and passed the invalidateSelf() call directly from the Handler, removing the intermediary interface methods, which didn't seem to do anything special anyway.

There was a slight difference in the CPU usage using drawBitmap(Bitmap source, int X, int Y, Paint p) vs. drawBitmap(Bitmap source, Matrix matrix, Paint p), so I opted for the latter to save cycles.

New methods are as follows:

// Runnable
mInvalidater = newRunnable(){
    @Overridepublicvoidrun(){
        // decrement the drawables step size
        mPosX -= SlidingDrawable.STEP_SIZE;
        if(Math.abs(mPosX) >= mBitmapWidth){
            mPosX = 0;
            mImageMatrix.setTranslate(0, 0);
        }
        mImageMatrix.postTranslate(-STEP_SIZE, 0);
        SlidingDrawable.this.invalidateSelf();
    }
};
// Draw method@Overridepublicvoiddraw(Canvas canvas) {
    canvas.drawBitmap(mBitmap.getBitmap(), mImageMatrix, null);
    mHandler.postAtTime(mInvalidater, SystemClock.uptimeMillis() + 64); 
}

I tested the battery results by unplugging the phone, running the application for around a minute, then opening the battery usage on my Moto Droid. Before, the battery usage surpassed the Display, now it sits comfortably below.

Angry Birds was also a benchmark, by running the opening screen (where the bg scrolls and the birds fly everywhere) for the same amount of time, in some cases my app sat below Angry Birds, but not always.

CPU usage was checked using the ADB shell command dumpsys cpuinfo as there seems to be a problem viewing CPU info on through the DDMS on devices running 2.2.

I'd still be up to hear other thoughts on this, but for now, it's solved.

Post a Comment for "Battery Consumption With Custom Animated Drawable"