Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使imageView离开屏幕[Kotlin]_Android_Kotlin - Fatal编程技术网

Android 如何使imageView离开屏幕[Kotlin]

Android 如何使imageView离开屏幕[Kotlin],android,kotlin,Android,Kotlin,所以我是Kotlin的新手,我正在尝试制作一个超级简单的应用程序。它所做的就是当我点击右键时,它会向右移动,和左键一样。问题是,当我单击任一按钮(例如右键)时,我可以单击它,直到图像完全脱离屏幕。那么,我如何实现一个代码,一旦它击中屏幕边缘,它就停止移动 我的代码 package com.example.change_position_circle import android.animation.ObjectAnimator import androidx.appcompat.app.App

所以我是Kotlin的新手,我正在尝试制作一个超级简单的应用程序。它所做的就是当我点击右键时,它会向右移动,和左键一样。问题是,当我单击任一按钮(例如右键)时,我可以单击它,直到图像完全脱离屏幕。那么,我如何实现一个代码,一旦它击中屏幕边缘,它就停止移动

我的代码

package com.example.change_position_circle

import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //val picture = findViewById<ImageView>(R.id.SpongeBob)
        val right_button = findViewById<Button>(R.id.right)
        val left_button = findViewById<Button>(R.id.left)


        right_button.setOnClickListener()
        {
            //ObjectAnimator.ofFloat(SpongeBob, "x", 100)

            SpongeBob.animate().setDuration(90).translationXBy(100f)
        }

        left_button.setOnClickListener()
        {
            //ObjectAnimator.ofFloat(SpongeBob, "translationXBy", 100f).apply {
            //duration = 200
            // start()
            SpongeBob.animate().setDuration(90).translationXBy(-100f)
            //}
        }
    }
}
感谢您的帮助

欢迎来到科特林

所以是的,你已经有了你的海绵宝宝动画,现在你需要一些逻辑来控制动画。这里的问题是你并不总是希望完整的动画发生,对吧?如果他离屏幕边缘太近,你希望按钮只能将他移动到看不见的墙,如果他正好靠在墙上,那就意味着根本不移动

动画和绘图系统不会对可以放置视图的位置设置任何限制,因此由您自己处理。您基本上需要在单击按钮时执行此操作:

获取海绵宝宝的位置坐标,这是你现在真正关心的X 计算出你关心的边的位置,视图的坐标描述了左上角的位置,因此如果你在看右边,你需要X坐标+视图的宽度 计算出屏幕或父布局边缘的X坐标,无论您希望海绵宝宝包含在其中 如果海绵宝宝边和屏幕边之间的距离小于正常动画移动,则需要将其更改为剩余距离 您还需要计算出适当的持续时间,如果他移动的距离为正常距离的一半,则动画所需的时间应为正常距离的一半 这需要做很多工作,有几种方法可以做到这一点,但这里有一种方法只是使用屏幕边缘作为边界

    import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.abs
import kotlin.math.min

private const val MOVE_DISTANCE = 100
private const val MOVE_TIME = 90

class MainActivity : AppCompatActivity() {
    private var screenWidth = 0
    private lateinit var spongeBob : View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.spongebob)

        // store this when the Activity is created, if the device is rotated the Activity
        // will be recreated and this method gets run again
        screenWidth = applicationContext.resources.displayMetrics.widthPixels

        //val picture = findViewById<ImageView>(R.id.SpongeBob)
        val right_button = findViewById<Button>(R.id.right)
        val left_button = findViewById<Button>(R.id.left)
        spongeBob = findViewById(R.id.spongeBob)

        right_button.setOnClickListener()
        {
            // two possible values - the distance to the edge, and the normal amount we move
            // we want the smaller of the two (i.e. always move the normal amount, unless
            // the edge is closer than that
            val distance = min(distanceToEdge(left = false), MOVE_DISTANCE)
            moveSpongeBob(distance)
        }

        left_button.setOnClickListener()
        {
            val distance = min(distanceToEdge(left = true), MOVE_DISTANCE)
            // we're moving left so we need to use a negative distance
            moveSpongeBob (-distance)
        }
    }

    private fun distanceToEdge(left: Boolean): Int {
        // Get the Spongebob's top-left position - the call is a method on the View class,
        // I'm assuming SpongeBob is a View, and you need to pass an array in because
        // that's just how it works for whatever reason...
        val location = IntArray(2)
        spongeBob.getLocationOnScreen(location)
        val x = location[0]

        // I'm just using the View.getWidth() call here (Kotlin style) but I don't know
        // what the SpongeBob class is, so you'll need to handle this
        // You could set this once, like when we get the screen width, but width will be 0 until
        // the View is laid out - so you can't do it in #onCreate, #onViewCreated should work
        val spongeBobWidth = spongeBob.width

        // the left edge is just the x position, however far that is from zero
        return if (left) x
        // the right edge is the x position plus the width of the bob
        else screenWidth - (x + spongeBobWidth)
    }

    // Actually move the view, by the given distance (negative values to move left)
    private fun moveSpongeBob(distance: Int) {
        // work out how much this distance relates to our standard move amount, so we can
        // adjust the time by the same proportion - converting to float so we don't get
        // integer division (where it's rounded to a whole number)
        val fraction = distance.toFloat() / MOVE_DISTANCE
        // distance can be negative (i.e. moving left) so we need to use the abs function
        // to make the duration a postitive number
        val duration = abs(MOVE_TIME * fraction).toLong()
        spongeBob.animate().setDuration(duration).translationXBy(distance.toFloat())
    }
}
有更好的东西你可以做,海绵宝宝应该被称为海绵宝宝,并成为一个视图,但这是最基本的。在坐标系上也可能对您有所帮助。

欢迎来到Kotlin

所以是的,你已经有了你的海绵宝宝动画,现在你需要一些逻辑来控制动画。这里的问题是你并不总是希望完整的动画发生,对吧?如果他离屏幕边缘太近,你希望按钮只能将他移动到看不见的墙,如果他正好靠在墙上,那就意味着根本不移动

动画和绘图系统不会对可以放置视图的位置设置任何限制,因此由您自己处理。您基本上需要在单击按钮时执行此操作:

获取海绵宝宝的位置坐标,这是你现在真正关心的X 计算出你关心的边的位置,视图的坐标描述了左上角的位置,因此如果你在看右边,你需要X坐标+视图的宽度 计算出屏幕或父布局边缘的X坐标,无论您希望海绵宝宝包含在其中 如果海绵宝宝边和屏幕边之间的距离小于正常动画移动,则需要将其更改为剩余距离 您还需要计算出适当的持续时间,如果他移动的距离为正常距离的一半,则动画所需的时间应为正常距离的一半 这需要做很多工作,有几种方法可以做到这一点,但这里有一种方法只是使用屏幕边缘作为边界

    import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.abs
import kotlin.math.min

private const val MOVE_DISTANCE = 100
private const val MOVE_TIME = 90

class MainActivity : AppCompatActivity() {
    private var screenWidth = 0
    private lateinit var spongeBob : View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.spongebob)

        // store this when the Activity is created, if the device is rotated the Activity
        // will be recreated and this method gets run again
        screenWidth = applicationContext.resources.displayMetrics.widthPixels

        //val picture = findViewById<ImageView>(R.id.SpongeBob)
        val right_button = findViewById<Button>(R.id.right)
        val left_button = findViewById<Button>(R.id.left)
        spongeBob = findViewById(R.id.spongeBob)

        right_button.setOnClickListener()
        {
            // two possible values - the distance to the edge, and the normal amount we move
            // we want the smaller of the two (i.e. always move the normal amount, unless
            // the edge is closer than that
            val distance = min(distanceToEdge(left = false), MOVE_DISTANCE)
            moveSpongeBob(distance)
        }

        left_button.setOnClickListener()
        {
            val distance = min(distanceToEdge(left = true), MOVE_DISTANCE)
            // we're moving left so we need to use a negative distance
            moveSpongeBob (-distance)
        }
    }

    private fun distanceToEdge(left: Boolean): Int {
        // Get the Spongebob's top-left position - the call is a method on the View class,
        // I'm assuming SpongeBob is a View, and you need to pass an array in because
        // that's just how it works for whatever reason...
        val location = IntArray(2)
        spongeBob.getLocationOnScreen(location)
        val x = location[0]

        // I'm just using the View.getWidth() call here (Kotlin style) but I don't know
        // what the SpongeBob class is, so you'll need to handle this
        // You could set this once, like when we get the screen width, but width will be 0 until
        // the View is laid out - so you can't do it in #onCreate, #onViewCreated should work
        val spongeBobWidth = spongeBob.width

        // the left edge is just the x position, however far that is from zero
        return if (left) x
        // the right edge is the x position plus the width of the bob
        else screenWidth - (x + spongeBobWidth)
    }

    // Actually move the view, by the given distance (negative values to move left)
    private fun moveSpongeBob(distance: Int) {
        // work out how much this distance relates to our standard move amount, so we can
        // adjust the time by the same proportion - converting to float so we don't get
        // integer division (where it's rounded to a whole number)
        val fraction = distance.toFloat() / MOVE_DISTANCE
        // distance can be negative (i.e. moving left) so we need to use the abs function
        // to make the duration a postitive number
        val duration = abs(MOVE_TIME * fraction).toLong()
        spongeBob.animate().setDuration(duration).translationXBy(distance.toFloat())
    }
}

有更好的东西你可以做,海绵宝宝应该被称为海绵宝宝,并成为一个视图,但这是最基本的。在坐标系上也可能对您有所帮助。

谢谢!它按预期工作,并感谢添加评论,以帮助我更好地理解它。之所以没有将海绵宝宝初始化为视图,是因为我使用的是kotlinx.android.synthetic.main.activity_main。*我在Kotlin中学到,只要使用该软件包,就不必像在Java中那样初始化视图。哦,好吧,我没有弄乱这些扩展!我对视图的意思是,我使用的是视图方法和属性,比如.width,所以代码假设无论Spongebob是什么类,它都是从视图派生的-所以如果它是ImageView或其他什么,那就好了!非常感谢。它按预期工作,并感谢添加评论,以帮助我更好地理解它。之所以没有将海绵宝宝初始化为视图,是因为我使用的是kotlinx.android.synthetic.main.activity_main。*我在Kotlin中学到,只要使用该软件包,就不必像在Java中那样初始化视图。哦,好吧,我没有弄乱这些扩展!我对视图的意思是,我使用的是视图方法和属性,比如.width,所以代码假设无论Spongebob是什么类,它都是从视图派生的-所以如果它是ImageView或其他什么,那就好了!