Reputation: 497
In order to support different size of screen resolutions, I programmatically scale the bitmaps in my android game application using SurfaceView. I performed it by using drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
method. I saw a Bitmap
method named as createScaledBitmap that can create a new bitmap scaled from an existing bitmap. Is there a performance difference between them?
Upvotes: 0
Views: 1585
Reputation: 10288
Creating a scaled bitmap does not draw the bitmap. It provides a means to include options before drawing. For example you can use different interpolation techniques depending on the size, rotation, etc. of the original image vs. where it will be drawn:
What does the filter parameter to createScaledBitmap do?
So if you are not satisfied with the results of drawBitmap, you can try to improve the Bitmap with scaling options. drawBitmap uses "automatic" scaling but does not specify what is used.
Specifically related to performance, createScaledBitmap uses the native method "nativeCreate" and drawBitmap uses "native_drawBitmap" - so it will vary by platform implementation of the native methods. Regardless, you will need to draw the bitmap onto a canvas once it's scaled, so "drawBitmap" will be required either way and even if the bitmap is already scaled it's very unlikely to improve performance. However, if you are not drawing it, then there's no point in using "drawBitmap" because it autoscales and uses resources to perform the draw.
Upvotes: 2
Reputation: 93726
Yes, there is. They do different things. createScaledBitmap takes a bitmap and creates a new scaled copy in memory. It does not place it on a canvas, this is a new bitmap object that can later be drawn to a canvas. drawBitmap draws the bitmap to the canvas (which may be backed by a bitmap, surface, or the screen), scales it, applies effects from the paint object, respects clipping regions, etc.
You should not use drawBitmap unless you actually want to draw it to a canvas- using it just to scale is inefficient. If you need to draw it and scale it- if you'll need to scale it repeatedly and memory isn't an issue, use createScaledBitmap first and then draw that scaled bitmap. If you don't need to draw it again or memory is an issue, use drawBitmap to scale it as you draw it.
Upvotes: 2