小数或双数之间的转换

Transition between decimal or double numbers

想象一个数字 10,然后在用户单击按钮后它变为 100。但是如何进行有效的转换

10 -> 100,

将显示像

这样的值

12, 15, 18, ..., 97, 100 over 1 second.

我在 "Cookie clicker" 中看到过类似的东西,但在源代码中找不到任何关于这种转换的信息。

我有一个循环的想法(for number1 < number2, do number1++),对于小数它会很好地工作,但是如果 10 变成 10 亿,那么循环可能会冻结整个应用程序。

第二个想法是获得附加值 (100-10=90) 并除以 30 或 60 帧,并将此值与每一帧相加。但是如果丢帧会发生什么? - 可能不会增加价值。如果用户双击或系统自动添加值怎么办?

希望它能让我知道我需要什么样的数字转换。 也许我忽略了并且有一个简单的方法?感谢任何帮助。

我猜你可以通过使用不同的线程来完成。只有主线程与 UI 一起工作,因此您可以将间隔分成小间隔并在不同的 threads.After 中进行转换,将它们发送到主线程并打印。希望它会有所帮助。

希望这个使用 ValueAnimator 的小演示能启发您找到合适的解决方案。
您可以指定动画的持续时间(请参阅代码),甚至可以通过说 mAnimator.setFrameDelay(frameDelay);.
来调整帧速率 通过使用 animator.isRunning()animator.isStarted(),您可以在当前动画运行时防止双击故障或其他不需要的行为。

主要Activity:

/** ValueAnimator demo */
public class MainActivity extends Activity {

    ValueAnimator mAnimator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView tv = (TextView) findViewById(R.id.textview);
        mAnimator = ValueAnimator.ofInt(1, 100).setDuration(1000);
        mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mAnimator.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(final ValueAnimator animator) {

                final Integer value = (Integer) animator.getAnimatedValue();
                tv.setText(String.format("%04d", value));
            }

        });
        mAnimator.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animator) {

                super.onAnimationEnd(animator);
                final int endValue = Integer.parseInt((String) tv.getText());
                mAnimator.setIntValues(endValue, endValue + 100);
            }
        });
    }

    /** Button callback */
    public void onClick(final View view) {

        if (!mAnimator.isStarted() && !mAnimator.isRunning()) {
            mAnimator.start();
        }
    }
}  

简单的演示布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:typeface="monospace"
    android:text="0001" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Gimme +100" 
    android:onClick="onClick">
</Button>

这是另一个演示(希望这能回答您的第 2 个问题),它根据单击或双击按钮实现不同的行为。只需试验一下,您现在就有了构建自己行为的基本构建块...

/** ValueAnimator demo */
public class MainActivity extends Activity {

    ValueAnimator mAnimator;
    TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTv = (TextView) findViewById(R.id.textview);
        mAnimator = ValueAnimator.ofInt(1, 100).setDuration(1000);
        mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mAnimator.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(final ValueAnimator animator) {
                final Integer value = (Integer) animator.getAnimatedValue();
                mTv.setText(String.format("%04d", value));
            }

        });

        final Button button = (Button) findViewById(R.id.button);
        final GestureDetector gestureDetector = new GestureDetector(this,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        performAnimation(100);
                        return true;
                    }

                    @Override
                    public boolean onSingleTapConfirmed(MotionEvent e) {
                        performAnimation(0);
                        return true;
                    }
                });

        button.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
    }

    /** starts animation */
    private void performAnimation(final int offset) {

        if (!mAnimator.isStarted() && !mAnimator.isRunning()) {
            final int endValue = Integer.parseInt((String) mTv.getText());
            mAnimator.setIntValues(endValue + offset, endValue + 100 + offset);
            mAnimator.start();
        }

    }
}

不要忘记替换布局文件,因为按钮的点击属性已被删除:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:typeface="monospace"
        android:text="0001" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gimme +100" >
    </Button>

</LinearLayout>