Почему setPivotX () просто заменяет представления? - PullRequest
0 голосов
/ 22 октября 2018

В коде - после того, как первая часть анимации поворачивается, и ... позиция просмотра тоже! (Странное поведение)

Вот код (условие - один 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>

И вот что я хочу сделать (левая часть; правая часть - это то, что происходит на самом деле) (желтая точка - точка разворота): enter image description here Iсмотрел исходный код setPivotX, но он мне ничего не говорит.Может быть, я должен назвать кого-то из invalidate-методов просмотра?

1 Ответ

0 голосов
/ 24 октября 2018

Я воссоздал этот код и запустил его на своем телефоне Android.Он дает именно ту анимацию, которую вы ищете.

Я создал изображение, соответствующее вашему изображению «спички».Например, я сделал его шириной 400 пикселей и высотой 40.

Я дал ему идентификатор: '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);


}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...