我想通过从左到右填充按钮背景来设置动画。我用这个。当我把它放在onCreate
中时,它工作正常,但当用户在我的问答中选择正确答案时,我需要动画按钮。这是我的检查答案方法
fun checkAnswer(selectedAnswer: Button) {
enableAnswersButton(false)
if (selectedAnswer.text.toString() == listOfTests[currentQuestion].rightAnswer){
val layerDrawable = selectedAnswer.background as LayerDrawable
mClipDrawable =
layerDrawable.findDrawableByLayerId(R.id.clip_drawable_false) as ClipDrawable
mAnimator = TimeAnimator()
mAnimator!!.setTimeListener(this)
selectedAnswer.setOnClickListener {
if (!mAnimator!!.isRunning) {
mCurrentLevel = 0;
mAnimator!!.start()
}
}
} else {
val layerDrawable = selectedAnswer.background as LayerDrawable
mClipDrawable =
layerDrawable.findDrawableByLayerId(R.id.clip_drawable_true) as ClipDrawable
mAnimator = TimeAnimator()
mAnimator!!.setTimeListener(this)
selectedAnswer.setOnClickListener {
if (!mAnimator!!.isRunning) {
mCurrentLevel = 0;
mAnimator!!.start()
}
}
问题是动画作品时有发生。有时onTimeUpdate
方法不被调用,动画也不起作用。
override fun onTimeUpdate(animation: TimeAnimator?, totalTime: Long, deltaTime: Long) {
mClipDrawable!!.level = mCurrentLevel
if (mCurrentLevel >= MAX_LEVEL) {
mAnimator!!.cancel()
} else {
mCurrentLevel = min(MAX_LEVEL, mCurrentLevel + LEVEL_INCREMENT)
}
}
请帮助
我已经调查过了!。那里的实现很好,只是你不应该使用
selectedAnswer.setOnClickListener {//<===this part need to be removed as its setting onClick which will work if next time same button is clicked
if (!mAnimator!!.isRunning) {
mCurrentLevel = 0;
mAnimator!!.start()
}
}
该部件需要删除,因为它的设置是点击,如果下次点击相同的按钮,点击就会起作用
如果只想在按下任何应答按钮时播放动画。您需要在设置答案文本视图/按钮选项时设置动画师
下面的代码可以给你一个想法
firstOptionTextView.setText(Put the Option here);
if (firstOptionTextView.text.toString() == listOfTests[currentQuestion].rightAnswer){
val layerDrawable = selectedAnswer.background as LayerDrawable
mClipDrawable =
layerDrawable.findDrawableByLayerId(R.id.clip_drawable_false) as ClipDrawable
mAnimator = TimeAnimator()
mAnimator!!.setTimeListener(this)
firstOptionTextView.setOnClickListener {
if (!mAnimator!!.isRunning) {
mCurrentLevel = 0;
mAnimator!!.start()
}
}
} else {
val layerDrawable = selectedAnswer.background as LayerDrawable
mClipDrawable =
layerDrawable.findDrawableByLayerId(R.id.clip_drawable_true) as ClipDrawable
mAnimator = TimeAnimator()
mAnimator!!.setTimeListener(this)
firstOptionTextView.setOnClickListener {
if (!mAnimator!!.isRunning) {
mCurrentLevel = 0;
mAnimator!!.start()
}
}
现在,由于这个选项有一个动画师,无论是正确的还是不正确的,它都会被播放。