10

My idea is to have two surfaceViews. One surfaceView which will hold an image (say ImageSurgaceView) and second surface that lie on top of the first one which holds the annotations (say AnnotationSurfaceView) like circle, rectangle etc. Now I have to map these surface views such that the height, width are same and moreover, both the surface view should move on drag i.e share the touch events. To be precise, when I move the image up the annotations should also move up.

Now, I am not sure if this is possible in android. Can any one tell or share some info on this. I don't have any implementation idea to this and hence I don't have any specific code. Moreover, I couldn't find anything similar.

Thanks for helping.

Shobhit
  • 407
  • 5
  • 15

1 Answers1

21

Yes, this is possible. You can add both SurfaceViews as children of a FrameLayout. You will also want to call setZOrderMediaOverlay on one or both of your SufaceViews in order to specify how they are layered.

Furthermore, this could be a graphic intensive algorithm you are describing. Consider adding the AndroidManifest.xml application attribute android:hardwareAccelerated="true".

Finally, just set up a custom OnTouchListener to handle drag events. Use MotionEvent.getRawX() and MotionEvent.getRawY() to get the touch point, and use this to manipulate the canvases.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • I am unable to add both the surfaceviews to the framelayout. It says, I have to call **removeView()** before adding another child. Also, while setting the **android:HardwareEnabled="true"**, I am getting another error which says **error:No resource identifier found for attribute 'HardwareEnabled' in package 'android'**. – Shobhit Aug 22 '13 at 10:14
  • @Shobhit, the attribute was a typo, which I fixed. Try `android:hardwareAccelerated="true"`. As for your first problem - this sounds like you are using a `ScrollView` instead. Can you add your layout (`XML` or programmatic) to the question? – Phil Aug 22 '13 at 13:06
  • hey Phil, there was a small bug and now I was able to get that working but only partially. I have to still implement the custom OnTouchListener. Do you by any chance have any link to the example code? If yes, could you please share? I shall post the current code as my answer. Thanks! :) – Shobhit Aug 22 '13 at 19:32
  • @Shobhit, there are numerous examples on *StackOverflow*, but [this one](http://stackoverflow.com/questions/9398057/android-move-a-view-on-touch-move-action-move) seems most promising. – Phil Aug 22 '13 at 19:36
  • I was successful in writing custom `onTouchListener`. I will post it as the answer below. But I would like to correct you at one place. `MotionEvent.getRawX/Y()` is automatically adding(atleast in my case) some extra digits to the exact touch points for example, the effective x is touchpoint+200 and the effective y is touchpoint+50. However, I used MotionEvent.getX/Y() which gives my the exact touch points. I am now able to get the touchpoints in both the surface views. I have to anyway expand the custom `onTouchListener` to accomplish my needs. Thanks a lot for the help. – Shobhit Aug 23 '13 at 16:23
  • 2
    One minor addition: setZOrderMediaOverlay() should be called in OnCreate() or it will have no effect – vovan888 Jun 20 '17 at 19:35