android - 动画结束不平滑过渡到新图像
问题描述
我有一个图像视图,它根据状态显示向上/向下箭头BottomSheet
,我正在尝试为箭头设置动画,使其看起来更好,但在动画结束时它不会平滑过渡到新图像。我看到旧图像大约有一毫秒,直到它切换到更新的图像。
这是我的代码
private val bottomSheetCallback = object:BottomSheetBehavior.BottomSheetCallback(){
override fun onSlide(p0: View, p1: Float) {}
/**
* Listen to state changes of the bottom sheet
*/
@SuppressLint("SwitchIntDef")
override fun onStateChanged(p0: View, state: Int) {
when(state){
BottomSheetBehavior.STATE_COLLAPSED -> startRotateAnimation()
BottomSheetBehavior.STATE_EXPANDED -> startRotateAnimation()
}
}
private fun startRotateAnimation(){
val rotateAnimation:RotateAnimation = RotateAnimation(0f,180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
rotateAnimation.duration = 500
rotateAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {
}
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
when(_scannedBarcodesList.state){
BottomSheetBehavior.STATE_COLLAPSED -> expand.setImageResource(R.drawable.ic_keyboard_arrow_up_24px)
BottomSheetBehavior.STATE_EXPANDED -> expand.setImageResource(R.drawable.ic_keyboard_arrow_down_24px)
}
}
})
expand.startAnimation(rotateAnimation)
}
}
我正在尝试将图像旋转 180 度(例如向上箭头指向下方),然后在动画结束时实际将图像视图切换到向下箭头,但就像我说的那样,动画似乎在图像更改显示之前结束向上箭头一毫秒,所以它看起来很糟糕。
如何在图像切换之前摆脱动画结束?
解决方案
当动画结束导致 UI 卡顿时,您正在更改图像。
试试这个方法
public static void rotateView(ImageView iv, int stateOfBottomSheet){
RotateAnimation rotateAnimation;
if(stateOfBottomSheet == BottomSheetBehavior.STATE_COLLAPSED){
rotateAnimation = new RotateAnimation(180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
}else {
rotateAnimation = new RotateAnimation(0.0f,180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
}
rotateAnimation.setInterpolator(new DecelerateInterpolator());
rotateAnimation.setRepeatCount(0);
rotateAnimation.setDuration(300);
rotateAnimation.setFillAfter(true);
iv.startAnimation(rotateAnimation);
}
推荐阅读
- java - 在不创建常量池的情况下编译/反汇编 Java?
- algorithm - 在非常大的集合中找到整数的“最近邻”的算法或数据结构
- powershell - 删除“空”文件夹
- hibernate - 如何避免在 Spring Data Jpa 中保存时获取 @ManyToOne 子实体?
- java - Java理解继承:父类的getter和setter
- java - 在 Java 中创建 JEP SubInterpreter 时遇到“致命的 Python 错误”
- php - Symfony QueryBuilder - 检查一个数组是否包含另一个数组的任何项目
- docker - 描述容器中的路径 - Ansible
- c++ - clang tidy 在循环中抱怨 push_back into vector
- kubernetes - 如果一个 pod 死了,它发出的 cookie 和会话也会消失吗?