首页 > 解决方案 > 如何调整 DragShadowBuilder 的大小?

问题描述

DragShadowBuilder在一个ImageView.

    val data = ClipData.newPlainText("", "")
    dragShadow = MyDragShadowBuilder(imageViewTemp, imageLayout.width, imageLayout.width)
    imageViewTemp.startDragAndDrop(data, dragShadow, imageViewTemp, View.DRAG_FLAG_OPAQUE)

如您所见,我已经设置了 的宽度和高度DragShadowBuilder,但我想将其调整为 250,当它被拖动时。我怎样才能做到这一点?如果我不能调整它的大小,我可以重新创建DragShadowBuilder一些减少宽度和高度的?

class MyDragShadowBuilder(
    view: View,
    val scaledWidth: Int,
    val scaledHeight: Int
) : View.DragShadowBuilder(view) {
    var tempScaleX = 0.0F
    var tempScaleY = 0.0F

    override fun onProvideShadowMetrics(outShadowSize: Point?, outShadowTouchPoint: Point?) {
        // calculate the scaled view's dimensions
        tempScaleX = scaledWidth.toFloat() / view.width
        tempScaleY = scaledHeight.toFloat() / view.height

        // set the scaled shadow size
        outShadowSize?.set(scaledWidth, scaledHeight)

        // re-center the out touch point using the scaled dimensions
        outShadowTouchPoint?.set(scaledWidth / 2, scaledHeight / 2)
    }

    override fun onDrawShadow(canvas: Canvas?) {
        // scale the canvas so it can fit the scaled view
        canvas?.scale(
            tempScaleX,
            tempScaleY
        )

        view.draw(canvas)
    }
}

标签: androidkotlin

解决方案


推荐阅读