I have a bitmap with the size of 400 x 70 pixels. I would like to scroll this bitmap up by one row and fill up row 70 with new pixels. I have tried to scroll the bitmap by copying it to an IntBuffer , copy it to another IntBuffer (starting at the second row) and the then write it back to the bitmap. This works but it takes a lot of CPU power and it is 开发者_如何学JAVAkind of slow. Is there any faster way to do this?
private Bitmap shift(Bitmap bitmap)
{
buffer.rewind();
buffer1.rewind();
bitmap.copyPixelsToBuffer(buffer);
buffer.position(bitmap.getWidth());
buffer1.put(buffer.array(), bitmap.getWidth()*2, bitmap.getWidth() * bitmap.getHeight());
buffer1.rewind();
bitmap.copyPixelsFromBuffer(buffer1);
return bitmap;
}
This is one way to do it using canvas:
canvas.drawBitmap(bgr, bgrPosition, 0, null);
canvas.drawBitmap(bgr, 0 - bgrW + bgrPosition, 0, null);
//next value for the background's position
if ( (bgrPosition += bgrDx) > bgrW){
bgrPosition = 0;
}
bgrDx is the speed of scrolling try bgrDx = 1, while bgrW is the width of the bgr bitmap. This method repeats the image and produces an infinite scroll, however you would need to use a looped image (the same on both ends) to make the scroll look seamless. Perhaps you should experiment with this and see if it performs any better for you.
精彩评论