【问题标题】:ObjectAnimator on Path not working on Marshmallow?路径上的 ObjectAnimator 不适用于棉花糖?
【发布时间】:2016-09-21 09:16:45
【问题描述】:

ObjectAnimator.ofFloat(Object target, String xPropertyName, String yPropertyName, Path path) 似乎适用于 API 21 但不适用于 API 23? 简单复制:这是活动和布局。尝试长按文本视图,两个级别都可以。但是,只需点击视图:21 个作品,但不是 23 个。

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>

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

【问题讨论】:

  • 我用 AppCompatActivity 和模拟器版本 22 和 23 进行了测试:在 22 上工作,在 23 上开始移动,但在那之后立即冻结。
  • 我添加了一个更新侦听器,并查看了动画值 - 在 API 21 上它有所不同,但在 API 23 上它是稳定的,总是相同的“-100” - 确切地说。
  • 并且使用 path.lineTo(200,200) - 它运行良好。需要深入研究圈子的东西,甚至文本视图的东西已经改变了。
  • ImageView 的行为相同,所以它不仅仅是关于 TextView
  • 部分弧线也会发生,这才是我真正关心的;刚刚用了一个圆圈来测试。

标签: android android-animation android-6.0-marshmallow android-graphics


【解决方案1】:

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

首先,您的Path 需要一个PathMeasure。这将使您能够访问特定距离(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();
    }
});

请注意,我更改了代码并在OnClickListener 之外声明/初始化了ValueAnimator 以获得更好的性能:这样,每次单击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]);
    }
}

【讨论】:

  • 看起来很有希望。我破解的只是分别为 x 和 y 设置动画,但一个带有加速插值器,一个带有减速器,以产生某种圆形。肯定不好看。如果可以,我会返回并实现它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 2016-08-27
  • 2018-09-08
  • 1970-01-01
  • 2016-03-15
  • 2019-11-04
相关资源
最近更新 更多