我正在为"自定义视图' ImageView'获取android-studio警告。已调用setOnTouchListener,但不会覆盖performClick",

时间:2018-03-17 08:58:51

标签: android kotlin touch

这让我很恼火,因为每次上课时我都必须在代码中看到黄线。

我还在onTouch上添加了@SuppressLint("ClickableViewAccessibility")

我的MultiTouchListener类:

open class MultiTouchListener : OnTouchListener {
    var maximumScale: Float = 0.toFloat()
    var minimumScale: Float = 0.toFloat()
    var rotateEnabled: Boolean = false
    var scaleEnabled: Boolean = false
    var translateEnabled: Boolean = false
    private var onTouch: OnTouch? = null
    private var rotateListener: OnRotateListener? = null
    private var activePointerId: Int = 0
    private var prevX: Float = 0.toFloat()
    private var prevY: Float = 0.toFloat()
    private var rect: Rect? = null
    private var scaleGestureDetector: ScaleGestureDetector? = null
    private var _root: StickerView? = null
    private var imgViewFrame: ImageView? = null

    constructor(isRotateEnabled: Boolean, isTranslateEnabled: Boolean, isScaleEnabled: Boolean, imgViewFrame: ImageView, _root: StickerView) {
        this.minimumScale = 0.0f
        this.maximumScale = 10.0f
        this.activePointerId = INVALID_POINTER_ID
        this.rotateEnabled = isRotateEnabled
        this.scaleEnabled = isScaleEnabled
        this.translateEnabled = isTranslateEnabled
        this.imgViewFrame = imgViewFrame
        this.scaleGestureDetector = ScaleGestureDetector(ScaleGestureListener())
        this._root = _root
    }

    constructor(isRotateEnabled: Boolean, isTranslateEnabled: Boolean, isScaleEnabled: Boolean, rotateListener: OnRotateListener) {
        this.minimumScale = 0.5f
        this.maximumScale = 10.0f
        this.activePointerId = INVALID_POINTER_ID
        this.rotateEnabled = isRotateEnabled
        this.scaleEnabled = isScaleEnabled
        this.translateEnabled = isTranslateEnabled
        this.scaleGestureDetector = ScaleGestureDetector(ScaleGestureListener())
        this.rotateListener = rotateListener
    }

    constructor() {
        this.rotateEnabled = true
        this.translateEnabled = true
        this.scaleEnabled = true
        this.minimumScale = 0.5f
        this.maximumScale = 10.0f
        this.activePointerId = INVALID_POINTER_ID
        this.scaleGestureDetector = ScaleGestureDetector(ScaleGestureListener())
    }

    constructor(onTouch: OnTouch) {
        this.rotateEnabled = true
        this.translateEnabled = true
        this.scaleEnabled = true
        this.minimumScale = 0.5f
        this.maximumScale = 10.0f
        this.activePointerId = INVALID_POINTER_ID
        this.scaleGestureDetector = ScaleGestureDetector(ScaleGestureListener())
        this.onTouch = onTouch
    }

    private fun move(view: View, info: TransformInfo) {
        computeRenderOffset(view, info.pivotX, info.pivotY)
        adjustTranslation(view, info.deltaX, info.deltaY)
        val scale = Math.max(info.minimumScale, Math.min(info.maximumScale, view.scaleX * info.deltaScale))
        view.scaleX = scale
        view.scaleY = scale
        view.rotation = adjustAngle(view.rotation + info.deltaAngle)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(view: View, event: MotionEvent): Boolean {
        var newPointerIndex = 0
        this.scaleGestureDetector!!.onTouchEvent(view, event)
        if (this.translateEnabled) {
            val action = event.action
            val pointerIndex: Int
            when (event.actionMasked and action) {
                MotionEvent.ACTION_DOWN -> {
                    when {
                        this.onTouch != null -> this.onTouch!!.onTouchDown()
                    }
                    this.prevX = event.x
                    this.prevY = event.y
                    this.rect = Rect(view.left, view.top, view.right, view.bottom)
                    this.activePointerId = event.getPointerId(0)
                    imgViewFrame?.alpha = .5f
                    removeGalleryView()
                }
                MotionEvent.ACTION_UP -> {
                    this.activePointerId = INVALID_POINTER_ID
                    imgViewFrame?.alpha = 1f
                }
                MotionEvent.ACTION_MOVE -> {
                    pointerIndex = event.findPointerIndex(this.activePointerId)
                    when {
                        pointerIndex != INVALID_POINTER_ID -> {
                            val currX = event.getX(pointerIndex)
                            val currY = event.getY(pointerIndex)
                            when {
                                !this.scaleGestureDetector!!.isInProgress -> adjustTranslation(view, currX - this.prevX, currY - this.prevY)
                            }
                        }
                    }
                }
                MotionEvent.ACTION_CANCEL -> this.activePointerId = INVALID_POINTER_ID
                MotionEvent.ACTION_POINTER_UP -> {
                    pointerIndex = 65280 and action shr 8
                    when {
                        event.getPointerId(pointerIndex) == this.activePointerId -> {
                            when (pointerIndex) {
                                0 -> newPointerIndex = 1
                            }
                            this.prevX = event.getX(newPointerIndex)
                            this.prevY = event.getY(newPointerIndex)
                            this.activePointerId = event.getPointerId(newPointerIndex)
                        }
                    }
                }
            }
        }
        return true
    }

    interface OnRotateListener {
        fun getRotation(f: Float): Float
    }

    interface OnTouch {
        fun onTouchDown()
    }

    private inner class ScaleGestureListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
        private var mPivotX: Float = 0.toFloat()
        private var mPivotY: Float = 0.toFloat()
        private val mPrevSpanVector: Vector2D = Vector2D()

        override fun onScaleBegin(view: View, scaleGestureDetector: ScaleGestureDetector): Boolean {
            this.mPivotX = scaleGestureDetector.focusX
            this.mPivotY = scaleGestureDetector.focusY
            this.mPrevSpanVector.set(scaleGestureDetector.currentSpanVector)
            return true
        }

        override fun onScale(view: View, scaleGestureDetector: ScaleGestureDetector): Boolean {
            var angle: Float = when {
                this@MultiTouchListener.rotateEnabled -> Vector2D.getAngle(this.mPrevSpanVector, scaleGestureDetector.currentSpanVector)
                else -> 0.0f
            }
            var f = 0.0f
            val info = TransformInfo()
            info.deltaScale = if (this@MultiTouchListener.scaleEnabled) scaleGestureDetector.scaleFactor else 1.0f
            info.deltaAngle = angle
            angle = when {
                this@MultiTouchListener.translateEnabled -> scaleGestureDetector.focusX - this.mPivotX
                else -> 0.0f
            }
            info.deltaX = angle
            when {
                this@MultiTouchListener.translateEnabled -> f = scaleGestureDetector.focusY - this.mPivotY
            }
            info.deltaY = f
            info.pivotX = this.mPivotX
            info.pivotY = this.mPivotY
            info.minimumScale = this@MultiTouchListener.minimumScale
            info.maximumScale = this@MultiTouchListener.maximumScale
            this@MultiTouchListener.move(view, info)
            return false
        }
    }

    private inner class TransformInfo {
        var deltaAngle: Float = 0.toFloat()
        var deltaScale: Float = 0.toFloat()
        var deltaX: Float = 0.toFloat()
        var deltaY: Float = 0.toFloat()
        var maximumScale: Float = 0.toFloat()
        var minimumScale: Float = 0.toFloat()
        var pivotX: Float = 0.toFloat()
        var pivotY: Float = 0.toFloat()
    }

    companion object {
        private const val INVALID_POINTER_ID = -1

        private fun adjustAngle(degrees: Float): Float {
            return when {
                degrees > 180.0f -> degrees - 360.0f
                else -> when {
                    degrees < -180.0f -> degrees + 360.0f
                    else -> degrees
                }
            }
        }

        private fun adjustTranslation(view: View, deltaX: Float, deltaY: Float) {
            val deltaVector = floatArrayOf(deltaX, deltaY)
            view.matrix.mapVectors(deltaVector)
            view.translationX = view.translationX + deltaVector[0]
            view.translationY = view.translationY + deltaVector[1]
        }

        private fun computeRenderOffset(view: View, pivotX: Float, pivotY: Float) {
            when {
                view.pivotX != pivotX || view.pivotY != pivotY -> {
                    val prevPoint = floatArrayOf(0.0f, 0.0f)
                    view.matrix.mapPoints(prevPoint)
                    view.pivotX = pivotX
                    view.pivotY = pivotY
                    val currPoint = floatArrayOf(0.0f, 0.0f)
                    view.matrix.mapPoints(currPoint)
                    val offsetY = currPoint[1] - prevPoint[1]
                    view.translationX = view.translationX - (currPoint[0] - prevPoint[0])
                    view.translationY = view.translationY - offsetY
                }
            }
        }
    }
}

我这样称呼它:

        private var myTouchView1: ImageView? = null

        myTouchView1 = findViewById(R.id.myTouchView1)
        myTouchView1!!.setOnTouchListener(
                object : MultiTouchListener(true, true, true, imgViewBG!!, stickerView!!) {

                })

据我所知,它是关于覆盖performClick,但在哪里覆盖它? 我正在使用ImageView Class。

1 个答案:

答案 0 :(得分:0)

我在使用TextView时遇到了OnTOuchListener这个问题,所以我做的是,我创建了一个这样的自定义TextVIew类,

    public class AppTextViewRegular extends TextView {

    private Context context;

    public AppTextViewRegular(Context context) {
        super(context);
        this.context = context;

    }

    public AppTextViewRegular(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void setSize(float size) {
        setTextSize(size);
    }

    @Override
    public boolean performClick() {
        return true;
    }
}

并在其中添加了performClick()(正如您在我的代码中看到的那样)。在我的XML和Java代码中使用它。警告消失了。