Android 中带有 TouchEvents 的简单移动对象
在本文中,我们将制作一个应用程序,通过该应用程序,用户可以根据他/她的手指在屏幕上的移动来移动屏幕上可见的对象。触摸拖动/移动对象(如图像、按钮)在拼图等游戏中非常流行。在这里,我们将创建一种在用户完成此操作后在设备屏幕上触摸和拖动图像的简单方法,通过使用onTouchListener视图,图像将在设备屏幕上有一个新位置。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Kotlin语言来实现这个项目。
分步实施
第 1 步:创建一个新项目
要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Kotlin作为编程语言。
第 2 步:在布局中设置任何图像对象
导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。
XML
Kotlin
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View.OnTouchListener
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var mainLayout: ViewGroup
private lateinit var image: ImageView
// default position of image
private var xDelta = 0
private var yDelta = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainLayout = findViewById(R.id.main)
image = findViewById(R.id.image)
// returns True if the listener has
// consumed the event, otherwise False.
image.setOnTouchListener(onTouchListener())
}
@SuppressLint("ClickableViewAccessibility")
private fun onTouchListener(): OnTouchListener {
return OnTouchListener { view, event ->
// position information
// about the event by the user
val x = event.rawX.toInt()
val y = event.rawY.toInt()
// detecting user actions on moving
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_DOWN -> {
val lParams = view.layoutParams as RelativeLayout.LayoutParams
xDelta = x - lParams.leftMargin
yDelta = y - lParams.topMargin
}
MotionEvent.ACTION_UP -> Toast.makeText(this,
"new location!", Toast.LENGTH_SHORT)
.show()
MotionEvent.ACTION_MOVE -> {
// based on x and y coordinates (when moving image)
// and image is placed with it.
val layoutParams = view.layoutParams as RelativeLayout.LayoutParams
layoutParams.leftMargin = x - xDelta
layoutParams.topMargin = y - yDelta
layoutParams.rightMargin = 0
layoutParams.bottomMargin = 0
view.layoutParams = layoutParams
}
}
// reflect the changes on screen
mainLayout.invalidate()
true
}
}
}
步骤 3:使用MainActivity.kt 文件
转到MainActivity.kt文件并参考以下代码。下面是MainActivity.kt文件的代码。代码中添加了注释以更详细地理解代码。
科特林
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View.OnTouchListener
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var mainLayout: ViewGroup
private lateinit var image: ImageView
// default position of image
private var xDelta = 0
private var yDelta = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainLayout = findViewById(R.id.main)
image = findViewById(R.id.image)
// returns True if the listener has
// consumed the event, otherwise False.
image.setOnTouchListener(onTouchListener())
}
@SuppressLint("ClickableViewAccessibility")
private fun onTouchListener(): OnTouchListener {
return OnTouchListener { view, event ->
// position information
// about the event by the user
val x = event.rawX.toInt()
val y = event.rawY.toInt()
// detecting user actions on moving
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_DOWN -> {
val lParams = view.layoutParams as RelativeLayout.LayoutParams
xDelta = x - lParams.leftMargin
yDelta = y - lParams.topMargin
}
MotionEvent.ACTION_UP -> Toast.makeText(this,
"new location!", Toast.LENGTH_SHORT)
.show()
MotionEvent.ACTION_MOVE -> {
// based on x and y coordinates (when moving image)
// and image is placed with it.
val layoutParams = view.layoutParams as RelativeLayout.LayoutParams
layoutParams.leftMargin = x - xDelta
layoutParams.topMargin = y - yDelta
layoutParams.rightMargin = 0
layoutParams.bottomMargin = 0
view.layoutParams = layoutParams
}
}
// reflect the changes on screen
mainLayout.invalidate()
true
}
}
}
现在,运行应用程序。
输出:
想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!