Android拖放startDragAndDrop拖拽onDrawShadow动态添加View,Kotlin(3)
import android.content.ClipData
import android.graphics.Canvas
import android.graphics.Point
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.FrameLayout
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompatclass 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)val builder = MyDragShadowBuilder(imageView)imageView.setOnLongClickListener(object : OnLongClickListener {//长按事件触发拖拽override fun onLongClick(v: View?): Boolean {Log.d(TAG, "onLongClick")val data = ClipData.newPlainText("name", "phil")imageView.startDragAndDrop(data,builder,null,0 or View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_OPAQUE)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 mView: View) :View.DragShadowBuilder() {override fun onProvideShadowMetrics(outShadowSize: Point?, outShadowTouchPoint: Point?) {val width: Int = mView.width * 2val height: Int = mView.height * 2//拖动图像的宽和高outShadowSize?.set(width, height)//手指在拖动图像的位置 中点outShadowTouchPoint?.set(width / 2, height / 2)}override fun onDrawShadow(canvas: Canvas) {val width: Int = mView.width * 2val height: Int = mView.height * 2val image = ImageView(mView.context)image.setImageResource(android.R.drawable.stat_notify_error)image.layoutParams = FrameLayout.LayoutParams(width, height)val frameLayout = FrameLayout(mView.context)frameLayout.setBackgroundColor(ContextCompat.getColor(mView.context,android.R.color.holo_green_light))frameLayout.addView(image)frameLayout.measure(width, height)frameLayout.layout(0, 0, width, height)frameLayout.draw(canvas)}}
}
图像随着手指拖放滑动:
Android View拖拽/拖放DragAndDrop自定义View.DragShadowBuilder,Kotlin(2)-CSDN博客文章浏览阅读54次。Android DynamicGrid:拖曳交换位置Android DynamicGrid是一个第三方开源项目,DynamicGrid在github上的项目主页是:https://github.com/askerov/DynamicGrid它实现在一个网格布局内,拖曳任意子view实现动态的交换位置,这很类似手机的桌面,手机桌面的图标,均可自由拖曳实现摆放位置的交换,如动图所示:_android 拖拽交换位置。Android View拖拽startDragAndDrop,Kotlin-CSDN博客。https://blog.csdn.net/zhangphil/article/details/134008458
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