如何改变NumberPicker的价值与动画?价值、动画、NumberPicker

2023-09-06 04:21:38 作者:我的心寄错了地方 ゅ

我已经为Android的应用程序,其中有一个NumberPicker。我需要改变这个NumberPicker的价值,但有一个流畅的动画效果,当你触摸它像并改变它的值。

I've created an application for Android, in which there is a NumberPicker. And I need to change the value of this NumberPicker but with a smooth animation like when you touch it and change its value.

例如,假定当前值是1,这将是5,我希望NumberPicker自旋为1〜2,然后以3等等。但我想它旋转不只是瞬间改变的值!

For example, assume the current value is 1 and it's going to be 5, I want the NumberPicker to spin from 1 to 2 then to 3 and so on. But I want it to spin not just instantly change the values!

如果我用下面的code:

If I use the following code:

numberPicker.setValue(5);

它的值将立即改变到5,但我想它从1卷起至5就像当你手动触摸它,使之旋转。

its value will instantly change to 5, but I want it to roll up from 1 to 5 like when you manually touch it and make it spin.

推荐答案

我通过refelction解决它

I solved it via refelction

        /**
         * using reflection to change the value because
         * changeValueByOne is a private function and setValue
         * doesn't call the onValueChange listener.
         * 
         * @param higherPicker
         *            the higher picker
         * @param increment
         *            the increment
         */
        private void changeValueByOne(final NumberPicker higherPicker, final boolean increment) {

            Method method;
            try {
                // refelction call for
                // higherPicker.changeValueByOne(true);
                method = higherPicker.getClass().getDeclaredMethod("changeValueByOne", boolean.class);
                method.setAccessible(true);
                method.invoke(higherPicker, increment);

            } catch (final NoSuchMethodException e) {
                e.printStackTrace();
            } catch (final IllegalArgumentException e) {
                e.printStackTrace();
            } catch (final IllegalAccessException e) {
                e.printStackTrace();
            } catch (final InvocationTargetException e) {
                e.printStackTrace();
            }
        }

运行完美。我不知道为什么这个方法是私有的

works perfect. I don't know why this method is private