Android OnHoverListener:应用程序在视图区域内移动鼠标时崩溃

时间:2017-05-31 17:34:10

标签: android crash hover mouse

编辑:

我初始化了canvasBitmap = Bitmap.createBitmap(500,300,Bitmap.Config.RGB_565); drawCanvas = new Canvas(canvasBitmap);应用程序不会崩溃,但也不会绘制任何内容..我在HOVER MOVE案例中使用canvas.drawcolor(Color.RED)测试它。现在,从崩溃中学习我知道它正在识别我的悬停运动..那么为什么现在什么也没发生?任何想法?

我有一个连接了usb鼠标的android 5.1开发板。当鼠标移动到视图窗口内时,我需要执行一些绘图操作。 我知道android工作室模拟器不能用于悬停,所以我的所有测试都在开发板本身上

我在我的OnCreate方法中添加了一个悬停监听器。我基本上有一个透明的视图坐在camera2视频(textureview)的顶部。试着听听。 一旦我在应用程序中移动鼠标,它就会崩溃。所以它正在检测悬停。崩溃的原因需要一些帮助。这里是所有相关的代码:Activit包含Oncreate,我的CustomView和我的XML

public class CameraActivity extends Activity {
    private Canvas drawCanvas;
    private Bitmap canvasBitmap;
    private Paint canvasPaint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        if (null == savedInstanceState) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, Camera2BasicFragment.newInstance())
                    .commit();
        }


        View container = findViewById(R.id.container);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
        container.setOnHoverListener(new View.OnHoverListener() {

            @Override
            public boolean onHover(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_HOVER_ENTER:

                        break;
                    case MotionEvent.ACTION_HOVER_MOVE:

                        drawCanvas.drawCircle(50,50,100,canvasPaint);
                        break;
                    case MotionEvent.ACTION_HOVER_EXIT:

                        break;
                }
                return false;
            }
        });
    }



}

customview:

public class CustomView extends View{

    //drawing path
    private Path drawPath;
    //drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    //initial color
    private int paintColor = 0xff00ff00;
    //canvas
    private Canvas drawCanvas;
    //canvas bitmap
    private Bitmap canvasBitmap;

    //constructor
    public CustomView(Context context){
        super(context);
        setupDrawing();
    }
    public CustomView(Context context, AttributeSet attrs){
        super(context, attrs);
        setupDrawing();
    }

    //prepare drawing
    private void setupDrawing(){
        drawPath = new Path();
        drawPaint = new Paint();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(10);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }

    //view assigned size
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    //draw view
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    //respond to touch interaction
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();
        //respond to down, move and up events
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(touchX, touchY);
                break;
            case MotionEvent.ACTION_UP:
                drawPath.lineTo(touchX, touchY);
                drawCanvas.drawPath(drawPath, drawPaint);
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // this code will be executed after 2 seconds
                        drawCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

                    }
                }, 2000);

                drawPath.reset();
                break;
            default:
                return false;
        }
        //redraw
        invalidate();
        return true;
    }

}

的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.android.camera2basic.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <com.example.android.camera2basic.CustomView
        android:id="@+id/texture2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />



    <FrameLayout
        android:id="@+id/control"
        android:layout_width="match_parent"
        android:layout_height="112dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:background="@color/control_background">

        <Button
            android:id="@+id/picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/picture" />

        <ImageButton
            android:id="@+id/info"
            android:contentDescription="@string/description_info"
            style="@android:style/Widget.Material.Light.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:padding="20dp"
            android:src="@drawable/ic_action_info" />

    </FrameLayout>

</RelativeLayout>

0 个答案:

没有答案