本文純屬個人總結記錄,新手不懂的可以借鑑下,老鳥勿噴!!!
先上效果圖:
PS:gif可能看起來有點卡,這是通病,哈哈哈。。。
該篇博客將的是下面綠色的效果,廢話不多說,直接上代碼!
public class ScanView extends View {
private Paint mPaint;
private int scanTop;//開始掃描點
private int scanSpeed;//掃描的速度
private boolean isStart;
public ScanView(Context context) {
this(context, null);
}
public ScanView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ScanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLUE);
scanTop = 0;
scanSpeed = 2;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GREEN);
canvas.drawRect(0,scanTop, getRight(), scanTop + 10, mPaint);
if(isStart){
scanTop += scanSpeed;
if(scanTop >= getHeight()){
scanTop = 0;
}
invalidate();
}
}
public void startScan(){
isStart = true;
invalidate();
}
}
原理:其實很簡單——就是簡單下移,通過不斷修改top的值來進行掃描的,所以這篇文章叫《簡單直線掃描效果》,我這裏是畫的Rect來當做直線使用進行掃描的,個人感覺這樣方便點,也可以用bitmap來做掃描線的(用過zxing的都知道)。
寫這篇博客的原因是做個記錄,以方便實現圖中上面的效果,後面會寫上面的自定義View實現的過程。
如有問題,請指出,萬分感謝!