Add a touch listener to images on a rotated canvas

So, I'm stuck on this for about a week ...

I followed this guide: https://code.tutsplus.com/tutorials/android-sdk-augmented-reality-camera-sensor-setup--mobile-7873

And I got images that are dynamically drawn on the canvas, which is then rotated by the rotation matrix that I get from the sensors.

What I'm trying to do is add a listener to the ImageView, which I draw and rotate on the canvas, but I cannot figure out how to do this.

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 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(); } } 
+6
source share

Source: https://habr.com/ru/post/1013386/


All Articles