0% found this document useful (0 votes)
73 views

Experiment No.2 Aim:: To Develop An Application That Draws Basic Graphical Primitives On The Screen

The document describes an experiment to develop an Android application that draws basic graphical primitives on the screen. It discusses using an ImageView to display drawings, and the Canvas and Paint classes to render lines, circles, rectangles and squares. The code provided creates a Bitmap canvas, defines Paint objects for different colors, and uses the Canvas to draw the shapes along with labels on the ImageView.

Uploaded by

Tejas Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Experiment No.2 Aim:: To Develop An Application That Draws Basic Graphical Primitives On The Screen

The document describes an experiment to develop an Android application that draws basic graphical primitives on the screen. It discusses using an ImageView to display drawings, and the Canvas and Paint classes to render lines, circles, rectangles and squares. The code provided creates a Bitmap canvas, defines Paint objects for different colors, and uses the Canvas to draw the shapes along with labels on the ImageView.

Uploaded by

Tejas Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CST411: Mobile Application Development Lab Class: TY [B] Roll :38

Course Coordinator: Mrs. Mrunal Deshpande Name: Latika Nandai

Experiment No.2

Aim: To develop an application that draws basic graphical primitives on the screen.

Problem statement: Develop an android application to draw line, circle, rectangle and square on
screen using ImageView, Canvas class and Paint class.

Theory:

ImageView:

• ImageView class is used to display any kind of image resource in the android application
either it can be android.graphics.Bitmap or android.graphics.drawable.Drawable.
• ImageView is also used to control the size and movement of an image.

• XML Attributes of ImageView

XML Attribute Description

android:id To uniquely identify an image view

android:src/app:srcCompat To add the file path of the inserted image

android:background To provide a background color to the inserted image

android:layout_width To set the width of the image

android:layout_height To set the height of the image

To add padding to the image from left, right, top, or bottom of


android:padding the view

android:scaleType To re-size the image or to move it in order to fix its size

Canvas and Paint class in android:


CST411: Mobile Application Development Lab Class: TY [B] Roll :38
Course Coordinator: Mrs. Mrunal Deshpande Name: Latika Nandai

• The android.graphics.Canvas can be used to draw graphics in android. It provides


methods to draw oval, rectangle, picture, text, line etc.
• The android.graphics.Paint class is used with canvas to draw objects. It holds the
information of color and style.
• A Canvas is a 2D drawing framework that provides us with methods to draw on the
underlying Bitmap.
• A Bitmap acts as the surface over which the canvas is placed. The Paint class is used to
provide colors and styles.

Code:

//Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />

</RelativeLayout>

//MainActivity.java
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
CST411: Mobile Application Development Lab Class: TY [B] Roll :38
Course Coordinator: Mrs. Mrunal Deshpande Name: Latika Nandai

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Creating a Bitmap
Bitmap bg = Bitmap.createBitmap(720, 1280, Bitmap.Config.ARGB_8888);

//Setting the Bitmap as background for the ImageView


ImageView i = (ImageView) findViewById(R.id.imageView);
i.setBackgroundDrawable(new BitmapDrawable(bg));

//Creating the Canvas Object


Canvas canvas = new Canvas(bg);

//Creating the Paint Object and set its color & TextSize
Paint paint = new Paint();
Paint paint1 = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
paint.setColor(Color.RED);
paint1.setColor(Color.BLACK);
paint2.setColor(Color.YELLOW);
paint3.setColor(Color.BLUE);
paint.setTextSize(50);

//To draw a Rectangle


canvas.drawText("Rectangle", 420, 150, paint);
canvas.drawRect(400, 200, 650, 700, paint);

//To draw a Circle


canvas.drawText("Circle", 120, 150, paint);
canvas.drawCircle(200, 350, 150, paint1);

//To draw a Square


canvas.drawText("Square", 120, 800, paint);
canvas.drawRect(50, 850, 350, 1150, paint2);

//To draw a Line


canvas.drawText("Line", 480, 800, paint);
canvas.drawLine(520, 850, 520, 1150, paint3);
}
}

Output:
CST411: Mobile Application Development Lab Class: TY [B] Roll :38
Course Coordinator: Mrs. Mrunal Deshpande Name: Latika Nandai

You might also like