Here I give full source code in Java and XML and Sample output with mobile application in .apk format.
EX.NO 3 Write an application that draws basic graphical primitives on the screen.
Aim:
To develop a Simple Android Application that draws basic Graphical Primitives on the screen.
Procedure:
Open android studio and create new project
Select our project in the project explorer
Go to res folder and select layout Double click the main xml file
Type the code for main.xml or drag and drop various components used in our program
Drag and drop relative layout and change its properties
Drag and drop image view and change its properties according to our programs
Screen layout can be viewed by clicking graphics layout tab
Include necessary files
Override OnCreate() function
Create Image view and initialize its using id of some components used in the xml program
Save the program
Run the program
Output can be viewed in the android emulator
PROGRAM:
XML CODING:
File Name: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ImageView"/>
</RelativeLayout>
JAVA CODING:
File Name: MainActivity.java
package com.example.experiment3;
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
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.setColor(Color.BLACK);
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, paint);
//To draw a Square
canvas.drawText("Square", 120, 800, paint);
canvas.drawRect(50, 850, 350, 1150, paint);
//To draw a Line
canvas.drawText("Line", 480, 800, paint);
canvas.drawLine(520, 850, 520, 1150, paint);
}
}
Output:
Result:
Thus a Simple Android Application that draws basic Graphical Primitives on the screen is developed and executed successfully.
Here I was attach my Mobile Application in .apk format.
Click on blow link: (Download the mobile app run in your mobile)
https://drive.google.com/file/d/1KIp-q2YrMIUR3WLLBVQvIDpxof-Aw009/view?usp=sharing
Any quires leave a comment , I will Try to answer soon.