Question

Suppose, we've got a display sized 100x100, an image 100x50, and I want to enlarge that image so that it has the same height as the screen does. So the image will be 200x100. After that I want to make the animation of image movement to the left and to the right. I've found 2 ways of the realization of my task:

  1. To create a new image using Bitmap.createScaledBitmap(). But the image created this way will occupy twice more RAM.
  2. To use matrix.setScale() in every onDraw(). But this works very slow. Is there any other better solution?
Was it helpful?

Solution

The second method is slow because you calculate your "matrix" on every frame! Since your matrix is always the same thing and it does not change, you can pre-calculate your matrix and save it as a "field" in you class, then just use this function to draw it on screen:

canvas.drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)

It should be fast enough. The other option is to use:

canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

Again, try to pre-calculate stuff as much as possible. In this case, pre-calc src and dst rectangles.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top