Android:触摸屏幕后在Activity上显示ImageView,然后用手指移动
Android: Display ImageView on the Activity after touching the screen and then move with finger
我有一个应用程序。现在,当我点击屏幕上的任意位置时,该应用会执行此操作,它会在触摸点显示一个 ImageView。这在 MotionEvent.ACTION_DOWN
中正在发生。
我希望该应用程序执行以下操作:只要我用手指触摸屏幕并移动它,ImageView 就会跟随我的手指线。我如何在 MotionEvent.ACTION_MOVE
中实现它?
这是 activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
这里是 MainActivity.kt :
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility", "UseCompatLoadingForDrawables")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rr = findViewById<View>(R.id.relative_layout) as RelativeLayout
rr.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
val x = event.x.toInt()
val y = event.y.toInt()
val lp = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
val iv = ImageView(applicationContext)
lp.setMargins(x, y, 0, 0)
iv.layoutParams = lp
iv.setImageDrawable(
resources.getDrawable(
R.drawable.ic_baseline_blur_circular_24
)
)
(v as ViewGroup).addView(iv)
}
if (event.action == MotionEvent.ACTION_MOVE) {
// what should i do here to move the ImageView follow my finger?
}
false
}
}
}
当我在任何地方触摸屏幕时:
我解决了这个问题:
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val relativeLayout = findViewById<RelativeLayout>(R.id.relative_layout)
val yourImage = findViewById<ImageView>(R.id.image_view)
relativeLayout.setOnTouchListener(View.OnTouchListener { view, event ->
val newX: Float
val newY: Float
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
val x = event.x
val y = event.y
yourImage.setX(x - yourImage.getWidth() / 2)
yourImage.setY(y - yourImage.getHeight() / 2)
yourImage.isVisible = true
}
MotionEvent.ACTION_MOVE -> {
val x = event.x
val y = event.y
yourImage.setX(x - yourImage.getWidth() / 2)
yourImage.setY(y - yourImage.getHeight() / 2)
}
ACTION_UP -> {
yourImage.isVisible = false
}
else -> { // Note the block
return@OnTouchListener false
}
}
true
})
}
}
我有一个应用程序。现在,当我点击屏幕上的任意位置时,该应用会执行此操作,它会在触摸点显示一个 ImageView。这在 MotionEvent.ACTION_DOWN
中正在发生。
我希望该应用程序执行以下操作:只要我用手指触摸屏幕并移动它,ImageView 就会跟随我的手指线。我如何在 MotionEvent.ACTION_MOVE
中实现它?
这是 activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
这里是 MainActivity.kt :
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility", "UseCompatLoadingForDrawables")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rr = findViewById<View>(R.id.relative_layout) as RelativeLayout
rr.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
val x = event.x.toInt()
val y = event.y.toInt()
val lp = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
val iv = ImageView(applicationContext)
lp.setMargins(x, y, 0, 0)
iv.layoutParams = lp
iv.setImageDrawable(
resources.getDrawable(
R.drawable.ic_baseline_blur_circular_24
)
)
(v as ViewGroup).addView(iv)
}
if (event.action == MotionEvent.ACTION_MOVE) {
// what should i do here to move the ImageView follow my finger?
}
false
}
}
}
当我在任何地方触摸屏幕时:
我解决了这个问题:
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val relativeLayout = findViewById<RelativeLayout>(R.id.relative_layout)
val yourImage = findViewById<ImageView>(R.id.image_view)
relativeLayout.setOnTouchListener(View.OnTouchListener { view, event ->
val newX: Float
val newY: Float
when (event?.action) {
MotionEvent.ACTION_DOWN -> {
val x = event.x
val y = event.y
yourImage.setX(x - yourImage.getWidth() / 2)
yourImage.setY(y - yourImage.getHeight() / 2)
yourImage.isVisible = true
}
MotionEvent.ACTION_MOVE -> {
val x = event.x
val y = event.y
yourImage.setX(x - yourImage.getWidth() / 2)
yourImage.setY(y - yourImage.getHeight() / 2)
}
ACTION_UP -> {
yourImage.isVisible = false
}
else -> { // Note the block
return@OnTouchListener false
}
}
true
})
}
}