2017-05-27 80 views
1

我正在尝试添加自定义视图类以超出我的相机预览。Android:将自定义视图添加到OnCreate

但我无法得到它的工作。即使只加入customview = new CustomView(this)不起作用。我得到错误:CustomView(com.example.android.camera2video.Camera2VideoFragment)CustomView不能适用于(com.example.android.camera2video.CameraAvticity)

这里是我的代码,

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(); 
     } 
    } 

} 
+0

您制作了一个构造函数,它将Camera2VideoFragment作为参数,但在代码中将其传递给MainActivity实例。 – RobCo

+0

阅读https://stackoverflow.com/questions/12273976/camera-tutorial-for-android-using-surfaceview –

回答

1

它不工作,因为你的自定义视图的构造需要Camera2VideoFragment类,而不是一个活动。

public CustomView(Camera2VideoFragment context) {...} 

当你调用new CustomView(this/* is Activity not Camera2VideoFragment */)你当它实际上预计Camera2VideoFragment传递活动的一个实例。这两者之间没有继承关系,因此你会得到编译时错误。
为了使它工作,你有3种选择:

  1. 传递Camera2VideoFragment
  2. 变化从Camera2VideoFragment构造函数参数的实例来Activity
  3. 让您的活动延长Camera2VideoFragment
+0

将其更改为customview = new CustomView(Camera2VideoFragment.newInstance());很好? – David

+0

它应该是好的。它工作吗? – Tudor

+0

现在没有错误,但是当我添加到布局我得到错误:未能实例化一个或多个类。 CustomView类没有实例化 – David

1

呦您已通过customview = new CustomView(this);创建了自定义视图,但您并未将此视图添加到您的布局中。你必须在你想要的地方添加这个视图。也许把它添加到你的根ViewGroup。换句话说,你没有在任何地方使用这个customView,这就是为什么它没有显示。

构造函数改成这样..

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

} 

将其添加到您的rootview

final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0); 
viewGroup.addView(new CustomView(this)); 
+0

甚至在添加到布局之前,添加customview =新的CustomView(this);到OnCreate,我得到错误:自定义视图(com.example.android.camera2video.Camera2VideoFragment)CustomView不能应用于(com.example.android.camera2video.CameraAvticity) – David

+0

我改变了类,如你所说,你能告诉我什么改变OnCredate方法PLZ? – David

+0

我给你一个创建视图组代码。使用它。 –

1

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 .

请勿

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

Constructor预计Activity Context instead of Fragment getactivity

DO

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

FYI

这将是更好,如果你以后的setContentView调用customview

setContentView(R.layout.activity_camera); 
    customview = new CustomView(this); 
+0

得到它的感谢..现在它运行,但我看到黑色的布局,当我触摸它的应用程序崩溃,生病有关它的问题。关于这个Therad的主题 – David

相关问题