0

大家好,希望你没事

我在我的非常基本的绘图 android 应用程序中有一个小问题我有一个图像按钮来更改 PaintColor 但问题是:颜色仅在毫秒内发生变化,然后它回到 DrawingView 类中定义的基本颜色,其他问题是所有的previeus绘图线更改为newColor并回到基本的 这是我的2个课程这是非常基本的项目

DrawingView.kt
package com.wissewassim.kid_drawing_app

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import kotlin.collections.ArrayList

class DrawingView (context : Context, attrs :AttributeSet) : View(context,attrs)  {
    private var mDrawPath : CustomPath? = null
    private var mCanvasBitmap : Bitmap? = null
    private var mDrawPaint : Paint? = null
    private var mCanvasPaint : Paint? = null
    private var mBrishSize : Float = 0.toFloat()
    private var color : Int = Color.CYAN
    private var canvas : Canvas? = null
    private val paths   = ArrayList<CustomPath>()

    init {
        setUpDrawing()
    }
    private fun setUpDrawing(){
        /** we give it  to drawPath to draw in the screen  */
        mDrawPaint = Paint()
        /**we use it to draw on the scren using the Paint that returnned from customPath.class */
        mDrawPath = CustomPath(color,mBrishSize)
        /**define the style and the color and the edges of the line of the draw (attributes) */
        mDrawPaint!!.color = color
        mDrawPaint!!.style = Paint.Style.STROKE
        mDrawPaint!!.strokeCap =  Paint.Cap.ROUND
        mDrawPaint!!.strokeJoin =  Paint.Join.ROUND
        /**we use it to draw the canvas on the view with PaintFlag
         * PaintFlag: how does this canvas render in the screen*/
        mCanvasPaint= Paint(Paint.DITHER_FLAG)
        /**the size of the brush we use it to draw lines */
          mBrishSize = 20.toFloat()
    }
    /** put canvas it the view each time the screen size changed
     *  or an view was added to the hierarchy
     *  */
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mCanvasBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888)
        canvas = Canvas(mCanvasBitmap!!)

    }

    /**هده الدالى تستدعي عند بدأ الرسم علي canvas*/

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        /**
         *           بقوم بالرسم باستخدام دالة ال canvas نمرر لها (  mCanvasPaint witch has the Attributes of the draw  ,الاحداصيات Y;الاحداصيات X,الكنفس التي سنرسم عليها )
         */

        canvas.drawBitmap(mCanvasBitmap!!,0f,0f,mCanvasPaint)
        //set mDrawPaint stroke and color using the old path colors to draw it again
        for (path in  paths){

            //initialize the stroke width using the object we created from customPath.class
            mDrawPaint!!.strokeWidth = path.brishThickness
            mDrawPath!!.color = path.color
            //
            canvas.drawPath(path,mDrawPaint!!)
        }
        //check if the drawPath was created using the inner class CustomPath
        if(!mDrawPath!!.isEmpty){
            //initialize the stroke width using the object we created from customPath.class
            mDrawPaint!!.strokeWidth = mDrawPath!!.brishThickness
            //initialize the draw color  using the object we created from customPath.class
            mDrawPaint!!.color = mDrawPath!!.color
            ///pass the drawpath and draw paint to the canvas to draw the path in the method draw path
            canvas.drawPath(mDrawPath!!,mDrawPaint!!)
        }
    }
    fun setBrushSize(newSize : Float){
        mBrishSize= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,newSize,resources.displayMetrics)
        mDrawPaint!!.strokeWidth = mBrishSize
        mDrawPaint!!.color = color
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {

        val touchX = event?.x
        val touchY = event?.y
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {

                mDrawPath!!.color = color
                mDrawPath!!.brishThickness = mBrishSize
                mDrawPath!!.reset()

                if (touchY != null) {
                    if (touchX != null) {
                        mDrawPath!!.moveTo(touchX, touchY)
                    }
                }

            }
            MotionEvent.ACTION_MOVE -> {
                if (touchY != null) {
                    if (touchX != null) {
                        mDrawPath!!.lineTo(touchX, touchY)
                    }
                }
            }
            MotionEvent.ACTION_UP -> {
                paths.add(mDrawPath!!)
                mDrawPath = CustomPath(color, mBrishSize )

            }

            else -> return false
        }
        invalidate()
        return true



    }

    fun setpathColor(newColor : Int ){
        color = newColor
        mDrawPaint!!.color= color
    }

    /** inner class to create customPath that we use to draw using it */
    internal class CustomPath(var color:Int, var brishThickness : Float ): Path()

}

MainActivity.kt

package com.wissewassim.kid_drawing_app

import android.app.Dialog
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageButton
import com.wissewassim.kid_drawing_app.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), OnClickListener {
    private lateinit var binding: ActivityMainBinding
    private var brushSize = 0.toFloat()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        //set the default brush size
        binding.drawingView.setBrushSize(20.toFloat())
        //change brush size when choose size in choosing brush size Dialog
          binding.ibBrushSizeChange.setOnClickListener{ brushSizeDialog() }
       binding.ibBrushColorRed.setOnClickListener(brushSizeDialog())
        binding.ibBrushColorGreen.setOnClickListener(brushSizeDialog())
        binding.ibBrushColorBlack.setOnClickListener(brushSizeDialog())
        binding.ibBrushColorBlue.setOnClickListener(brushSizeDialog())
        binding.ibBrushColorYellow.setOnClickListener(brushSizeDialog())
}
    private fun brushSizeDialog(){
        val brushDialog = Dialog(this)
        brushDialog.setContentView(R.layout.dialog_brush_size)
        brushDialog.setTitle("set brush size :")

        val smallbrush = brushDialog.findViewById<ImageButton>(R.id.ib_small_brush_size)
        val mudiumbrush = brushDialog.findViewById<ImageButton>(R.id.ib_meduim_brush_size)
        val largebrush = brushDialog.findViewById<ImageButton>(R.id.ib_large_brush_size)
        smallbrush.setOnClickListener{
            brushSize = 20.toFloat()
            binding.drawingView.setBrushSize(brushSize)
            brushDialog.dismiss()

        }
        mudiumbrush.setOnClickListener{
            brushSize = 30.toFloat()
            binding.drawingView.setBrushSize(brushSize)
            brushDialog.dismiss()

        }
        largebrush.setOnClickListener{
            brushSize = 40.toFloat()
            binding.drawingView.setBrushSize(brushSize)
            brushDialog.dismiss()

        }
    brushDialog.show()

    }

    override fun onClick(v: View?) {
       when(v?.id){
           binding.ibBrushColorRed.id->{
               binding.drawingView.setpathColor(Color.RED)
           }
               binding.ibBrushColorBlack.id->{
               binding.drawingView.setpathColor(Color.BLACK)
           }
               binding.ibBrushColorYellow.id->{
               binding.drawingView.setpathColor(Color.YELLOW)
           }
               binding.ibBrushColorGreen.id->{
               binding.drawingView.setpathColor(Color.GREEN)
           }
               binding.ibBrushColorBlue.id->{
               binding.drawingView.setpathColor(Color.BLUE)
           }
       }
    }


}
4

1 回答 1

0

用这个改变你的DrawingView班级:

class DrawingView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private var mDrawPath: CustomPath? = null
    private var mCanvasBitmap: Bitmap? = null
    private var mDrawPaint: Paint? = null
    private var mCanvasPaint: Paint? = null
    private var mBrishSize: Float = 0.toFloat()
    private var color: Int = Color.CYAN
    private var canvas: Canvas? = null
    private val paths = ArrayList<CustomPath>()
    private val pathColorList = ArrayList<Pair<CustomPath, Int>>()


    init {
        setUpDrawing()
    }

    private fun setUpDrawing() {
        /** we give it  to drawPath to draw in the screen  */
        mDrawPaint = Paint()
        /**we use it to draw on the scren using the Paint that returnned from customPath.class */
        mDrawPath = CustomPath(color, mBrishSize)
        /**define the style and the color and the edges of the line of the draw (attributes) */
        mDrawPaint!!.color = color
        mDrawPaint!!.style = Paint.Style.STROKE
        mDrawPaint!!.strokeCap = Paint.Cap.ROUND
        mDrawPaint!!.strokeJoin = Paint.Join.ROUND
        /**we use it to draw the canvas on the view with PaintFlag
         * PaintFlag: how does this canvas render in the screen*/
        mCanvasPaint = Paint(Paint.DITHER_FLAG)
        /**the size of the brush we use it to draw lines */
        mBrishSize = 20.toFloat()
    }

    /** put canvas it the view each time the screen size changed
     *  or an view was added to the hierarchy
     *  */
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mCanvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        canvas = Canvas(mCanvasBitmap!!)

    }

    /**هده الدالى تستدعي عند بدأ الرسم علي canvas*/

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawBitmap(mCanvasBitmap!!, 0f, 0f, mCanvasPaint)
        for ((path, color) in pathColorList) {
            mDrawPaint!!.strokeWidth = path.brishThickness
            mDrawPaint!!.color = color
            canvas.drawPath(path, mDrawPaint!!)
        }
        if (!mDrawPath!!.isEmpty) {
            mDrawPaint!!.strokeWidth = mDrawPath!!.brishThickness
            mDrawPaint!!.color = mDrawPath!!.color
            canvas.drawPath(mDrawPath!!, mDrawPaint!!)
        }
    }

    fun setBrushSize(newSize: Float) {
        mBrishSize = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            newSize,
            resources.displayMetrics
        )
        mDrawPaint!!.strokeWidth = mBrishSize
        mDrawPaint!!.color = color
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {

        val touchX = event?.x
        val touchY = event?.y
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {

                mDrawPath!!.color = color
                mDrawPath!!.brishThickness = mBrishSize
                mDrawPath!!.reset()

                if (touchY != null) {
                    if (touchX != null) {
                        mDrawPath!!.moveTo(touchX, touchY)
                    }
                }

            }
            MotionEvent.ACTION_MOVE -> {
                if (touchY != null) {
                    if (touchX != null) {
                        mDrawPath!!.lineTo(touchX, touchY)
                    }
                }
            }
            MotionEvent.ACTION_UP -> {
                paths.add(mDrawPath!!)
                mDrawPath = CustomPath(color, mBrishSize)

            }

            else -> return false
        }
        invalidate()
        return true


    }

    fun setpathColor(newColor: Int) {
        color = newColor
        mDrawPaint!!.color = color
        val pair = Pair(mDrawPath!!, newColor)
        pathColorList.add(pair)
    }

    /** inner class to create customPath that we use to draw using it */
    internal class CustomPath(var color: Int, var brishThickness: Float) : Path()

}
于 2021-09-04T18:39:53.677 回答