Peter
Peter

Reputation: 11920

How to optimize bitmap draw in Android?

In my Android app, I receive a remote video stream in a proprietary format. As a new network packet arrives, I decode it natively and obtain a bitmap. In the UI, this bitmap is displayed in an ImageView control via setImageBitmap() method.

I am able to process 12 to 14 frames per second. I am wondering if there is a way to optimize this in terms of performance. Perhaps I can use hardware acceleration, if possible.

Upvotes: 0

Views: 350

Answers (1)

sockeqwe
sockeqwe

Reputation: 15929

Why not using a VideoView that already does all that kind of things for you?

However:

  • I think you should use SurfaceView instead of ImageView. With SurfaceView you have your own dedicated thread that is just responsible for drawing on the canvas. Therefore redrawing is faster.

  • I guess you are encoding a single Frame bitmap in an async thread which is completely fine. But you should think about how to parallelize that. Basically use a ThreadPool or something similar that decodes your video to single Bitmaps. Put them in a Buffer which can be a Fifo, List or Queue or whatever, but just keep in mind that one thread can be faster than another, so you better give every decoding task an order number so that you can add the encoded bitmap in correct order into the Buffer. The SurfaceView next should always take the first image from the buffer and display it on screen.

  • Schedule the SurfaceView Thread redrawing to achive 60 frames per seconds. So not redraw over and over again in a while-loop think about a smarter (scheduled) solution.

  • Only redraw if the Buffer contains enough images (you have to determine the number of images in the buffer by your own or google it). Otherwise display a "loading / buffering" animation

  • Garbage Collection can take some time, so try to reduce the time garbage collection runs. Bitmap association can consume a lot of memory and therefore garbage collector needs to run often to find free memory for the next bitmap. So think about the video source file if you can deliver it with a width / height that is more suitable for smartphones (maybe different files for different display sizes). Think about how you can decrease memory consumption. There are various options in BitmapFactory that you can use to reduce memory consumption (which reduces running garbage collection) like inSampleSize or inBitmap which seems a good choice since all bitmaps should have the same width / height. Have a look here. You could also reduce the Bitmap quality by using BitmapConfig.RGB_565.

Upvotes: 1

Related Questions