2

ObjectAnimator.ofFloat(Object target, String xPropertyName, String yPropertyName, Path path)似乎在API 21中工作,但不是23?简单再现:这是活动和布局。试着长按文本视图,这两个级别都可以工作。不过,只需轻点上的观点:21级的作品,但不是23路径上的ObjectAnimator无法在棉花糖上工作?

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final View hello = findViewById(R.id.hello); 
     final Path p = new Path(); 
     p.addCircle(-200, 0, 100, Path.Direction.CW); 
     hello.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ObjectAnimator a = ObjectAnimator.ofFloat(hello, "translationX", "translationY", p); 
       a.setDuration(1000); 
       a.start(); 
      } 
     }); 
     hello.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       ObjectAnimator a = ObjectAnimator.ofFloat(hello, "scaleX", 2); 
       a.setDuration(1000); 
       a.start(); 
       return true; 
      } 
     }); 
    } 
} 

-

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="letstwinkle.com.pathtest.MainActivity"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello World!" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:layout_marginEnd="63dp" 
     android:layout_marginTop="210dp" 
     android:id="@+id/hello" 
     android:background="#880033"/> 
</RelativeLayout> 

这可能是与此相同的错误在六月报道。 https://code.google.com/p/android/issues/detail?id=212776

+0

我AppCompatActivity和仿真器版本22和23测试:适用于22,开始于23移动,但经过正确的冻结。 – 0X0nosugar

+1

我已经添加了一个更新监听器,并查看了动画值 - 它在API 21上有所区别,但在API 23上它是稳定的,总是与“-100”相同 - 确切而言。 –

+0

并与path.lineTo(200,200) - 它是完美的工作。需要深入到圈子里,甚至是使用textview的东西已经改变了。 –

回答

2

作为解决方法,您可以使用ValueAnimator和自定义AnimatorUpdateListener

首先你需要一个PathMeasure为你的Path。这将使您可以访问数据,如路径长度和路径上的坐标的特定距离(0 < =距离< =路径长度)。

Path p = new Path(); 
p.addCircle(-200, 0, 100, Path.Direction.CW); 
PathMeasure pathMeasure = new PathMeasure(p, true); 

然后,您将ValueAnimator设置为从0到动画的路径长度。

final ValueAnimator a = ValueAnimator.ofFloat(0,pathMeasure.getLength()); 
a.setDuration(1000); 

接下来,添加一个AnimatorUpdateListener实际操作View。我会先显示如何添加它:

MyAnimatorUpdateListener myListener = new MyAnimatorUpdateListener(hello, pathMeasure); 
a.addUpdateListener(myListener); 

继续与OnClickListener:

hello.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     a.start(); 
    } 
}); 

请注意,我改变了代码并且宣称/初始化ValueAnimatorOnClickListener的,以获得更好性能:这样,每次点击TextView时都不会重新创建,因此垃圾收集器将不再那么繁忙:)。

最后,AnimatorUpdateListener代码:

class MyAnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener 
{ 
    private View view; 
    private PathMeasure pathMeasure; 
    private float[] coordinates = new float[2]; 

    public MyAnimatorUpdateListener(View view, PathMeasure pathMeasure) 
    { 
     this.view = view; 
     this.pathMeasure = pathMeasure; 
    } 

    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     float distance = (float) animation.getAnimatedValue(); 
     pathMeasure.getPosTan(distance, coordinates, null); 
     view.setTranslationX(coordinates[0]); 
     view.setTranslationY(coordinates[1]); 
    } 
} 
+0

看起来很有前途。我所入侵的只是分别对x和y进行动画处理,而另一个则使用加速插值器和减速器来产生某种圆形。肯定看起来不太好。当我可以的时候,我会循环回去并实现它。 – user3175580

相关问题