我有一个属性动画,但它不平滑,即我得到了动画中的跳跃。我试着用动画师的参数,如插值器,持续时间和帧延迟,但不能得到一个平滑的效果。有谁有什么窍门/例子吗?下面是我当前设置动画师的代码:
rotationAnimator=new ObjectAnimator();
rotationAnimator.setTarget(rotationController);
rotationAnimator.setPropertyName("mapRotation");
rotationAnimator.setInterpolator(new LinearInterpolator());
ValueAnimator.setFrameDelay(24);
rotationAnimator.setDuration(ROTATION_DURATION);
rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
rotationAnimator.setRepeatMode(ValueAnimator.RESTART);
(还有一个对setFloatValues
的调用,但它出现在代码的后面,在我开始动画之前)
编辑:我被要求展示我正在制作的动画,所以它是这样的:
这是从动画中获取值的setter函数:
public void setMapRotation(float rotationDegrees)
{
if ((rotationAnimator!=null)&&(rotationAnimator.isRunning()))
rotationAnimator.cancel();
rotationDegrees%=360;
if (rotationDegrees<0) rotationDegrees+=360;
rotationView.setRotationDegrees(rotationDegrees);
if (rotationDegrees!=oldRotationDegrees)
{
notifyRotationListeners();
oldRotationDegrees=rotationDegrees;
}
}
如果你查看代码,你会看到有另一个函数被称为(setRotationDegrees),所以它是这样的:
public void setRotationDegrees(float rotationDegrees)
{
this.rotationDegrees=rotationDegrees%360;
if (this.rotationDegrees<0) this.rotationDegrees+=360;
Log.i("MapView","View degrees: " + this.rotationDegrees);
invalidate();
}
这就是失效后发生的事情:
@Override protected void dispatchDraw(Canvas canvas)
{
Log.i("MapView","Redrawing");
int saveCount=canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.rotate(rotationDegrees,getWidth()/2,getHeight()/2);
canvas.setDrawFilter(drawFilter);
canvas.getMatrix(rotationMatrix);
super.dispatchDraw(canvas);
canvas.restoreToCount(saveCount);
}
我不知道这里是不是有什么特别重的东西,但我可能错了。
发布于 2013-01-12 22:53:32
定义一个AnimatorSet
并在其上设置插值器可能会解决您的问题:
rotationAnimator=new ObjectAnimator();
rotationAnimator.setTarget(rotationController);
rotationAnimator.setPropertyName("mapRotation");
ValueAnimator.setFrameDelay(24);
rotationAnimator.setDuration(ROTATION_DURATION);
rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
rotationAnimator.setRepeatMode(ValueAnimator.RESTART);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.getChildAnimations().add(rotationAnimator);
animatorSet.setInterpolator(new LinearInterpolator());
...
animatorSet.start();
发布于 2016-04-23 22:23:25
我已经解决了这个问题,将HW加速直接添加到我的动画视图和视图容器(myViewContainer -在我的例子中是一个RelativeLayout):
if(!myViewContainer.isHardwareAccelerated())
{
myViewContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);
myAnimatedView1.setLayerType(View.LAYER_TYPE_HARDWARE, null);
myAnimatedView2.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
发布于 2013-01-12 22:46:38
动画中间的跳跃更有可能是由rotationController类中的某个东西引起的,即接收动画值"mapRotation“。或者这可能与你的布局有关。
我会想象有一些相当强烈的东西,内存或cpu的智慧,这是导致杂乱。
如果你的动画中有很多布局,那么这可能就是原因。如果在你的代码中,你有大量的findViewByID作为动画的结果被调用,那么这可能会导致速度变慢。
如果你只是动画一个简单的图像,那么我不能完全确定问题会是什么。
就我个人而言,我认为你的代码看起来很好,也许可以展示/描述你的动画。
编辑我对画布动画的了解有限,所以我快速地看了一下你是如何使用canvas.save()和canvas.restoreToCount()的,我以前没有见过它们。
这一切看起来都很好,但我确实觉得奇怪的是,有几个教程旋转画布而不是球图像来创建旋转。尽管如此,我发现改进动画效果的唯一参考是下面的link中的一个建议使用postInvalidateOnAnimation()的注释。但是不确定在哪里,也许是用invalidate()代替。
https://stackoverflow.com/questions/14294345
复制相似问题