【发布时间】: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