0

I would like to have the app draw several lines for showing user directions by using a background image as a base view and the app "draws" onto it in another layer on top of the background ImageView. Several lines would be made for the objectives to be met, and also in a specific location, so it has to have several layers too right?? I found a trick for drawing a single line using XML

<View 
  android:id="@+id/line"
  android:layout_width="2dip"
  android:layout_height="300dip"
  android:background="#000000"
/>

But that code doesn't show positions and not in a layered form.

Update : I would like the lines to appear through drawing, not through another picture

Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43

3 Answers3

1

You can make a custom SurfaceView class, set the image as background and paint your lines on its Canvas. If I correctly understood what you want to achieve, that's probably the easiest way.

SurfaceView is a widget from the Android framework. Extend it to make your own: MySurfaceView extends SurfaceView. Then override public void onDraw(Canvas canvas), as android_user77 noted. Use this canvas object to paint on. Finally, add your class in the layout xml:

<com.your.app.MySurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/my_image" />

For details as why to use SurfaceView instead of View: Difference between SurfaceView and View?

Community
  • 1
  • 1
Jos van Egmond
  • 228
  • 1
  • 8
  • So that SurfaceView is another Java class? And I call that class' canvas on top of my ImageView? – Kevin Murvie May 23 '15 at 03:35
  • I have updated my explanation. This way, you do not use ImageView. – Jos van Egmond May 23 '15 at 15:54
  • I am sorry, I meant that the app draws a static image by itself.. :( That onDraw is for drawing by user, right? – Kevin Murvie May 24 '15 at 01:57
  • With onDraw you can paint on top of the View, yes. And you can use a View as a layer on top of another. What do you mean by static image? – Jos van Egmond May 24 '15 at 13:31
  • I see, that's what I need, a view on top of a view. What I meant by static image was lines which are drawn by the app itself and the image won't change, for example if I press button "A", the lines drawn by the app are the lines started from "Start" point to point "A" – Kevin Murvie May 25 '15 at 04:11
  • You can still achieve the end result with only 1 View, as I explained in my answer. I tested it, works for me. But if you want a View on top of a View, use a RelativeLayout. Make sure the image is the topmost View. I remember the topmost View in the hierarchy is drawn first, which is a bit counter-intuitive. – Jos van Egmond May 26 '15 at 14:15
  • Yes I realized that it can be done by using just one view, but.. My lecturer asked me to do something a bit more complicated, so that I know what to do if I were to find another problem like this.. I need to use TouchImageView for both of the views though so they can both be zoomed in the same time.. Thanks for the help, I'm going to try it now :D – Kevin Murvie May 26 '15 at 15:36
0

You can extend view and do the drawing by overriding the onDraw method

0

Use FrameLayout to create many "layers". I do not think you want to draw anything on the view. So do not use onDraw(), SurfaceView...

The code is like:

<FrameLayout ...>
<ImageView id="@+id/layer1"...> 
<ImageView id="@+id/layer2"...>
</FrameLayout>
Injury
  • 143
  • 1
  • 1
  • 10
  • Yes, I don't want to draw anything on the view itself, but I would like the app to make lines on top of a view, using the lines' own view.. I would like the lines to appear through drawing, not through another picture, I should update the question – Kevin Murvie May 24 '15 at 01:59