Niko
Niko

Reputation: 8153

Drawing offscreen in Android to canvas performance

My question is about drawing performance. Let's say I have a Bitmap for image width=2400px and height=800px.

My Canvas is only 800px wide and 800px high. View containing the Canvas is a child of HorizontalScrollView so user can scroll to see whole image.

I load the Bitmap once and draw it every frame in onDraw method. Does the "offscreen" drawing cause performance hiccups in this scenario? If so, how to get it smoother?

Thanks.

Upvotes: 2

Views: 878

Answers (1)

Patrick Chan
Patrick Chan

Reputation: 1029

Certainly the Bitmap size is a problem. In typical Android implementation, the texture with dimension over 4000px cannot be rendered. In some Android devices, this limit is 2000px.

Since your objective is to allow user to scroll to see the whole image, if I were you, I won't use HorizontalScrollView. Instead, you should implement a subclass of View and override the onDraw(Canvas canvas) method. You can then detect the touches and modify a Matrix. Such matrix will be used in calling Canvas.drawBitmap(Bitmap, Matrix,Paint)

Specifically for the oversize image problem, upon receiving the Bitmap object, you can slice it into 6 pieces. (2400 x 800 -> 800 x 800 x 6). Then you should control the viewport location and deduce the visible part of the image. In best case, you only need to draw 1 800x800 Bitmap. In worst case, you need to draw 4 800x800 Bitmap.

Upvotes: 2

Related Questions