Learn About How to Draw Rectangle Using Canvas in Android

Introduction

This article explains the Canvas in Android. Android Studio is used for the sample.

The Canvas is used for graphics effects. The Canvas class provides us methods by which you can draw on a bitmap.The Canvas object provides the bitmap on which you draw. It also provides methods like "drawARGB()" for drawing a color, drawBitmap() method to draw a Bitmap, drawText() to draw a text and drawRoundRect() to draw a rectangle with round corners.

Step 1

Create a project as in the following:

Clipboard02.jpg

Step 2

Create an XML file and write this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Text" />

 

</LinearLayout>

Step 3

Create a Java file and write this:

package com.SampleCanvas;

 

import java.util.ArrayList;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Rect;

import android.graphics.RectF;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.View.OnTouchListener;

import android.view.ViewGroup.LayoutParams;

import android.view.Window;

import android.view.WindowManager;

import android.widget.FrameLayout;

 

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.View;

 

public class SampleCanvasActivity extends View {

    Paint paint = new Paint();

 

    public SampleCanvasActivity(Context context) {

        super(context);

    }

 

    @Override

    public void onDraw(Canvas canvas) {

        paint.setColor(Color.BLACK);

        paint.setStrokeWidth(3);

        canvas.drawRect(130, 130, 180, 180, paint);

        paint.setStrokeWidth(0);

        paint.setColor(Color.CYAN);

        canvas.drawRect(133, 160, 177, 177, paint );

        paint.setColor(Color.YELLOW);

        canvas.drawRect(133, 133, 177, 160, paint );

 

    }

 }

Step 4

Create a Java file and write this:

package com.SampleCanvas;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

 

public class Second extends Activity {

    SampleCanvasActivity drawView;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

 

        drawView = new SampleCanvasActivity(this);

        drawView.setBackgroundColor(Color.WHITE);

        setContentView(drawView);

 

    }

}

Step 5

Android Manifext.xml file:
 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.SampleCanvas"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk android:minSdkVersion="6" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

            android:name=".Second"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

Step 6

Clipboard03.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all