在旋转的画布上为图像添加触摸侦听器

时间:2016-12-21 11:34:57

标签: android canvas listener

所以我现在已经坚持了一个星期了......

我已经按照本教程: https://code.tutsplus.com/tutorials/android-sdk-augmented-reality-camera-sensor-setup--mobile-7873

我在Canvas上动态绘制图像,然后通过旋转矩阵旋转,我从传感器获取。

我想要做的是添加侦听器到ImageView我正在绘制并在画布上旋转,但我无法想办法。

if (lastAccelerometer != null && lastCompass != null) {
    boolean gotRotation = SensorManager.getRotationMatrix(rotation,
            identity, lastAccelerometer, lastCompass);
    if (gotRotation) {
        float cameraRotation[] = new float[9];
        // remap such that the camera is pointing straight down the Y
        // axis
        SensorManager.remapCoordinateSystem(rotation,
                SensorManager.AXIS_X, SensorManager.AXIS_Z,
                cameraRotation);

        // orientation vector
        float orientation[] = new float[3];
        SensorManager.getOrientation(cameraRotation, orientation);

        // draw horizon line (a nice sanity check piece) and the target (if it's on the screen)
        canvas.save();
        // use roll for screen rotation
        canvas.rotate((float) (0.0f - Math.toDegrees(orientation[2])));

        // Translate, but normalize for the FOV of the camera -- basically, pixels per degree, times degrees == pixels
        float dx = (float) ((canvas.getWidth() / horizontalFOV) * (Math.toDegrees(orientation[0]) - currentBearing));
        float dy = (float) ((canvas.getHeight() / verticalFOV) * Math.toDegrees(orientation[1]));

        // wait to translate the dx so the horizon doesn't get pushed off
        canvas.translate(0.0f, 0.0f - dy);

        // now translate the dx
        canvas.translate(0.0f - dx, 0.0f);

        ImageView img = new ImageView(context);
        img.setBackgroundResource(R.drawable.frame);
        img.setDrawingCacheEnabled(true);
        img.setScaleX(0.4f);
        img.setScaleY(0.3f);
        img.setAlpha(0.5f);
        img.layout(0, 0, 270, 185);


        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Starting new intent
                Intent in = new Intent(getContext(),
                        PlaceInfo.class);
                // Sending place refrence id to single place activity
                // place refrence id used to get "Place full details"
                in.putExtra(KEY_REFERENCE, reference);
                getContext().startActivity(in);
            }
        });

        Paint boxPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        boxPaint.setAlpha(85);

        // >> 1 is equivelant to /2

        TextView textView = new TextView(context);
        textView.setText(directionPOIs.get(i));
        textView.setTextColor(Color.WHITE);
        textView.setDrawingCacheEnabled(true);
        textView.setTextSize(11);
        textView.setWidth(270);
        textView.setHeight(185);
        textView.setSingleLine(false);
        textView.layout(0, 0, 270, 185);

        int height = this.getHeight();


        double elev = elevationListArray.get(i) - MainActivity.getMyElevation();
        double elevPercentage = elev / MainActivity.getMyElevation();


        int yPos = (int) Math.round(height * elevPercentage);

        int middleY = (int) height >> 1;

        if (yPos < 0) {
            yPos = middleY - Math.abs(yPos);
        } else {
            yPos = middleY + yPos;
        }
        reference = referenceNumArray.get(i);

        canvas.drawBitmap(img.getDrawingCache(), canvas.getWidth() >> 1, yPos, boxPaint);
        canvas.drawBitmap(textView.getDrawingCache(), (canvas.getWidth() >> 1) + 20, yPos, null);
        img.setDrawingCacheEnabled(false);
        textView.setDrawingCacheEnabled(false);
        canvas.restore();
    }
}

0 个答案:

没有答案