Android Canvas draws finger MotionEvent.ACTION_MOVE event rectangular box, Kotlin

Android Canvas draws finger MotionEvent.ACTION_MOVE event rectangular box, Kotlin

 

 

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Bundle
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatImageView


class MainActivity : AppCompatActivity() {
    private var image: MyImage? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        image = findViewById(R.id.image)
    }
}

class MyImage : AppCompatImageView {
    private var curX: Float? = 0f
    private var curY: Float? = 0f

    private var mIsDraw = false

    constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) {

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.actionMasked) {
            MotionEvent.ACTION_DOWN -> {
                mIsDraw = true
            }

            MotionEvent.ACTION_MOVE -> {
                curX = event?.x
                curY = event?.y

                invalidate()
            }

            MotionEvent.ACTION_UP -> {
                mIsDraw = false
            }
        }

        return true
    }

    override fun draw(canvas: Canvas) {
        super.draw(canvas)

        if (mIsDraw) {
            myDraw(canvas)
        }
    }

    private fun myDraw(canvas: Canvas) {
        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.color = Color.RED
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 40f

        val w = 400
        val h = 300
        val rect = Rect((curX!! - w / 2).toInt(), (curY!! - h / 2).toInt(), (curX!! + w / 2).toInt(), (curY!! + h / 2).toInt())

        canvas.drawRect(rect, paint) //在这块区域边框绘制线。
    }
}

 

The finger moves on the screen, and the red box follows the position of the finger. The position where the finger touches the screen is the center of the red box:

efd94f64c28c417fbb69bdc92490af1e.png

 

 

 

Android Canvas draws drawBitmap based on source Rect and destination Rect. The Kotlin-CSDN blog article has been viewed and read 1.3k times, liked 19 times, and collected 15 times. The article has been viewed and read 9.6k times. The article has been viewed and read 1.8k times. /*Java code converts Drawable to Bitmap */ Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth();Android Material Design: LinearLayoutCompat adds dividing line divider_linearlayout dividing line - CSDN Blog. https://blog.csdn.net/zhangphil/article/details/134818221 Android gesture scaling pictures and pictures pasted on the finger to move with the gesture_an example of android picture scaling following the finger - CSDN blog article has been viewed 3.5k times. A tool class for Android gestures to zoom pictures; at the same time, this class also implements another function: when the finger moves on the touch screen, the picture "sticks" to the finger and moves as a whole as the finger moves. The specific usage method can be as follows: first create a new instance of this type, and then setOnTouchListener(new ImageViewOnMultiTouchListener()) in the ImageView method; for example: ImageViewOnMultiTouchListen_android Example of image scaling following the finger https://blog.csdn.net/zhangphil/article /details/43965701

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/135160784