Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Adding CustomView to OnCreate

I am trying to add a custom view class to go above my camera preview.

But I cant get it to work.Even adding just customview = new CustomView(this) does not work. I get error: CustomView(com.example.android.camera2video.Camera2VideoFragment) CustomView cannot be applied to (com.example.android.camera2video.CameraAvticity)

Here is my code,

CustomView.java

public class CustomView extends SurfaceView {

    private final Paint paint;
    private final SurfaceHolder mHolder;
    private final Context context;

    public CustomView(Camera2VideoFragment context) {
        super(context.getActivity().getBaseContext());
        mHolder = getHolder();
        mHolder.setFormat(PixelFormat.TRANSPARENT);
        this.context = context.getActivity().getBaseContext();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            invalidate();
            if (mHolder.getSurface().isValid()) {
                final Canvas canvas = mHolder.lockCanvas();
                Log.d("touch", "touchRecieved by camera");
                if (canvas != null) {
                    Log.d("touch", "touchRecieved CANVAS STILL Not Null");
                    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                    canvas.drawColor(Color.TRANSPARENT);
                    canvas.drawCircle(event.getX(), event.getY(), 100, paint);
                    mHolder.unlockCanvasAndPost(canvas);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Canvas canvas1 = mHolder.lockCanvas();
                            if(canvas1 !=null){
                                canvas1.drawColor(0, PorterDuff.Mode.CLEAR);
                                mHolder.unlockCanvasAndPost(canvas1);
                            }

                        }
                    }, 1000);

                }
                mHolder.unlockCanvasAndPost(canvas);


            }
        }


        return false;

CameraActivity.java

package com.example.android.camera2video;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

public class CameraActivity extends Activity {
    private  Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customview = new CustomView(this);
        setContentView(R.layout.activity_camera);
        if (null == savedInstanceState) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, Camera2VideoFragment.newInstance())
                    .commit();
        }
    }

}
like image 999
David Avatar asked Jul 15 '26 22:07

David


1 Answers

Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor's formal parameters . So A class contains constructors that are invoked to create objects from the class blueprint .

Do Not

 public CustomView(Camera2VideoFragment context) 
   {
    super(context.getActivity().getBaseContext());  
    ........

Your Constructor expects Activity Context instead of Fragment getactivity .

DO

 public CustomView(Context context) {
 super(context);

FYI

It will be better if you call customview after setContentView.

    setContentView(R.layout.activity_camera);
    customview = new CustomView(this);
like image 191
IntelliJ Amiya Avatar answered Jul 17 '26 14:07

IntelliJ Amiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!