| 9 P a g e
5. Write an application that draws basic graphical primitives on the screen.
Activit_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf ="parent"
app:layout_constraintEnd_toEndOf ="parent"
app:layout_constraintStart_toStartOf ="parent"
app:layout_constraintTop_toTopOf ="parent" />
</androidx.constraintlayout.widget.ConstraintLayout >
MainActivity.java
package com.example.graphic;
import androidx.appcompat.app.AppCompatActivity ;
import android.graphics.Color ;
import android.os.Bundle;
import android.graphics.Bitmap ;
import android.widget.ImageView ;
import android.graphics.Canvas ;
import android.graphics.Paint ;
public class MainActivity extends AppCompatActivity {
Bitmap bg;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_main);
bg=Bitmap.createBitmap( 720, 1280, Bitmap.Config.RGB_565);
img=findViewById(R.id.imageview);
img.setImageBitmap(bg);
Canvas canvas=new Canvas (bg);
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(50);
canvas.drawText("Rectangle",420,150,paint);
canvas.drawRect(400,200,650,700,paint);
canvas.drawText("Circle",120,150,paint);
canvas.drawCircle(200,350,150,paint);;
canvas.drawText("square",120,800,paint);
canvas.drawRect(50,850,6350,1150,paint);
Downloaded by Dilpreet Kaur (
[email protected])
lOMoARcPSD|23838930