动画搜索栏进度

发布于 2024-11-12 22:30:09 字数 360 浏览 6 评论 0原文

我的屏幕有 4 个搜索栏(如下图所示)。如果用户移动 B、C 或 DI,则计算三者的平均值并将 A 的进度设置为平均值。这是最容易的部分。我想做的是为进度条 A 设置动画,使其不会一次性跳跃(例如从 25-75 )。

为 A 制作动画的推荐方法是什么?我得到了一个简单的动画,但我每 50 毫秒调用一个 TimerTask 来将 A 增加或减少一个单位值,直到到达所需的位置。但其效率并不高。

注意:我有一个自定义 Seekbar 对象,使用它创建了eekBar 的 A、B、C 和D. 抱歉,我无法真正分享代码,但很乐意澄清任何事情。

在此处输入图像描述

I have a screen with 4 seek bars ( As shown in the image below) . If the user moves B,C or D I calculate the average of the three and setProgress of A to the average. That was the easy part . What I would like to do is animate the progress bar A such that it does not jump in one shot (for eg from 25-75 ).

What are the recommended ways to animate A? I got a simple animation up but I am calling a TimerTask every 50ms to increment or decrement A by a unit value till I reach the required position. But its not very efficient.

Note: I have a custom Seekbar object using which I have created seekBar's A,B,C & D. Sorry I cant really share the code but will be happy to clarify anything.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光下的泡沫是彩色的 2024-11-19 22:30:09

我想已经太晚了。但我找到了一种使用 ValueAnimator 来实现这一目标的方法。

ValueAnimator anim = ValueAnimator.ofInt(0, seekBar.getMax());
anim.setDuration(1000);
anim.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
    int animProgress = (Integer) animation.getAnimatedValue();
    seekBar.setProgress(animProgress);
    }
});
anim.start();

I guess its too late. But I found a way to achieve this using ValueAnimator.

ValueAnimator anim = ValueAnimator.ofInt(0, seekBar.getMax());
anim.setDuration(1000);
anim.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
    int animProgress = (Integer) animation.getAnimatedValue();
    seekBar.setProgress(animProgress);
    }
});
anim.start();
记忆消瘦 2024-11-19 22:30:09

我已经使用ObjectAnimator完成了它。

private void animateProgression(int progress) {
    final ObjectAnimator animation = ObjectAnimator.ofInt(mySeekBar, "progress", 0, progress);
    animation.setDuration(3000);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.start();
    mySeekBar.clearAnimation();
}

I have done it using ObjectAnimator.

private void animateProgression(int progress) {
    final ObjectAnimator animation = ObjectAnimator.ofInt(mySeekBar, "progress", 0, progress);
    animation.setDuration(3000);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.start();
    mySeekBar.clearAnimation();
}
故乡的云 2024-11-19 22:30:09

这适用于任何 Android 版本,无需使用任何动画。

private void animateSeek(final SeekBar mSeek, final int toVal) {
    new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
                if (mSeek.getProgress() == toVal) {
                    break;
                }
                int mProgress = mSeek.getProgress();
                if (mProgress < toVal) {
                    mProgress++;

                } else if (mProgress > toVal) {
                    mProgress--;
                }

                mSeek.setProgress(mProgress);

                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

This works on any Android version, without using any animations.

private void animateSeek(final SeekBar mSeek, final int toVal) {
    new Thread(new Runnable() {
        @Override
        public void run() {

            while (true) {
                if (mSeek.getProgress() == toVal) {
                    break;
                }
                int mProgress = mSeek.getProgress();
                if (mProgress < toVal) {
                    mProgress++;

                } else if (mProgress > toVal) {
                    mProgress--;
                }

                mSeek.setProgress(mProgress);

                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}
守望孤独 2024-11-19 22:30:09

我需要的代码就像 @sidhanshu_udawat 所做的那样。
这是一个很好的起点。巨大的胜利,因为它在自己的线程上运行。谢谢你的例子。

然而,当我使用该代码并尝试它时,出现了一个问题:它会递增然后停止。

我更改了代码(粘贴在下面),现在如果您使用如下代码发送搜索栏,您将看到搜索栏按比例上下动画。

isRunning - 从您的 Activity 进行控制

此外,我需要在有人单击按钮时停止动画,因此我将布尔值 isRunning 添加为我的 Activity 的成员。现在,当他们单击按钮时,我将 isRunnig 设置为 false 并且 SeekBar 停止。

像下面这样设置 SeekBar,然后调用该方法:

animSeekBar.incrementProgressBy(1);
  animSeekBar.setMax(100);
  animateSeek(animSeekBar,100);

private void animateSeek(final SeekBar mSeek, final int toVal) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                boolean isIncrementing = true;
                int mProgress = 0;
                isRunning = true;

                while (isRunning) {
                    if (isIncrementing){
                        mProgress++;
                    }
                    else{
                        mProgress--;
                    }
                    mSeek.setProgress(mProgress);
                    if (mProgress >= toVal) {
                        isIncrementing = false;

                    } else if (mProgress <= 0) {
                        isIncrementing = true;
                    }
                    try {
                        Thread.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

I needed code that was just like what @sidhanshu_udawat had done.
That was a great base to start from. Big win because it runs on its own thread. Thanks for that example.

However, when I took that code and tried it there was an issue that it would increment up and then stop.

I altered the code (pasted below) and now if you send in your seekbar with code like the following you will see that the seekbar animates up and down the scale.

isRunning - Control From Your Activity

Also, I needed to stop the animation when someone clicked a button so I added the boolean isRunning as a member on my Activity. Now when they click the button, I set isRunnig to false and the SeekBar stops.

Set up your SeekBar like the following and then call the method:

animSeekBar.incrementProgressBy(1);
  animSeekBar.setMax(100);
  animateSeek(animSeekBar,100);

private void animateSeek(final SeekBar mSeek, final int toVal) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                boolean isIncrementing = true;
                int mProgress = 0;
                isRunning = true;

                while (isRunning) {
                    if (isIncrementing){
                        mProgress++;
                    }
                    else{
                        mProgress--;
                    }
                    mSeek.setProgress(mProgress);
                    if (mProgress >= toVal) {
                        isIncrementing = false;

                    } else if (mProgress <= 0) {
                        isIncrementing = true;
                    }
                    try {
                        Thread.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
杀お生予夺 2024-11-19 22:30:09

如果您使用 Honeycomb,那么您可以使用 ViewPropertyAnimator
http://android-developers.blogspot.com/2011/05/introducing -viewpropertyanimator.html

3.0之前的动画非常有限,但作为您的方法的替代方案,您可以使用动画侦听器:

anim.setAnimationListener(new AnimationListener() {
  public void onAnimationStart(Animation anim) {
     //moveseekbar
  }
  public void onAnimationRepeat(Animation anim) {}
  public void onAnimationEnd(Animation anim) {
     //call animation again if movement not complete
  }
});   

不要忘记调用setFillAfter(布尔值 fillAfter)
因为动画 <3.0 实际上并不移动视图。它只是给人一种视图已经移动的错觉。如果您仍然希望搜索栏继续接收触摸,则重要的区别。

If you are using Honeycomb then you can use ViewPropertyAnimator
http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html

The animation before 3.0 is quite limited but as an alternative to your method you could use an animation listener:

anim.setAnimationListener(new AnimationListener() {
  public void onAnimationStart(Animation anim) {
     //moveseekbar
  }
  public void onAnimationRepeat(Animation anim) {}
  public void onAnimationEnd(Animation anim) {
     //call animation again if movement not complete
  }
});   

Don't forget to call setFillAfter(boolean fillAfter)
because the animation <3.0 doesn't actually move the view. It just gives the illusion that the view has moved. Important distinction if you still want the seekbar to continue to receive touches.

只等公子 2024-11-19 22:30:09

我通过指数函数而不是单位增量迭代计时器任务来减少迭代次数。仍然希望得到更好的答案:-)

I reduced the number of iterations by iterating the timer task over an exponential function instead of unit increments. Still hoping for better answers :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文