Snake
Snake

Reputation: 14648

Draw a line on top of existing layout programmatically

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

Upvotes: 0

Views: 2354

Answers (2)

AggieDev
AggieDev

Reputation: 5045

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

Upvotes: 0

Happy Cupz Cupz
Happy Cupz Cupz

Reputation: 181

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.

Upvotes: 1

Related Questions