Posted By Our Mentors
Amreen Khan
Android Developer
animesh bansal
iphone App Developer
Devendra chouksey
Android Developer
Dilip Saket
IOS apps developer
Hemraj Jhariya
iOS Developer
Jaihind singh Rajput
Smart Phone App Dev
Mujahid khan
Android developer
Narendra Yadav
android developer
Narinder Gupta
Mobile App Architect
Pradeep singh Thakur
Mobile App Architect
Sanjay Sharma
Andriod Developer
Shakti Rajpal
IT Architect
Soniya Vishwakarma
iPhone App Developer
Sunil Raghuwanshi
iPhone App Developer
Supriya Kapse
Senior developer
View current Topics
How to use canvas in your android apps ? Part-I Android
The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).
Posted By: Amreen Khan Date: 12 Jul 2012 View: 1037
      Rate This !!



In this article I have just used the canvas to draw the two simple lines.There combination looks as right sign.

 

CanvasActivity.java class

 

package com.canvasone;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.os.Bundle;

import android.view.View;

 

public class CanvasActivity extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(new myView(this));

    } 

        

    public class myView extends View {

          //The Paint class holds the style and color information about 

          //how to draw geometries, text and bitmaps. 

 

        Paint paint = new Paint();

 

        public myView(Context context) {

            super(context);

            //Set the paint's color.

            paint.setColor(Color.YELLOW);

        }

         

        //Implement onDraw this to do your drawing.

    

        //The Canvas class holds the "draw" calls. To draw something, 

        //you need 4 basic components: A Bitmap to hold the pixels, 

        //a Canvas to host the draw calls (writing into the bitmap),

        //a drawing primitive (e.g. Rect, Path, text, Bitmap), and 

        //a paint (to describe the colors and styles for the drawing). 

 

        @Override

        public void onDraw(Canvas canvas) {

          //Draw a line segment with the specified start and stop x,y coordinates,

          //using the specified paint. 

                canvas.drawLine(0, 60, 30, 90, paint);

                canvas.drawLine(30, 90, 100, 20, paint);

        }

 

    }

}

 

Output:- 

 canvas1.bmp

NOTE: since a line is always "framed", the Style is ignored in the paint.

Any queries or suggestions are most welcome!

Thank's

Regards

Amreen Khan



 



Posted Queries