您可以使用只繪製矩形到畫布上的自定義繪製在畫布上的條紋。下面是一個基本的樣品,只是使用它像
BackgroundDrawable bg = new BackgroundDrawable();
anyView.setBackground(bg);
bg.start();
這裏的基本工作落實:
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()) {
// animation in progress
final int save = canvas.save();
long timeDiff = SystemClock.uptimeMillis() - mStartTime;
canvas.clipRect(bounds);
float progress = ((float) timeDiff)/((float) mDuration); // 0..1
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 {
// todo draw normal
}
}
@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();
}
}
}
另外,我寫了一篇關於可繪製和基本的動畫處理這裏的詳細說明:Custom drawables and animations。
怎麼樣:創建一個白色的背景。在所需寬度的屏幕上等間隔添加紅色矩形。在屏幕左側創建一個矩形的另一個矩形。將所有條紋的位置向右轉換一個像素,並循環等於以像素爲單位的柄寬度的次數。然後重新開始,因爲您將再次回到您的初始圖像。 –