android - 如何移动 Canvas 上绘制的 Path 对象?

标签 android android-canvas android-custom-view touch-event

我有一个自定义 View ,它在其中绘制一个灰色矩形和一条黑线:

class CustomViewDemo: View {

    private val mRectangleGray = Path()
    private val mLineBlack = Path()

    private lateinit var mRectanglePaint: Paint
    private lateinit var mBarPaint: Paint

    private var marginLeft: Float = 0F
    private var marginBottom: Float = 0F
    private var marginRight: Float = 0F
    private var marginTop: Float = 0F

    private lateinit var mRectSize: RectF
    private lateinit var mLineSize: RectF

    constructor(context: Context?) : super(context) {
        init(null)
    }

    constructor(context: Context?, list: MutableList<String>, timelineWidth: Int) : super(context) {
        init(null)
    }

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

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

    private fun init(attrs: AttributeSet?) {
        mRectanglePaint = Paint()
        mRectanglePaint.isAntiAlias = true
        mRectanglePaint.color = Color.parseColor("#dedede")

        mBarPaint = Paint()
        mBarPaint.style = Paint.Style.FILL
        mBarPaint.color = Color.BLACK
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        drawRectangle(canvas)
        drawVerticalLine(canvas)
    }

    private fun drawRectangle(canvas: Canvas) {
        marginLeft = 50F
        marginRight = width - 50F
        marginTop = 300F
        marginBottom = 450F

        mRectSize = RectF(marginLeft, marginTop, marginRight, marginBottom)
        mRectangleGray.addRect(mRectSize, Path.Direction.CW)

        canvas.drawPath(mRectangleGray, mRectanglePaint)
    }

    private fun drawVerticalLine(canvas: Canvas) {
        marginLeft = 50F
        marginRight = width - 50F
        marginTop = 300F
        marginBottom = 450F

        mLineSize = RectF(marginRight - 7, marginTop, marginRight, marginBottom)
        mLineBlack.addRect(mLineSize, Path.Direction.CW)

        canvas.drawPath(mLineBlack, mBarPaint)
    }

}

输出:

enter image description here

我想通过触摸灰色矩形的边框来左右移动黑线。

但是,我无法使用 onTouchEvent 做到这一点。 , Drag and move a circle drawn on canvas我尝试根据这个问题的答案来调整它,但我开始绘制不同的黑点,而不是线条。也许我做错了什么,我不知道,但我尝试了很多次。

如何通过触摸在 Canvas 上绘制的 Path 对象在矩形中左右移动?谢谢。

最佳答案

这不会为您提供所需的信息,但足以让您走上正确的道路。

将以下 onTouchEvent 处理程序添加到您的自定义 View :

private var linePositionOffset = 0f  
private var startX = width - 7  
  
override fun onTouchEvent(event: MotionEvent): Boolean {  
    val eventAction = event.action  
  
  val x = event.x.toInt()  
  when (eventAction) {  
        MotionEvent.ACTION_DOWN -> {  
            startX = x  
        }  
        MotionEvent.ACTION_UP -> {}  
        MotionEvent.ACTION_MOVE -> {  
            linePositionOffset = (startX - x).toFloat()  
        }  
  }  
  
    // tell the View to redraw the Canvas  
  invalidate()  
  
    // tell the View that we handled the event  
  return true  
}

并将onDraw()更改为:

override fun onDraw(canvas: Canvas) {  
    drawRectangle(canvas)  
    canvas.withTranslation(-linePositionOffset, 0f) {  
       drawVerticalLine(canvas)  
    }  
}

enter image description here

关于android - 如何移动 Canvas 上绘制的 Path 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71145199/

相关文章:

java - Android 动态壁纸视差滚动效果

Android:是否可以在可绘制选择器中使用字符串/枚举?

Android - SDK 端口如何工作?

android - 为 Firebase RecyclerView 添加触摸监听器

java - openFileOutput 在单例类中无法正常工作 - 想法/解决方法?

android - 当文本未完全在 View 内绘制时,为什么 Canvas.drawText 会变慢?

android-layout - 如何使用清除按钮制作可重复使用的 EditText?

java - 自定义ArrayAdapter不显示结果

android - 如何使 View 在触摸事件时可调整大小

Android 绘制 Canvas 贝塞尔曲线和进度