0

I have an existing layout that I am setting my current activity to. However, I want to draw a line (horizontal) and move it down in slow motion. Most articles talk about creating custom view and doing setContentView(myView) .

How ever I dont want to set my activity view to only this view. I already did setContentView(R.layout.main). And I just want to draw the line on top of moving contents.

Something like drawLine(fromX, fromY, toX, toY) and then add a loop while increasing Y to show it in motion.

I hope I am clear. Please point me to the right direction.

Thank you

Snake
  • 12,886
  • 20
  • 99
  • 231
  • For that I would use Canvas and Paint. To use that, [refer to this question][1]. [1]: http://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android – AggieDev Dec 06 '14 at 21:45
  • If you see my question, I said that most people tell you to set the activity to the custom view. I dont want to set it to view. Howcan I obtain the canvas of the root layout – Snake Dec 07 '14 at 05:24
  • What I did was created a class extending View, for example PaintView extends View, then add that to the xml of the layout of the activity. – AggieDev Dec 07 '14 at 19:31
  • But if I add the view to the xml, then that will only span the area the view is placed in. And I want to start the line from different area on the xml differently everytime (really depending on where the user click) – Snake Dec 08 '14 at 05:24
  • 1
    Yeah so what you can do is make sure that new View takes up the entire container. From there, you can simply draw on the canvas anywhere you want within the layout that it is placed. – AggieDev Dec 08 '14 at 06:50
  • Ohhhhh awesome like fill parent width and height with transparent background. Good idea. It is stupid that you can't just initialize a canvas and make it take the whole screen and draw wherever you want . anyways put your comment as answer and I will accept it – Snake Dec 08 '14 at 17:07
  • Exactly, I added an answer with a summary of this. – AggieDev Dec 09 '14 at 00:15

2 Answers2

1

create a view and then animate it.

<View
 android:id="+@id/ivHorizontalLine"
 android:layout_width="match_parent"
 android:layout_height="1px"
 android:background="#000000" />

change the height of the view to match how thick you want the line to be. and the background color for the color of the line.

TranslateAnimation horizontalLineAnimation = new TranslateAnimation(0, 0, YstartPoint, YendPoint);
horizontalLineAnimation.setDuration(duration);

ivHorizontalLine.startAnimation(horizontalLineAnimation);

change YstartPoint and YendPoint to match where you want the line to move from and to. and the duration to match how fast you want the line to move.

  • Thank you...I dont want the animation. as I want to draw the line at certain area. Animation temporary move the line and the orginal x, y stay the coordinates of the line – Snake Dec 07 '14 at 05:26
0

The best way to do this is create a View that takes up the entire container that you would like to paint on top of. No background is necessary, as it is only to be used to create a canvas on it. An example would be like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.packagename.PaintView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

And PaintView would be a public class PaintView extends View

AggieDev
  • 4,604
  • 9
  • 24
  • 46
  • Thank you so much for the solution – Snake Dec 09 '14 at 02:37
  • I cant get it to work. I've created the `DrawView.java` that you linked here and added it to xml just like what you did above. Did I miss something? Btw, I'm using `RelativeLayout` – ThisGuy Apr 18 '15 at 05:59