Sunday, August 28, 2016

Drawing on Canvas - Extending View

Hi .... Dear All ....  Today I am uploading a superb example of how Drawing on Canvas by Extending View Class.
Canvas 
@The Canvas class holds the "draw" calls.
The Four Basic Components are 
1.  A Bitmap to hold the pixels, 
2. Canvas to host the draw calls (writing into the bitmap)
3.Drawing primitive (e.g. Rect, Path, text, Bitmap)
4.Paint (to describe the colors and styles for the drawing).

On a View

1. Create your own class by extending the View Class
2. Override the onDraw() method

I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box

1. Output Will be like 

2. The Java code is like    MainActivity.java 

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
    ExampleCanvas canvasView;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        canvasView = new ExampleCanvas(this);
        setContentView(canvasView);
    }
    private class ExampleCanvas extends View
    {
        public ExampleCanvas(Context context)
        {
            super(context);
        }

        @Override        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            // custom drawing code here            Paint paint = new Paint();
            paint.setColor(Color.RED);
            paint.setTextSize(40);
            paint.setAntiAlias(true);
            canvas.drawColor(Color.WHITE);
            canvas.drawText("Welcome on My Blog",50,60,paint);
            canvas.drawCircle(500, 500, 100, paint);
            canvas.drawRect(150, 150, 250, 300, paint);


        }
    }
}




No comments:

Post a Comment