0

I was trying to make a paint app through which users can paint and draw for fun. I wanted to erase a drawing partially by using the undo button in my app. The floating action button I have used erases the whole drawing, but I want only that last part of the drawing to get erased. Kindly help me how can I do that ?

XML FILE

<?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"
android:background="@color/black"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:background="@drawable/toolbar_background"
    android:gravity="center"
    app:layout_constraintRight_toRightOf="parent">
    
    <ImageButton
        android:id="@+id/redColor"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/red_background"/>

    <ImageButton
        android:id="@+id/blueColor"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/blue_background"/>

    <ImageButton
        android:id="@+id/blackColor"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/black_background"/>

    <ImageButton
        android:id="@+id/whiteColor"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="10dp"
        android:background="@drawable/white_background"/>

</LinearLayout>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">

    <include
        android:id="@+id/include"
        layout="@layout/paint_view" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/resetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="RESET"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.954"
        app:layout_constraintStart_toStartOf="parent"
        android:src="@drawable/ic_baseline_delete_24"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.example.dapple

import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.dapple.PaintView.Companion.colorList
import com.example.dapple.PaintView.Companion.currentBrush
import com.example.dapple.PaintView.Companion.pathList
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

companion object{
    var path = Path()
    var paintBrush = Paint()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    supportActionBar?.hide()

    redColor.setOnClickListener{
        paintBrush.color = Color.RED
        currentColor(paintBrush.color)
    }

    blueColor.setOnClickListener{
        paintBrush.color = Color.BLUE
        currentColor(paintBrush.color)
    }

    blackColor.setOnClickListener{
        paintBrush.color = Color.BLACK
        currentColor(paintBrush.color)
    }

    whiteColor.setOnClickListener{
        paintBrush.color = Color.WHITE
        currentColor(paintBrush.color)
    }

    resetButton.setOnClickListener{
        pathList.clear()
        colorList.clear()
        path.reset()
    }
}

private  fun currentColor(color: Int){
    currentBrush = color
    path = Path()
}
}

PaintView.kt

package com.example.dapple

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
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintSet
import com.example.dapple.MainActivity.Companion.paintBrush
import com.example.dapple.MainActivity.Companion.path

class PaintView : View {

var params:ViewGroup.LayoutParams? = null

companion object{
    var pathList = ArrayList<Path>()
    var colorList = ArrayList<Int>()
    var currentBrush = Color.WHITE
}

constructor(context: Context) : this(context, null){
    init()
}
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
{
    init()
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    init()
}

private fun init(){
    paintBrush.isAntiAlias = true
    paintBrush.color = currentBrush
    paintBrush.style = Paint.Style.STROKE
    paintBrush.strokeJoin = Paint.Join.ROUND
    paintBrush.strokeWidth = 8f

    params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}

override fun onTouchEvent(event: MotionEvent): Boolean {
    var x = event.x
    var y = event.y

    when(event.action){
        MotionEvent.ACTION_DOWN->{
            path.moveTo(x,y)
            return true
        }
        MotionEvent.ACTION_MOVE->{
            path.lineTo(x,y)
            pathList.add(path)
            colorList.add(currentBrush)
        }
        else -> return false
    }
    postInvalidate()
    return false
}

override fun onDraw(canvas: Canvas) {
    for(i in pathList.indices){
        paintBrush.setColor(colorList[i])
        canvas.drawPath(pathList[i],paintBrush)
        invalidate()
    }
}
}

0 Answers0