我在SurfaceView上有一个可以绘制的画布。 现在,按下一个按钮,我要拿画布并制作一个位图,该位图将显示在底部。 那么,要编写位图,我需要编写什么代码?
public class Square extends Activity implements Runnable {
private SurfaceView surface;
private SurfaceHolder holder;
private boolean locker = true;
private Thread thread;
int tmp1 = 0, tmp2 = 0;
Background sfondo;
Canvas canvas;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surface = (SurfaceView) findViewById(R.id.mysurface);
surface.setFocusable(true);
holder = surface.getHolder();
sfondo = new Background((BitmapFactory.decodeResource(getResources(), R.drawable.sfondo)));
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while (locker) {
if (!holder.getSurface().isValid()) {
continue;
}
canvas = new Canvas();
canvas = holder.lockCanvas();
draw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void draw(Canvas canvas) {
sfondo.draw(canvas);
tmp1 = (int)Math.random();
tmp2 = (int)Math.random();
canvas.drawCircle(tmp1, tmp2, 5, Color.BLACK);
}
}
}