How to make your own custom paint (free hand drawing tool) Android App? – complete source code

In this video it shows how one can make his/ her paint or drawing android app. In this it creates a free hand drawing/ sketching App.

It uses onTouchEvent to get the X and Y points of user’s touch movement on the screen. Then it draws those lines on the imageView using the draw line method of the canvas.

The App shown in this video is also hosted on Play Store. Please refer at: https://play.google.com/store/apps/details?id=com.programmerworld.drawingapp

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com

Complete source code:

package com.programmerworld.paint_drawingapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

private ImageView imageView;

private float floatStartX = -1, floatStartY = -1,
floatEndX = -1, floatEndY = -1;

private Bitmap bitmap;
private Canvas canvas;
private Paint paint = new Paint();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ActivityCompat.requestPermissions(this
,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);

imageView = findViewById(R.id.imageView);
}

private void drawPaintSketchImage(){

if (bitmap == null){
bitmap = Bitmap.createBitmap(imageView.getWidth(),
imageView.getHeight(),
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
}
canvas.drawLine(floatStartX,
floatStartY-220,
floatEndX,
floatEndY-220,
paint);
imageView.setImageBitmap(bitmap);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN){
floatStartX = event.getX();
floatStartY = event.getY();
}

if (event.getAction() == MotionEvent.ACTION_MOVE){
floatEndX = event.getX();
floatEndY = event.getY();
drawPaintSketchImage();
floatStartX = event.getX();
floatStartY = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP){
floatEndX = event.getX();
floatEndY = event.getY();
drawPaintSketchImage();
}
return super.onTouchEvent(event);
}

public void buttonSaveImage(View view){
File fileSaveImage = new File(this.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
Calendar.getInstance().getTime().toString() + ".jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileSaveImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
Toast.makeText(this,
"File Saved Successfully",
Toast.LENGTH_LONG).show();

} catch (Exception e) {
e.printStackTrace();
}
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.paint_drawingapp">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PaintDrawingApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

<?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="0dp"
android:layout_height="615dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="141dp"
android:layout_marginTop="2dp"
android:onClick="buttonSaveImage"
android:text="Save"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

<?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="0dp"
android:layout_height="615dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="141dp"
android:layout_marginTop="2dp"
android:onClick="buttonSaveImage"
android:text="Save"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply

%d bloggers like this:
Available for Amazon Prime