P
P
PK_D2018-05-04 15:52:13
Android
PK_D, 2018-05-04 15:52:13

How to make the animation speed the same on different Android devices?

Hello.
Can you please tell me how to make the animation the same on different Android devices?
I have a TextView in the form of a circle, which should be smoothly increased to a certain size in 0.7 seconds.
I do it like this:
layout:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/oval_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/shape_oval_background" />

    </RelativeLayout>

drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid
        android:color="#B6CFF5"/>

</shape>

activity:
View ovalBackground = findViewById(R.id.oval_background);
ValueAnimator animator = ValueAnimator.ofFloat(0, 230);
animator.setDuration(700);

AnimatorUpdateListener animatorUpdateListener = new AnimatorUpdateListener(this);
animator.addUpdateListener(animatorUpdateListener);

AnimatorUpdateListener:
public class AnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        float animateValue = (float) valueAnimator.getAnimatedValue();

        final float scale = getResources().getDisplayMetrics().density;
        int widthPixels = (int)animateValue * scale + 0.5f);
        int heightPixels = (int)animateValue * scale + 0.5f);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(widthPixels, heightPixels);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

        ovalBackground.setLayoutParams(layoutParams);
    }
}

This code works and does what it needs to do - the circle grows to the given size.
My problem is that on the emulator and on the device the animation happens at different speeds.
I think it's a matter of different pixel densities.
Empirically, I found that the increase occurs at the same speed both on the device and on the emulator with the following parameters:
  • animator.setDuration(347);
  • animator.setDuration(652);

I tried to find some relationship between these values ​​and the corresponding DisplayMetrics parameters (density, scaledDensity) - without success.
Can you tell me how to solve this kind of problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question