【问题标题】:Kotlin - Multiple animations at the same time and/or repeating after each otherKotlin - 多个动画同时和/或相互重复
【发布时间】:2020-02-21 17:33:36
【问题描述】:

我正在尝试在 Android Studio 中创建游戏应用。概念是:有圆圈掉下来,您需要单击/按下它们以使其消失,然后才能触摸屏幕底部的一条线。为此,我创建了一个带有圆圈作为图像的 ImageView。我让它出现在顶部,它以缓慢的速度向下移动。当我按下它时,它会消失。问题是,第二个圆圈出现在第一个圆圈之后,这很好,但是向下的动画没有开始,而且 OnClickListener 也不起作用。

下面是创建圆的代码:

/*Creation of ball via imageView.
ImageView is created on the current Activity Layout with the circle as image.
ImageView is 200x200 and appears in the middle of the screen (horizontal) on top.
*/
private fun drawBall(){
    val imageView = ImageView(this)
    imageView.setImageDrawable(getDrawable(R.drawable.ball_1))
    imageView.layoutParams = LinearLayout.LayoutParams(200, 200)
    imageView.x = (width/2.5).toFloat()
    layout.addView(imageView)
    moveBall(imageView)
}

这是动画开始的代码和圆圈消失的 OnClickListener:

//Animation of ball falling down.
private fun moveBall(imageView: ImageView){
    val valueAnimator = ValueAnimator.ofFloat(0f, height*0.68.toFloat() )
    valueAnimator.addUpdateListener {
        val value = it.animatedValue as Float
        imageView.translationY = value
    }

    valueAnimator.interpolator = LinearInterpolator()
    valueAnimator.duration = 2500
    valueAnimator.start()

    //Shooting the ball and incrementing score
    imageView.setOnClickListener(){
        imageView.visibility = View.INVISIBLE
        var currentScore = textView_score.text.toString().toInt()
        currentScore++
        textView_score.text = currentScore.toString()
    }
}

在这里你可以找到我试图创建一个圆的多个实例来倒下的代码:

//Multiple instances of the falling ball animation.
private fun startBalls(){
    val runnable = Runnable { drawBall() }
    runnable.run(){
        drawBall()
    }

    val exec = ScheduledThreadPoolExecutor(1)
    val period : Long = 1000
    exec.scheduleAtFixedRate(runnable,0, period, TimeUnit.MILLISECONDS)
    val delay : Long = 1000
    exec.scheduleWithFixedDelay(runnable, 0, delay, TimeUnit.MILLISECONDS)
}

我认为我的主要问题是我试图在哪里创建圆圈的多个实例。 提前谢谢你。

【问题讨论】:

    标签: android multithreading android-studio animation kotlin


    【解决方案1】:

    moveBall() 记录显示,当RunnableScheduledThreadPoolExecutor 启动时,代码并未在UI 线程上执行。更具体地说,valueAnimator.start() 之后的下一行根本不执行(第一次“触摸”UI 元素会导致非 UI 线程中的异常”),因此除了第一个ImageView

    因为当Runnable 的同一个实例与ScheduledThreadPoolExecutor 重用时,它也会seems to be a problem,因此您可以使用自定义Runnable 类并让它在UI 线程上调用drawBall()

    class MyRunnable(val functionToInvoke: () -> Unit): Runnable {
        override fun run() {
            Handler(Looper.getMainLooper()).post(Runnable {
                //this will run on the UI thread
                functionToInvoke.invoke()
          })
        }
    }
    

    startBalls()中的用法:

    val exec = ScheduledThreadPoolExecutor(1)
    val period : Long = 1000
    exec.scheduleAtFixedRate(MyRunnable({drawBall()}),0, period, TimeUnit.MILLISECONDS)
    val delay : Long = 1000
    exec.scheduleWithFixedDelay(MyRunnable({drawBall()}), 0, delay, TimeUnit.MILLISECONDS)
    

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多