自定义视图已调用setOnTouchListener但不覆盖performClick(类扩展Activity NOT View)

时间:2015-08-18 06:30:41

标签: java android opencv

我创建了一个实现一点OpenCV的Android Activity。它的作用是简单地创建一个自定义相机视图,当触摸屏幕时,照片将被保存。

我唯一的问题是BaseLoaderCallback中的代码mOpenCvCameraView.setOnTouchListener(MainActivity.this);包含警告。

mOpenCvCameraView.setOnTouchListener(MainActivity.this);发出警告

  

自定义视图com / example / zcameratestv2 / Version2CameraView已调用setOnTouchListener,但未覆盖performClick

与其他问题不同,我的类扩展了一个Activity而不是View,所以当我尝试覆盖函数private boolean performClick() { ...super.performClick(); }时,它无法识别。这是我的课程

package com.example.zcameratestv2;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.core.Mat;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Toast;

public class MainActivity extends Activity implements CvCameraViewListener, OnTouchListener {

private Version2CameraView mOpenCvCameraView;
private static final String TAG = "Version 2::Activity";

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.setOnTouchListener(MainActivity.this);
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
};

public MainActivity() {
    Log.i(TAG, "Version 2 Class instantiated");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (Version2CameraView) findViewById(R.id.java_surface_view);

    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);

    mOpenCvCameraView.setCvCameraViewListener(this);

}

public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onCameraViewStarted(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void onCameraViewStopped() {
    // TODO Auto-generated method stub

}

@Override
public Mat onCameraFrame(Mat inputFrame) {
    // TODO Auto-generated method stub
    return null;
}

@SuppressLint("SimpleDateFormat")
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //some code....
        break;
    case MotionEvent.ACTION_UP:
        Log.i(TAG,"onTouch event");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String currentDateandTime = sdf.format(new Date());
        String fileName = Environment.getExternalStorageDirectory().getPath() +
                               "/sample_picture_" + currentDateandTime + ".jpg";
        mOpenCvCameraView.takePicture(fileName);
        Toast.makeText(this, fileName + " saved", Toast.LENGTH_SHORT).show();
        v.performClick();
        break;
    default:
        break;
    }
    return true;
 }


}

以前public boolean onTouch(View v, MotionEvent event)事件有类似于OnTouchListener的类似警告,它显示我应该使用performClick();方法,但我不能覆盖它,因为我扩展到Activity而不是View。我发现应用v.PerformClick();修复此问题。

这个其他类处理相机过程

package com.example.zcameratestv2;

import java.io.FileOutputStream;

import org.opencv.android.JavaCameraView;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.AttributeSet;
import android.util.Log;

public class Version2CameraView extends JavaCameraView implements PictureCallback {

private static final String TAG = "Version2::CameraView";
private String mPictureFileName;

public Version2CameraView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void takePicture(final String fileName) {
    Log.i(TAG, "Taking picture");
    this.mPictureFileName = fileName;
    // Postview and jpeg are sent in the same buffers if the queue is not empty when performing a capture.
    // Clear up buffers to avoid mCamera.takePicture to be stuck because of a memory issue
    mCamera.setPreviewCallback(null);

    // PictureCallback is implemented by the current class
    mCamera.takePicture(null, null, this);
}

@Override
public void onPictureTaken(byte[] data, Camera camera) {
    Log.i(TAG, "Saving a bitmap to file");
    // The camera preview was automatically stopped. Start it again.
    mCamera.startPreview();
    mCamera.setPreviewCallback(this);

    // Write the image in a file (in jpeg format)
    try {
        FileOutputStream fos = new FileOutputStream(mPictureFileName);

        fos.write(data);
        fos.close();

    } catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
    }

}
}

我在Manifest文件中包含了所需的权限,例如CAMERA和WRITE_EXTERNAL_STORAGE

有人能确定问题吗?需要你的帮助。提前谢谢!

3 个答案:

答案 0 :(得分:6)

onTouch()方法从未被标记为“已处理”的基础视图获取每个触摸事件。如果您的Version2CameraView未处理触摸事件,则会在“活动”中处理这些事件,并将Version2CameraView作为View v参数传递。

很遗憾,您的JavaCameraView并未覆盖performClick(),但您正尝试在此视图上调用它。解?将此方法添加到Version2CameraView类:

@Override
public boolean performClick() {
    // do what you want
    return true;
}

答案 1 :(得分:0)

由于您的视图已调用setOnTouchListener(),因此您需要添加一种方法来处理触摸事件,并添加默认方法的替代,请务必在内部调用super方法,以避免类似“方法未替代”的错误方法”,如果您不打算在那里处理触摸事件,则可以像我的示例一样保留它:

@Override
public boolean performClick() {
    super.performClick();
    return true;
}

答案 2 :(得分:0)

我认为这有点晚了,但是解决方案很简单。像ButtonTextView这样的类是View的子类,而SurfaceViewJavaCameraViewVersion2CameraView子类的父级。

那有什么区别? 区别在于TextView实现了“ ViewTreeObserver.OnPreDrawListener”接口。因此,Button(TextView的子类)可以覆盖performClick()方法。

如何处理我的Version2CameraView课程? 只需实现一个接口即可覆盖performClick()方法。哪一个是“ ViewTreeObserver.OnPreDrawListener”?不,您应该像这样使用SurfaceHolder.Callback

public class Version2CameraView extends JavaCameraView implements PictureCallback, SurfaceHolder.Callback 
{
    ...
   /**
     * Process the MotionEvent.
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (getHolder()) {
            // Do something
            ...
            return true;
        }
    }

    @Override
    public boolean performClick() {
        // Calls the super implementation, which generates an AccessibilityEvent
        // and calls the onClick() listener on the view, if any
        super.performClick();

        // Handle the action for the custom click here

        return true;
    }
    ...
}

此解决方案必须删除您的警告。