Android Studio (Java) SimplePaint

Android Studio (Java)

The SimplePaint app (full code given below) allows users to draw pictures by pressing the screen in the app.

The existing SimplePaint app allows users to draw using “circles” using the “SimpleDrawingView.java (using circles)” code given below. Add a button at the top of the app to allow users to toggle between drawing:

(a) using circle

(b) using path (path code is given below)

activity_main.xml


   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
    
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"
       />

MainActivity.java

package com.example.simplepaint;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }
}

SimpleDrawingView.java (using circles)

package com.example.simplepaint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;

public class SimpleDrawingView extends View {
   // setup initial color
   private final int paintColor = Color.BLACK;
   // defines paint and canvas
   private Paint drawPaint;
   // Store circles to draw each time the user touches down
   private List circlePoints;

   public SimpleDrawingView(Context context, AttributeSet attrs) {
       super(context, attrs);
       setupPaint(); // same as before
       circlePoints = new ArrayList();
   }
   // Draw each circle onto the view
   @Override
   protected void onDraw(Canvas canvas) {
       for (Point p : circlePoints) {
           canvas.drawCircle(p.x, p.y, 5, drawPaint);
       }
   }

   // Append new circle each time user presses on screen
   @Override
   public boolean onTouchEvent(MotionEvent event) {
       float touchX = event.getX();
       float touchY = event.getY();
       circlePoints.add(new Point(Math.round(touchX), Math.round(touchY)));
       // indicate view should be redrawn
       postInvalidate();
       return true;
   }

   private void setupPaint() {
       drawPaint = new Paint();
       drawPaint.setColor(paintColor);
       drawPaint.setAntiAlias(true);
       drawPaint.setStrokeWidth(5);
       drawPaint.setStyle(Paint.Style.FILL);
       drawPaint.setStrokeJoin(Paint.Join.ROUND);
       drawPaint.setStrokeCap(Paint.Cap.ROUND);
   }
}

SimpleDrawingView.java (using path)

package com.example.simplepaint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class SimpleDrawingView extends View {
   // setup initial color
   private final int paintColor = Color.BLACK;
   // defines paint and canvas
   private Paint drawPaint;
   // stores next circle
   private Path path = new Path();

   public SimpleDrawingView(Context context, AttributeSet attrs) {
       super(context, attrs);
       setFocusable(true);
       setFocusableInTouchMode(true);
       setupPaint();
   }

   private void setupPaint() {
       // Setup paint with color and stroke styles
       drawPaint = new Paint();
       drawPaint.setColor(paintColor);
       drawPaint.setAntiAlias(true);
       drawPaint.setStrokeWidth(5);
       drawPaint.setStyle(Paint.Style.STROKE);
       drawPaint.setStrokeJoin(Paint.Join.ROUND);
       drawPaint.setStrokeCap(Paint.Cap.ROUND);
   }
   @Override
   protected void onDraw(Canvas canvas) {
       canvas.drawPath(path, drawPaint);
   }
   @Override
   public boolean onTouchEvent(MotionEvent event) {
       float pointX = event.getX();
       float pointY = event.getY();
       // Checks for the event that occurs
       switch (event.getAction()) {
           case MotionEvent.ACTION_DOWN:
               path.moveTo(pointX, pointY);
               return true;
           case MotionEvent.ACTION_MOVE:
               path.lineTo(pointX, pointY);
               break;
           default:
               return false;

       }

       // Force a view to draw again
       postInvalidate();
       return true;
   }
}