Lior Ohana
Lior Ohana

Reputation: 3527

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 image size is exactly as the view size, once the most right column of pixels exceeded the right edge, it will appear on the most left side of the view.

I've thought of several ways of doing it but all of them sounds too complicated to me and I'm sure there is a more "core" way to do that.

Thanks, Lior.

Upvotes: 1

Views: 2098

Answers (2)

user1404512
user1404512

Reputation: 66

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.

Upvotes: 0

slayton
slayton

Reputation: 20319

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);

void shiftPixels(int inPixels[], int bitmapHeight, int nPixHorizShift)
{

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

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

    inPixels = outPixels;
}

Upvotes: 2

Related Questions