I would like to use Canvas to “shift a bitmap”; to create a Bitmap from a Bitmap, but with a y offset such that all the pixels in the Bitmap are either shifted down (with blank pixels at the top) or shifted up (with blank pixels at the bottom). I am using the below code to do this. The code works fine as long as I am shifting up (shiftY is negative), but it gives a garbage bitmap if I try to shift down.
The second set of code is my work around for this, but this has the very undesirable effect of doubling my memory usage.
Is there any way to shift a Bitmap whitout using a second Bitmap?
//create canvas from the current Bitmap.
Canvas canvas = new Canvas (m_Bitmap);
/*draw into the current Bitmap into the canvas with an offset, thereby drawing over itself
shifted pixels*/
canvas.drawBitmap(m_Bitmap, 0, shiftY, null);
`
//create the canvas from a temp bitmap
Canvas canvas = new Canvas (m_2ndBitmap);
//draw the shifted pixels into the temp bitmap
canvas.drawBitmap(m_BackBuffer, shiftX, shiftY, null);
//swap the bitmaps
Bitmap temp = m_Bitmap;
m_Bitmap = m_2ndBitmap;
m_2ndBitmap = temp;
You can do it yourself if you copy row by row from the bottom. The problem is that your source and destination are the same and it’s copying from the top, destroying the rows beneath before you can copy them lower.