我在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);
}
}
}
答案 0 :(得分:0)
private Bitmap generateBitmap(){
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
surface.draw(canvas);
return bitmap;
}
然后你可以使用位图。
另一种解决方案就是
Bitmap bitmap = surface.getDrawingCache()
答案 1 :(得分:0)
要Lollo使用 {} 这是可读代码
@Override public void run() {
while(locker){
if(!holder.getSurface().isValid()){
continue;
}
AA = Bitmap.createBitmap(surface.getWidth(), surface.getHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(AA);
canvas = holder.lockCanvas();
draw(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void draw(Canvas canvas) {
tmp1, tmp2 = (int)Math.random(...);
canvas.drawCircle(tmp1, tmp2, gra, black);
}