• Android拖放startDragAndDrop拖拽Glide加载堆叠圆角图,Kotlin(5)


    Android拖放startDragAndDrop拖拽Glide加载堆叠圆角图,Kotlin(5)

     

     

    1. import android.content.ClipData
    2. import android.graphics.Canvas
    3. import android.graphics.Point
    4. import android.os.Bundle
    5. import android.util.Log
    6. import android.view.DragEvent
    7. import android.view.LayoutInflater
    8. import android.view.View
    9. import android.view.View.OnDragListener
    10. import android.view.View.OnLongClickListener
    11. import android.view.ViewGroup
    12. import android.widget.ImageView
    13. import android.widget.TextView
    14. import androidx.appcompat.app.AppCompatActivity
    15. import com.bumptech.glide.load.resource.bitmap.CenterCrop
    16. import com.bumptech.glide.load.resource.bitmap.RoundedCorners
    17. class MainActivity : AppCompatActivity() {
    18. companion object {
    19. const val TAG = "fly"
    20. const val DEGREE = -10 //图片选择的角度。
    21. const val RADIUS = 30 //图片的圆角半径。
    22. }
    23. override fun onCreate(savedInstanceState: Bundle?) {
    24. super.onCreate(savedInstanceState)
    25. setContentView(R.layout.activity_main)
    26. val shadowBuilder = createDragShadowBuilder()
    27. setData(shadowBuilder.getShadowView())
    28. val triggerView = findViewById(R.id.image)
    29. triggerView.setOnLongClickListener(object : OnLongClickListener {
    30. //长按事件触发拖拽.
    31. override fun onLongClick(v: View?): Boolean {
    32. val data = ClipData.newPlainText("name", "phil") //测试数据。
    33. triggerView.startDragAndDrop(
    34. data,
    35. shadowBuilder,
    36. null,
    37. 0 or View.DRAG_FLAG_GLOBAL or View.DRAG_FLAG_OPAQUE
    38. )
    39. return true
    40. }
    41. })
    42. triggerView.setOnDragListener(object : OnDragListener {
    43. override fun onDrag(v: View?, event: DragEvent?): Boolean {
    44. when (event?.action) {
    45. DragEvent.ACTION_DRAG_STARTED -> {
    46. //拖放开始
    47. Log.d(TAG, "ACTION_DRAG_STARTED")
    48. //updateData(shadowView)
    49. }
    50. DragEvent.ACTION_DRAG_ENTERED -> {
    51. //进入imageView
    52. Log.d(TAG, "ACTION_DRAG_ENTERED")
    53. }
    54. DragEvent.ACTION_DRAG_ENDED -> {
    55. //拖放结束
    56. Log.d(TAG, "ACTION_DRAG_ENDED")
    57. }
    58. DragEvent.ACTION_DRAG_EXITED -> {
    59. //离开imageView
    60. Log.d(TAG, "ACTION_DRAG_EXITED")
    61. }
    62. }
    63. return true
    64. }
    65. })
    66. }
    67. private fun createDragShadowBuilder(): MyDragShadowBuilder {
    68. val shadowView = LayoutInflater.from(this).inflate(R.layout.dnd, null)
    69. return MyDragShadowBuilder(shadowView)
    70. }
    71. private fun setData(viewGroup: View) {
    72. //从1和4两个数字中随机选一个。
    73. var cnt = intArrayOf(1, 4).random()
    74. Log.d(TAG, "cnt=$cnt")
    75. val group: androidx.constraintlayout.widget.Group = viewGroup.findViewById(R.id.group)
    76. val number = viewGroup.findViewById(R.id.number)
    77. if (cnt == 1) {
    78. //只显示一张。
    79. number.text = "1"
    80. group.visibility = ViewGroup.INVISIBLE
    81. } else if (cnt == 4) {
    82. //显示重叠在一起的4张。
    83. number.text = "4"
    84. group.visibility = ViewGroup.VISIBLE
    85. val imageViews = arrayOf(
    86. viewGroup.findViewById(R.id.iv1),
    87. viewGroup.findViewById(R.id.iv2),
    88. viewGroup.findViewById(R.id.iv3)
    89. )
    90. val resIds = arrayOf(
    91. R.mipmap.pic1,
    92. R.mipmap.pic2,
    93. R.mipmap.pic3
    94. )
    95. imageViews.forEachIndexed { index, iv ->
    96. val degree = (imageViews.size - index) * DEGREE
    97. Log.d(TAG, "index=$index degree=$degree")
    98. iv.rotation = degree.toFloat()
    99. GlideApp.with(this)
    100. .load(resIds[index])
    101. .transform(CenterCrop(), RoundedCorners(RADIUS)) //先中心缩放,再切圆角。
    102. .override(
    103. resources.getDimensionPixelSize(R.dimen.image_size_w),
    104. resources.getDimensionPixelSize(R.dimen.image_size_h)
    105. ).placeholder(R.drawable.ic_launcher_foreground)
    106. .error(android.R.drawable.stat_notify_error)
    107. .into(iv)
    108. }
    109. }
    110. val folder = viewGroup.findViewById(R.id.folder)
    111. //封面
    112. GlideApp.with(this)
    113. .load(R.mipmap.pic4)
    114. .transform(CenterCrop(), RoundedCorners(RADIUS)) //先中心缩放,再切圆角。
    115. .override(
    116. resources.getDimensionPixelSize(R.dimen.image_size_w),
    117. resources.getDimensionPixelSize(R.dimen.image_size_h)
    118. ).placeholder(R.drawable.ic_launcher_foreground)
    119. .error(android.R.drawable.stat_notify_error)
    120. .into(folder)
    121. }
    122. class MyDragShadowBuilder(private var mShadow: View) :
    123. View.DragShadowBuilder() {
    124. private val width: Int =
    125. mShadow.context.resources.getDimensionPixelSize(R.dimen.view_size_w)
    126. private val height: Int =
    127. mShadow.context.resources.getDimensionPixelSize(R.dimen.view_size_h)
    128. fun getShadowView(): View {
    129. return mShadow
    130. }
    131. override fun onProvideShadowMetrics(outShadowSize: Point?, outShadowTouchPoint: Point?) {
    132. //拖动图像的宽和高
    133. outShadowSize?.set(width, height)
    134. //手指在拖动图像的位置 中点
    135. outShadowTouchPoint?.set(width / 2, height / 2)
    136. }
    137. override fun onDrawShadow(canvas: Canvas) {
    138. mShadow.measure(width, height)
    139. mShadow.layout(0, 0, width, height)
    140. mShadow.draw(canvas)
    141. Log.d(TAG, "onDrawShadow width=$width height=$height")
    142. }
    143. }
    144. }

     

     

    只有一张图片,只显示封面图:

    fc388ed6edac4355962b9ab81cab9004.png

     

     

     

    多张图片,显示堆叠的图片,并同时设置封面图: 

    aefdc47b6caf4c18b48cb358e7bc7aaa.png

     

     

    dnd.xml:

    1. "1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. android:layout_width="@dimen/view_size_w"
    5. android:layout_height="@dimen/view_size_h"
    6. android:background="@android:color/holo_orange_light">
    7. <ImageView
    8. android:id="@+id/iv1"
    9. android:layout_width="@dimen/image_size_w"
    10. android:layout_height="@dimen/image_size_h"
    11. android:layout_margin="@dimen/my_margin"
    12. app:layout_constraintBottom_toBottomOf="parent"
    13. app:layout_constraintLeft_toLeftOf="parent"
    14. app:layout_constraintRight_toRightOf="parent"
    15. app:layout_constraintTop_toTopOf="parent" />
    16. <ImageView
    17. android:id="@+id/iv2"
    18. android:layout_width="@dimen/image_size_w"
    19. android:layout_height="@dimen/image_size_h"
    20. android:layout_margin="@dimen/my_margin"
    21. app:layout_constraintBottom_toBottomOf="parent"
    22. app:layout_constraintLeft_toLeftOf="parent"
    23. app:layout_constraintRight_toRightOf="parent"
    24. app:layout_constraintTop_toTopOf="parent" />
    25. <ImageView
    26. android:id="@+id/iv3"
    27. android:layout_width="@dimen/image_size_w"
    28. android:layout_height="@dimen/image_size_h"
    29. android:layout_margin="@dimen/my_margin"
    30. app:layout_constraintBottom_toBottomOf="parent"
    31. app:layout_constraintLeft_toLeftOf="parent"
    32. app:layout_constraintRight_toRightOf="parent"
    33. app:layout_constraintTop_toTopOf="parent" />
    34. <androidx.constraintlayout.widget.Group
    35. android:id="@+id/group"
    36. android:layout_width="wrap_content"
    37. android:layout_height="wrap_content"
    38. android:visibility="visible"
    39. app:constraint_referenced_ids="iv1,iv2,iv3" />
    40. <ImageView
    41. android:id="@+id/folder"
    42. android:layout_width="@dimen/image_size_w"
    43. android:layout_height="@dimen/image_size_h"
    44. android:layout_margin="@dimen/my_margin"
    45. app:layout_constraintBottom_toBottomOf="parent"
    46. app:layout_constraintLeft_toLeftOf="parent"
    47. app:layout_constraintRight_toRightOf="parent"
    48. app:layout_constraintTop_toTopOf="parent" />
    49. <TextView
    50. android:id="@+id/number"
    51. android:layout_width="wrap_content"
    52. android:layout_height="wrap_content"
    53. android:background="@android:color/holo_red_light"
    54. android:text="--"
    55. android:textColor="@android:color/white"
    56. android:textSize="30dp"
    57. app:layout_constraintRight_toRightOf="parent"
    58. app:layout_constraintTop_toTopOf="parent" />
    59. androidx.constraintlayout.widget.ConstraintLayout>

     

     

    dimens.xml:

    1. "1.0" encoding="utf-8"?>
    2. <resources>
    3. <dimen name="view_size_w">250dpdimen>
    4. <dimen name="view_size_h">200dpdimen>
    5. <dimen name="image_size_w">150dpdimen>
    6. <dimen name="image_size_h">100dpdimen>
    7. <dimen name="my_margin">50dpdimen>
    8. resources>

     

     

     

    Android拖放startDragAndDrop拖拽onDrawShadow静态添加xml布局View,Kotlin(4)-CSDN博客文章浏览阅读74次。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/134017828

     

  • 相关阅读:
    自学的一些面试题-html
    【RuoYi移动端】uni-app中实现生成二维码功能(代码示例)
    WPF Button去除按钮边框,添加下划线
    基于深度混合核极限学习机的多变量输入时间序列预测
    Python ChatGPT API 新增的函数调用功能演示
    Java 自定义Excel数据排序
    牛客多校4 N Particle Arts
    spinal HDL - 10 - 状态机
    JavaScript基础
    idea 2023 设置启动参数、单元测试启动参数
  • 原文地址:https://blog.csdn.net/zhangphil/article/details/134269432