0

So, question is very simple and asked many times before. But how to make this code working? This example was posted here Extended SurfaceView's onDraw() method never called and the answer was given: setWillNotDraw(false) by Gab Royer.

Thanks in advance.

class CameraPreviewView extends SurfaceView {

protected final Paint rectanglePaint = new Paint();

public CameraPreviewView(Context context, AttributeSet attrs) {
    super(context, attrs);
    rectanglePaint.setARGB(255, 200, 0, 0);
    rectanglePaint.setStyle(Paint.Style.FILL);
    rectanglePaint.setStrokeWidth(2);
}

@Override
protected void onDraw(Canvas canvas){
    canvas.drawRect(new Rect(10,10,200,200), rectanglePaint);
    Log.w(this.getClass().getName(), "On Draw Called");
 }
}

public class CameraPreview extends Activity implements SurfaceHolder.Callback{

private SurfaceHolder holder;
private Camera camera;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    // We remove the status bar, title bar and make the application fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    // We set the content view to be the layout we made
    setContentView(R.layout.camera_preview);

    // We register the activity to handle the callbacks of the SurfaceView
    CameraPreviewView surfaceView = (CameraPreviewView) findViewById(R.id.camera_surface);
    holder = surfaceView.getHolder();

    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {

    Camera.Parameters params = camera.getParameters();

    params.setPreviewSize(width, height);
    camera.setParameters(params);

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        e.printStackTrace();
    }
    camera.startPreview();
}

public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
}

public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();   
  }
}

Log:

04-03 17:55:17.119: E/AndroidRuntime(951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.os.Handler.dispatchMessage(Handler.java:99)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.os.Looper.loop(Looper.java:123)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.app.ActivityThread.main(ActivityThread.java:3683)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at java.lang.reflect.Method.invokeNative(Native Method)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at java.lang.reflect.Method.invoke(Method.java:507)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at dalvik.system.NativeStart.main(Native Method)
        04-03 17:55:17.119: E/AndroidRuntime(951): Caused by: java.lang.ClassCastException: android.view.SurfaceView
        04-03 17:55:17.119: E/AndroidRuntime(951):  at com.example.CameraPreview.onCreate(CameraPreview.java:32)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
        04-03 17:55:17.119: E/AndroidRuntime(951):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
Community
  • 1
  • 1
  • Why doesn't it work, and what were the answers given to the question when it was asked "many times before"? – Jason C Apr 03 '14 at 18:31
  • The program crashes after running. Answers were always: setWillNotDraw(false) in surfaceCreated. I have posted in my question example of "many times before". – user3495080 Apr 03 '14 at 18:44
  • 1
    Well if it crashes, why didn't you say so? What do you mean by "crash"? Is there a stack trace in the log? If so, post it, and clearly label the identified line in your code. Please describe your issue fully and specifically. – Jason C Apr 03 '14 at 18:47

0 Answers0