Herry
Herry

Reputation: 7087

How to draw a line in android?

I found some code for line drawing but it doesn't work. Can any one help me to draw a line in android?

here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layoutmain">
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
/>
</LinearLayout>

here is my code for class:

 public class DrawPoints extends Activity implements OnTouchListener {
     static int i = 0;
     static float static_x = 0;
     static float static_y = 0;
     static float static_x1 = 0;
     static float static_y1 = 0;

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         View topLayout = this.findViewById(R.id.layoutmain);
         // register for events for the view, previously
         topLayout.setOnTouchListener((OnTouchListener) this);
     }

     @Override
     public boolean onTouch(View v, MotionEvent event) {
         String tag = null;

         if (i == 0) {
             static_x = event.getX();
             static_y = event.getY();
             i = 1;
         } else {
             static_x1 = event.getX();
             static_y1 = event.getY();

         }

         if (i == 1) {
             Paint p = new Paint();
             p.setColor(Color.WHITE);
             p.setStyle(Paint.Style.STROKE);
             Canvas canvas = new Canvas();
             canvas.drawColor(Color.BLUE);

             canvas.drawLine(static_x, static_y, static_x1, static_y1, p);
             i = 0;
         }
         return false;
     }

 }

Upvotes: 0

Views: 15466

Answers (3)

penchoco
penchoco

Reputation: 301

You can simply write this in your xml layout:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="#cccccc"
    android:paddingTop="20dp" />

This will create a horizontal line.

Upvotes: 14

NguyenDat
NguyenDat

Reputation: 4159

This is my solution with little trick, that improve Draw performance faster than 163 times when compare with penchoco solution:

<View 
    android:id="@+id/line"
    android:layout_width="fill_parent"
    android:layout_height="1px"
    android:background="@drawable/ic_line"
</View>

The only difference thing with penchoco solution is used 9 patch Click here to download 9 patch image drawable 1x1 pixel instead of color.

android:background="@drawable/ic_line"

Question: Could anyone explain why using 9 patch image performance was improve more than 163 times, which implement behind scenario that make a magic result ?

Thanks

Upvotes: 2

Rohit Mandiwal
Rohit Mandiwal

Reputation: 10462

You can refer to this code provided in Android Samples.

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html

Upvotes: 0

Related Questions