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:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.    
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Text" />  
  11.    
  12. </LinearLayout> 
Step 3
 
Create a Java file and write this:
  1. package com.SampleCanvas;  
  2.    
  3. import java.util.ArrayList;  
  4.    
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.Canvas;  
  9. import android.graphics.Color;  
  10. import android.graphics.Paint;  
  11. import android.graphics.Path;  
  12. import android.graphics.Rect;  
  13. import android.graphics.RectF;  
  14. import android.os.Bundle;  
  15. import android.view.MotionEvent;  
  16. import android.view.SurfaceHolder;  
  17. import android.view.SurfaceView;  
  18. import android.view.View;  
  19. import android.view.View.OnTouchListener;  
  20. import android.view.ViewGroup.LayoutParams;  
  21. import android.view.Window;  
  22. import android.view.WindowManager;  
  23. import android.widget.FrameLayout;  
  24.    
  25. import android.content.Context;  
  26. import android.graphics.Canvas;  
  27. import android.graphics.Color;  
  28. import android.graphics.Paint;  
  29. import android.view.View;  
  30.    
  31. public class SampleCanvasActivity extends View {  
  32.     Paint paint = new Paint();  
  33.    
  34.     public SampleCanvasActivity(Context context) {  
  35.         super(context);  
  36.     }  
  37.    
  38.     @Override  
  39.     public void onDraw(Canvas canvas) {  
  40.         paint.setColor(Color.BLACK);  
  41.         paint.setStrokeWidth(3);  
  42.         canvas.drawRect(130130180180, paint);  
  43.         paint.setStrokeWidth(0);  
  44.         paint.setColor(Color.CYAN);  
  45.         canvas.drawRect(133160177177, paint );  
  46.         paint.setColor(Color.YELLOW);  
  47.         canvas.drawRect(133133177160, paint );  
  48.    
  49.     }  
  50.  } 
Step 4
 
Create a Java file and write this:
  1. package com.SampleCanvas;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5.    
  6. public class Second extends Activity {  
  7.     SampleCanvasActivity drawView;  
  8.    
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.    
  13.         drawView = new SampleCanvasActivity(this);  
  14.         drawView.setBackgroundColor(Color.WHITE);  
  15.         setContentView(drawView);  
  16.    
  17.     }  
Step 5
 
Android Manifext.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.SampleCanvas"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk android:minSdkVersion="6" />  
  8.    
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".Second"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.    
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.    
  23. </manifest> 
Step 6
 
Clipboard03.jpg


Similar Articles