如何在拖动视图时使用旋转动画以使中心位置不断变化?解决fromDegree toDegree问题
How to use a rotate animation while dragging view so center position keeps changing? To solve fromDegree toDegree issue
在我的 recyclerview 中,当用户拿着一个项目并移动它时,我希望所有项目都 jiggle/wobble/wiggle。我面临的问题是,当用户持有该项目并将其移动到相同的视图类型时,摆动是可以的,但是当有人将它拖到 recyclerview 的顶部时(这是一个 header 创建为视图类型recyclerview)摆动增加了很多。
在研究这些值时,我意识到这是因为即使旋转角度相同,它离项目中心越远,旋转也会增加。
我也尝试用 object 动画师来做这件事,但它没有帮助,因为它也有同样的旋转角度问题。
这是我的摆动代码
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="5" />
这是一段视频,展示了它的外观 -
link
关于开始动画的 bindView 逻辑
((VHItem) holder).rlContainer.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View view)
{
if (buPostModelList != null)
{
startAnimationItem = true;
isDragCover = true;
isEditCoverImage = false;
for (int i = 0; i <= buPostModelList.size(); i++)
{
if (recyclerView.getChildAt(i) != null && recyclerView.getChildViewHolder(recyclerView.getChildAt(i)).getItemViewType() != TYPE_HEADER)
{
recyclerView.getChildAt(i).startAnimation(AnimationUtils.loadAnimation(context, R.anim.jiggle));
}
}
touchHelper.startDrag(holder);
}
return true;
}
});
编辑
示例项目 - link
当你移动视图时,旋转的中心仍然在起始位置,但视图仍然来回移动五度,所以就像从旋转木马的中心移动到外围五度运动在相同时间内覆盖更远的距离。
我建议你换一个没有这个问题的ObjectAnimator。
ObjectAnimator
This subclass of ValueAnimator provides support for animating properties on target objects. The constructors of this class take parameters to define the target object that will be animated as well as the name of the property that will be animated. Appropriate set/get functions are then determined internally and the animation will call these functions as necessary to animate the property.
jiggle.xml
这是用于抖动效果的新 ObjectAnimator xml。它与您的 jiggle.xml.
非常相似
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:propertyName="rotation"
android:repeatCount="-1"
android:repeatMode="reverse"
android:valueFrom="-5"
android:valueTo="5"
android:valueType="floatType" />
VHItem
更新了具有动画师支持的视图持有者。
class VHItem extends RecyclerView.ViewHolder {
private ImageView ivCollectionImage, ivRemoveIcon;
private RelativeLayout rlContainer;
private Animator mAnimator;
public VHItem(View itemView) {
super(itemView);
ivCollectionImage = itemView.findViewById(R.id.ivCollectionImage);
ivRemoveIcon = itemView.findViewById(R.id.ivRemoveIcon);
rlContainer = itemView.findViewById(R.id.rlContainer);
}
// Start animation. Inflate the animator lazily.
public void startAnimator() {
if (mAnimator == null) {
mAnimator = AnimatorInflater.loadAnimator(context, R.animator.jiggle);
}
mAnimator.setTarget(itemView);
mAnimator.start();
}
// Stop the animation. Set the rotation back to zero.
public void stopAnimator() {
if (mAnimator != null) {
itemView.setRotation(0);
mAnimator.cancel();
}
}
}
您需要更新适配器的其余部分才能使用新动画。
在我的 recyclerview 中,当用户拿着一个项目并移动它时,我希望所有项目都 jiggle/wobble/wiggle。我面临的问题是,当用户持有该项目并将其移动到相同的视图类型时,摆动是可以的,但是当有人将它拖到 recyclerview 的顶部时(这是一个 header 创建为视图类型recyclerview)摆动增加了很多。
在研究这些值时,我意识到这是因为即使旋转角度相同,它离项目中心越远,旋转也会增加。
我也尝试用 object 动画师来做这件事,但它没有帮助,因为它也有同样的旋转角度问题。
这是我的摆动代码
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="5" />
这是一段视频,展示了它的外观 - link
关于开始动画的 bindView 逻辑
((VHItem) holder).rlContainer.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View view)
{
if (buPostModelList != null)
{
startAnimationItem = true;
isDragCover = true;
isEditCoverImage = false;
for (int i = 0; i <= buPostModelList.size(); i++)
{
if (recyclerView.getChildAt(i) != null && recyclerView.getChildViewHolder(recyclerView.getChildAt(i)).getItemViewType() != TYPE_HEADER)
{
recyclerView.getChildAt(i).startAnimation(AnimationUtils.loadAnimation(context, R.anim.jiggle));
}
}
touchHelper.startDrag(holder);
}
return true;
}
});
编辑 示例项目 - link
当你移动视图时,旋转的中心仍然在起始位置,但视图仍然来回移动五度,所以就像从旋转木马的中心移动到外围五度运动在相同时间内覆盖更远的距离。
我建议你换一个没有这个问题的ObjectAnimator。
ObjectAnimator
This subclass of ValueAnimator provides support for animating properties on target objects. The constructors of this class take parameters to define the target object that will be animated as well as the name of the property that will be animated. Appropriate set/get functions are then determined internally and the animation will call these functions as necessary to animate the property.
jiggle.xml
这是用于抖动效果的新 ObjectAnimator xml。它与您的 jiggle.xml.
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="100"
android:propertyName="rotation"
android:repeatCount="-1"
android:repeatMode="reverse"
android:valueFrom="-5"
android:valueTo="5"
android:valueType="floatType" />
VHItem
更新了具有动画师支持的视图持有者。
class VHItem extends RecyclerView.ViewHolder {
private ImageView ivCollectionImage, ivRemoveIcon;
private RelativeLayout rlContainer;
private Animator mAnimator;
public VHItem(View itemView) {
super(itemView);
ivCollectionImage = itemView.findViewById(R.id.ivCollectionImage);
ivRemoveIcon = itemView.findViewById(R.id.ivRemoveIcon);
rlContainer = itemView.findViewById(R.id.rlContainer);
}
// Start animation. Inflate the animator lazily.
public void startAnimator() {
if (mAnimator == null) {
mAnimator = AnimatorInflater.loadAnimator(context, R.animator.jiggle);
}
mAnimator.setTarget(itemView);
mAnimator.start();
}
// Stop the animation. Set the rotation back to zero.
public void stopAnimator() {
if (mAnimator != null) {
itemView.setRotation(0);
mAnimator.cancel();
}
}
}
您需要更新适配器的其余部分才能使用新动画。