You can use a custom drawable that simply draws rectangles on the canvas. Below is a basic example, just use it as
BackgroundDrawable bg = new BackgroundDrawable();
anyView.setBackground(bg);
bg.start();
Here is a basic working implementation:
public class BackgroundDrawable extends Drawable implements Runnable, Animatable {
private static final long FRAME_DELAY = 1000 / 60;
private boolean mRunning = false;
private long mStartTime;
private int mDuration = 1000;
private Paint mPaint;
private int mStripes = 7;
private void init() {
if (mPaint == null) {
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
}
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
if (isRunning()) {
final int save = canvas.save();
long timeDiff = SystemClock.uptimeMillis() - mStartTime;
canvas.clipRect(bounds);
float progress = ((float) timeDiff) / ((float) mDuration);
float width = bounds.width() / (mStripes * 2);
for (int i = 0; i < mStripes * 2 + 2; i++) {
mPaint.setColor(i % 2 == 0 ? Color.RED : Color.WHITE);
canvas.drawRect(bounds.left + width * (i - 1) + progress * 2 * width, bounds.top, bounds.left + width * i + progress * 2* width, bounds.bottom, mPaint);
}
canvas.restoreToCount(save);
} else {
}
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
init();
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return 0;
}
@Override
public void start() {
if (mRunning) stop();
mRunning = true;
mStartTime = SystemClock.uptimeMillis();
invalidateSelf();
scheduleSelf(this, SystemClock.uptimeMillis() + FRAME_DELAY);
}
@Override
public void stop() {
unscheduleSelf(this);
mRunning = false;
}
@Override
public boolean isRunning() {
return mRunning;
}
@Override
public void run() {
invalidateSelf();
long uptimeMillis = SystemClock.uptimeMillis();
if (uptimeMillis + FRAME_DELAY < mStartTime + mDuration) {
scheduleSelf(this, uptimeMillis + FRAME_DELAY);
} else {
mRunning = false;
start();
}
}
}
, : .