Skip to content Skip to sidebar Skip to footer

Android - Background Image Animation In Loop

I'm trying to animate an image (used as background image but it doesn't really matter) in such a way that it will move from left to right but in a cyclic way. For example, if the i

Solution 1:

If the Bitmap and the ImageView are the exact same size in pixels you can manually move the pixels around however you want.

int pixels[];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
shiftPixels(pixels, width, height);
bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.setWidth());

In shiftPixels simply create a copy of the original bitmap but when copying the pixels apply a linear shift to the pixels. Something like this (un-tested pseudo-code);

voidshiftPixels(int inPixels[], int bitmapHeight, int nPixHorizShift)
{

    intshift= bitmapHeight * nPixHorizShift;
    int outPixels[inPixels.size()];

    fori=1:pixels.size()
       outPixels[(i + shift) % outPixels.size()] = inPixels[i];

    inPixels = outPixels;
}

Solution 2:

Could you use canvas.drawBitmap(sourceRectangle,outputRectangle,null) to cut a part from a strip bitmap background? This is a similar method that is used to animate a bitmap, but you would add one pixel to the sourceRectangle's left and right values until it hits the edge of the background strip.

Post a Comment for "Android - Background Image Animation In Loop"