Tuesday, October 11, 2011

Android Canvas Draw Line Example

Draw Line Example

Source code Activity [TestActivity.java]

package com.linedraw; //package name

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class TestActivity extends Activity {
DrawView drawView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

drawView = new DrawView(this);
drawView.setBackgroundColor(Color.DKGRAY);
setContentView(drawView);
}
}

Source code Activity [DrawView.java]

package com.linedraw; //package name

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawView extends View {
Paint paint = new Paint();

public DrawView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawLine(0, 0, 20, 20, paint);
canvas.drawLine(20, 0, 0, 20, paint);
}

}
Output:



4 comments:

  1. can you show the xml part?

    ReplyDelete
  2. Nice, drew an x, nice and simple thanks

    ReplyDelete
  3. would put the project for download? or i it its not mind mail it to me : ka.esmaily@yahoo.com

    ReplyDelete
  4. yes but show us how you can draw this line inside a layout, between two other views, genius.

    ReplyDelete