Android SurfaceView多次绘制位图

时间:2017-09-26 15:24:36

标签: android surfaceview surfaceholder

我试图将一个位图绘制到surfaceView中我可以成功绘制但是我需要根据其他一些用户移动在屏幕上移动该位图但是当我设置

 canvas.drawColor(Color.TRANSPARENT);
 canvas.drawBitmap(bitmap, left, top, null);

它在屏幕上多次绘制相同的位图。

enter image description here

但是当我这样做时

 canvas.drawColor(Color.GREEN);
 canvas.drawBitmap(bitmap, left, top, null);

它正常工作只绘制一个位图并移动它,但我需要透明背景而不是彩色。

enter image description here

CODE

公共类DotsSurfaceView扩展了SurfaceView实现SurfaceHolder.Callback {     私人SurfaceHolder持有人;     私有布尔创建;     位图位图;

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // draw();
    created = true;
}

@Override
// This is always called at least once, after surfaceCreated
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // draw();
    created = true;
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

public DotsSurfaceView(Context context) {
    super(context);
    holder = getHolder();         
    holder.addCallback(this);
    holder.setFormat(PixelFormat.TRANSPARENT);
    Drawable drawable = ARTrackingActivity.contexti.getDrawable(R.drawable.ic_tune_black_24px);
    bitmap =Utils.drawableToBitmap(drawable);
}

public void draw(float left, float top) {
    Canvas canvas = null;
    try {
        canvas = holder.lockCanvas();

        synchronized (holder) {
            draw2(canvas, left, top);
        }
    } finally {
        if (canvas != null) {
            holder.unlockCanvasAndPost(canvas);
        }
    }
}


public void draw2(Canvas canvas, float left, float top) {
    if (created) {
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(bitmap, left, top, null);
    }
}

}

0 个答案:

没有答案