我正在尝试添加自定义视图类以超越我的相机预览。
但我无法让它工作。即使只添加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 :(得分:2)
构造函数允许在匹配时进行扩展转换 具有底层构造函数的newInstance()的实际参数 形式参数。因此,一个类包含被调用以从类蓝图创建对象的构造函数。
不
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);
答案 1 :(得分:1)
您已customview = new CustomView(this);
创建了自定义视图,但未将此视图添加到布局中。您必须将此视图添加到所需的位置。也许将它添加到您的根ViewGroup。换句话说,您还没有在任何地方使用过此自定义视图,这就是为什么它没有显示。
将构造函数更改为此..
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));
答案 2 :(得分:1)
它不起作用,因为您的自定义视图构造函数采用Camera2VideoFragment类而不是活动。
public CustomView(Camera2VideoFragment context) {...}
当您致电new CustomView(this/* is Activity not Camera2VideoFragment */)
时,您实际上希望Camera2VideoFragment
传递一个Activity实例。两者之间没有继承关系,因此会出现编译时错误
为了使它工作,你有3个选择:
Camera2VideoFragment
Camera2VideoFragment
更改为Activity
Camera2VideoFragment