首页 > 解决方案 > 为什么 setPivotX() 只是替换视图?

问题描述

在代码中 - 在视图的动画枢轴的第一部分发生变化并且......视图位置也是如此!(这是奇怪的行为)

这是代码(规定 - 一个ValueAnimator):

ValueAnimator animator = ValueAnimator.ofFloat(0,180);


float firstAnimLineX = ((47.5f * Resources.getSystem().getDisplayMetrics().density));
float firstAnimLineY = ((2.5f * Resources.getSystem().getDisplayMetrics().density));
float secondAnimLineX = ((47.5f * Resources.getSystem().getDisplayMetrics().density));
float secondAnimLineY = ((47.5f * Resources.getSystem().getDisplayMetrics().density));

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
    view.setPivotX((Float) animator.getAnimatedValue()>180/2?firstAnimLineX : secondAnimLineX);
    view.setPivotY((Float) animator.getAnimatedValue()>180/2?firstAnimLineY : secondAnimLineY);

    view.setRotation((Float) animator.getAnimatedValue());

    }
});

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/frameLayout">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="5dp"
            android:layout_gravity="center_horizontal"
            android:id="@+id/line"
            />
    </FrameLayout>
</RelativeLayout>

这就是我想要做的(左边部分;右边部分 - 这是真正发生的事情)(黄色点 - 枢轴):在此处输入图像描述 我看了 setPivotX 的源代码,但它什么也没说。也许我应该打电话给有无效观点的人?

标签: androidanimation

解决方案


我重新创建了这段代码并在我的 Android 手机上运行它。它提供了您正在寻找的确切动画。

我创建了一个图像来匹配您的“火柴棍”图像。例如,我将其设置为 400 px 宽 x 40 高。

我给了它 id: 'bar'

ImageView bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bar = findViewById(R.id.bar);
    bar.getLayoutParams().width = 400; // I set the dimensions here.
    bar.getLayoutParams().height = 40; // I set the dimensions here.
    bar.setX(0); bar.setY(0); // Then I positioned it in upper left corner.

    bar.setPivotX(380); // I set the pivot a little inside the image so that it looks kinda like it is pivoting on a nail.
    bar.setPivotY(20);
    bar.animate().rotation(-90).setDuration(2000); // Here it animates from your first image above, to your second image in 2 seconds.

// Here I used a postDelay to allow the first animation to finish its' 2 seconds 'before' calling the second animation. No need for an animate() - listener now.

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            bar.setY(360); // This re-positions the original image to the 
                           // location where the animation ended.
            bar.setRotation(90); // Because the original image was horizontal, this turns it vertical.

// Now we finish the animation to your 3rd image above.

            bar.animate().rotation(0).setDuration(2000);
        }
    },2100);


}

推荐阅读