Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimize Android canvas background drawing by caching matrix
    primarykey
    data
    text
    <p>I have a SurfaceView canvas I'm drawing a scene into. The canvas is transformed to accomodate the logical scene size which is 1024x768 (while the actual screen is 800x480), and also to support zoom/scroll navigation on it. Since I did not want black stripes on the sides (fitting 4:3 to 16:9) I'm using a different background bitmap which has "stretched" sides to cover the ratio difference. Another twist in the plot, is that I need to support both phones and tables. For tablets I'm loading a higher quality (size) image and for phones I'm scaling down the background bitmap when loading it from disk. Below is code that does what I want (although probably not the best way), and in particular, I have a feeling the background calculations can be cached into a matrix to be used later in <code>canvas.drawBitmap(Bitmap, Matrix, Paint);</code></p> <p>(Also, I couldn't get the equivalent positioning of drawBitmap(Bitmap, left, top, Paint) by translating the canvas with these offsets, I'm also curious why).</p> <p>This is the draw routine:</p> <pre><code>public void draw(Canvas canvas) { canvas.save(); float midX = width/2; float midY = height/2; // zoom out to fit logical view, and translate for future // sprite drawing with logical coordinates // x,y and zoom are calculated in on init and updated in touch events canvas.translate(x, y); canvas.scale(zoom, zoom, midX, midY); // background part if(back != null &amp;&amp; !back.isRecycled()) { // todo: these can probably be pre-calculated for optimization // todo: but so far I couldn't get it right.. if(bgMatrix != null) { // this is where I'm thinking of using the cached matrix canvas.drawBitmap(back, bgMatrix, paint); } else { float offsetW = (width-back.getWidth())/2; float offsetH = (height-back.getHeight())/2; canvas.save(); // bgScaleFactor is calculated upon setting the bg bitmap // it says by how much we need to scale the image to fill the canvas // taking into account the image (possible) downscale canvas.scale(bgScaleFactor, bgScaleFactor, midX, midY); // this doesn't work: canvas.postTranslate(offsetW, offsetH) and use 0,0 for next draw canvas.drawBitmap(back, offsetW, offsetH, paint); // todo: here I would like to save a matrix which represents // how the back bitmap was drawn onto the canvas // so that next time these calculations can be avoided // this fails: bgMatrix = canvas.getMatrix(); canvas.restore(); } // draw scene shapes on transformed canvas if(shapes != null){ shapes.onDraw(canvas); } canvas.restore(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload