I am also looking for a solution to this. I need to make a mark on the image where the user is touching. I am also going to remotely control a device with this information. What I found as a solution, which I will present below. I ran into a problem as dpi screens and devices change this marking operation. I am trying to improve the method. But also, perhaps this is useful for someone.
Here he is:
1) Method: First create an image using Gimp or Photoshop (or ...). Place the image in the layout. Place the image in this image with the original size. (this is the weak point number 1). For onTouchEvent, add the following code. What code will do is create 2 drawings. Create 2 layers. The 1st layer is the image (above), and the second layer is the sign. Then place the label where the user touches. (this is the weak point number 2).
2) Code:
Then
//OnCreateView ptable = (ImageView)rootView.findViewById(R.id.img_ptable); layers = new Drawable[2]; layers[0] = r.getDrawable(R.drawable.pratik); layers[1] = r.getDrawable(R.drawable.pratik); LayerDrawable ldr = new LayerDrawable(layers); ptable.setImageDrawable(ldr);
Then
ptable.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { String text = "Click X:"+ String.valueOf(event.getX())+ " Y:" + String.valueOf(event.getY()); //Dokunulan yere bir işaret oluştur. Bitmap mark = Bitmap.createBitmap(600, 400,Bitmap.Config.ARGB_8888); Canvas ca = new Canvas(mark); Paint pa = new Paint(); pa.setStyle(Paint.Style.STROKE); pa.setAntiAlias(true); //pa.setStrokeCap(Paint.Cap.ROUND); pa.setStrokeWidth((float)3); pa.setARGB(251, 20, 20, 20); float cx = event.getX() + 10 + (8*event.getX()/100); float cy = event.getY() + 10 + (6*event.getY()/100); ca.drawCircle(cx, cy, 13, pa); BitmapDrawable bmp = new BitmapDrawable(getResources(), mark); layers[1] = (Drawable)bmp; LayerDrawable ldr = new LayerDrawable(layers); ptable.setImageDrawable(ldr); return true; } });
This code works, but only works with a specific screen size and pixel density. I am going to improve this.
source share