2
I created a subclass of Surfaceview to abstract canvas operations, such as drawBitmap (among others), as shown below:
public class MyView extends SurfaceView {
public MyView(Context c) {
super(c);
this.canvasHolder = this.getHolder();
this.canvasHolder.addCallback(surfaceHolderCallback);
}
public void drawBitmap(Bitmap bitmap, float width, float height, Paint p) {
canvas.drawBitmap(bitmap, width, height, p);
}
private SurfaceHolder.Callback surfaceHolderCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
canvas = holder.lockCanvas();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
};
public void flush() {
this.canvasHolder.unlockCanvasAndPost(this.canvas);
this.canvas = this.canvasHolder.lockCanvas();
}
The full startup of Surfaceview depends on the callback, in which I can access the canvas I will need to manipulate. Otherwise, I get a Nullpointerexception on the first call to drawBitmap.
The problem is that I want to allow the use of the class very directly, as shown below. To do this, I need to abstract this entire boot mechanism with callback and ensure that drawBitmap has access to a valid canvas (not null).
Any idea how to do that?
MyView view = new MyView(getBaseContext());
String source = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/abc.png";
view.drawBitmap(BitmapFactory.decodeFile(source).copy(Bitmap.Config.ARGB_8888, true), 0, 0, null);
view.flush();
Thank you for your reply. In fact, I am aware of the possibility of adding the Ner, but I cannot do it, because both the creation of the object and the call to functions are made via reflection, in code coming from another language, synchronously. A very specific context. . =(
– Jemerson Damásio