我目前正在尝试为动画叠加制作通用方法。这样工作:将活动作为参数传递->方法将所需的视图附加到Activity的rootview上,使其无效并在这种情况下开始旋转动画。但是,动画的轴心不适用于正确的视图,除此以外,其他所有方法都可以正常工作,因此它只是从视图的X和Y旋转,这是不正确的。
方法:
fun createLoading(activity: Activity, activityWidth: Int): Animation {
val loadingAnimation = AnimationUtils.loadAnimation(activity, R.anim.animation_loading_rotate)
val parent = activity.window.decorView.findViewById<ViewGroup>(android.R.id.content)
val loadingBg = ImageView(activity)
loadingBg.tag = "customLoadingBg"
loadingBg.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
loadingBg.visibility = View.VISIBLE
loadingBg.setImageResource(R.drawable.darken_background)
val loadingImage = ImageView(activity)
loadingImage.tag = "customLoadingImage"
loadingImage.setImageResource(R.drawable.icon_web)
loadingImage.visibility = View.VISIBLE
loadingImage.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT) //TODO pivot doesn't work. Again...
loadingImage.layoutParams.width = (activityWidth * 0.1).toInt()
loadingImage.layoutParams.height = (activityWidth * 0.1).toInt()
loadingImage.pivotX = (activityWidth * 0.05).toFloat()
loadingImage.pivotY = (activityWidth * 0.05).toFloat()
loadingImage.x = ((activityWidth / 2 - (activityWidth * 0.1 / 2).toInt()).toFloat())
loadingImage.y = 0f
parent.addView(loadingBg)
parent.addView(loadingImage)
loadingAnimation!!.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
parent.removeView(parent.findViewWithTag<ImageView>("customLoadingImage"))
parent.removeView(parent.findViewWithTag<FrameLayout>("customLoadingBg"))
}
override fun onAnimationStart(animation: Animation?) {
loadingBg.alpha = 0.5f
loadingImage.pivotX = (activityWidth * 0.05).toFloat()
loadingImage.pivotY = (activityWidth * 0.05).toFloat()
Log.d("loadingimg_pivotX", loadingImage.pivotX.toString())
loadingBg.bringToFront()
loadingImage.bringToFront()
}
})
loadingImage.post {
activity.runOnUiThread {
parent.invalidate()
parent.getChildAt(parent.childCount - 1).startAnimation(loadingAnimation)
}
}
return loadingAnimation
}
如您所见,我什至尝试将其放入动画的onAnimationStart方法中,但不起作用。 我什至尝试使用预定义的XML动画并将其应用于视图,但这些方法都不适合我。 欣赏任何想法。
编辑: R.anim.animation_loading_rotate的声明
android:duration="750"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360"
答案 0 :(得分:2)
很高兴有样品,可以在本地检查。我不确定在此示例中您在谈论哪个枢轴。在我们的案例中,数据透视表应相对于动画下的视图,而不是父级。因此,在我们的情况下,如果轮换使用,则下一步将是正确的更改。
loadingImage.pivotX = (loadingImage.getWidth() * 0.5).toFloat()
loadingImage.pivotY = (loadingImage.getHeight() * 0.5).toFloat()
最后,我强烈建议您使用Android Property Animation
而不是View Animation
。您只需将示例替换为ObjectAnimator
,就可以解决您的问题。因为ObjectAnimator
不仅可以制作动画,还可以浏览View更改,因此简单的View动画只是重绘对象。检查更多详细信息from the documentation。