Android View拖拽/拖放DragAndDrop自定义View.DragShadowBuilder,Kotlin(2)
import android.graphics.Canvas
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.DragEvent
import android.view.View
import android.view.View.OnDragListener
import android.view.View.OnLongClickListener
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
private val TAG = "fly"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = findViewById<ImageView>(R.id.image)
imageView.setOnLongClickListener(object : OnLongClickListener {
//长按事件触发拖拽
override fun onLongClick(v: View?): Boolean {
Log.d(TAG, "onLongClick")
val builder = MyDragShadowBuilder(imageView)
imageView.startDragAndDrop(
null,
builder,
null,
0
)
return true
}
})
imageView.setOnDragListener(object : OnDragListener {
override fun onDrag(v: View?, event: DragEvent?): Boolean {
when (event?.action) {
DragEvent.ACTION_DRAG_STARTED -> Log.d(TAG, "DragEvent.ACTION_DRAG_STARTED")
DragEvent.ACTION_DRAG_ENTERED -> Log.d(TAG, "DragEvent.ACTION_DRAG_ENTERED")
DragEvent.ACTION_DRAG_ENDED -> Log.d(TAG, "DragEvent.ACTION_DRAG_ENDED")
}
return true
}
})
}
class MyDragShadowBuilder(private var imageView: ImageView) : View.DragShadowBuilder() {
//自定义绘制一个拖放时显示的阴影色块
private val shadow = ColorDrawable(
ContextCompat.getColor(
imageView.context,
android.R.color.holo_green_light
)
)
override fun onProvideShadowMetrics(outShadowSize: Point?, outShadowTouchPoint: Point?) {
val width: Int = imageView.width * 2
val height: Int = imageView.height * 2
shadow.setBounds(0, 0, width, height)
//拖动图像的宽和高
outShadowSize?.set(width, height)
//手指在拖动图像的位置 中点
outShadowTouchPoint?.set(width / 2, height / 2)
}
override fun onDrawShadow(canvas: Canvas) {
shadow.draw(canvas)
}
}
}
Android View拖拽startDragAndDrop,Kotlin-CSDN博客一个Android手势缩放图片的工具类;Android DynamicGrid:拖曳交换位置Android DynamicGrid是一个第三方开源项目,DynamicGrid在github上的项目主页是:https://github.com/askerov/DynamicGrid它实现在一个网格布局内,拖曳任意子view实现动态的交换位置,这很类似手机的桌面,手机桌面的图标,均可自由拖曳实现摆放位置的交换,如动图所示:_android 拖拽交换位置。但是还有一些遗漏问题尚未解决:垂直方向的拖曳。https://blog.csdn.net/zhangphil/article/details/133994955