In this post we learn that how to draw a line in android using canvas.
For draw a line in your layout follow this code.
Create one class DrawView which extends 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, 50, 350, 50, paint);
}
}
Now from your main Activity create Object of this class and in setContentView method add that view.
Code for Activity is like this.
public class MainActivity extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
See the output of this example.

No comments:
Post a Comment