仅使用时间戳创建进度条?

时间:2017-05-09 00:05:01

标签: java android media-player

是否有可能创建一个进度条,可以根据歌曲开始播放的时间以及歌曲的持续时间为歌曲的剩余时间设置动画?

例如,收集时间播放的int开始了,并再次比较ms的长度?

我理解这个概念,但不能完全理解我的想法!

编辑:为了澄清,我可以获得剩余的时间和时间的开始,但我不知道如何将它们组合成动画条,然后以XML格式显示。

1 个答案:

答案 0 :(得分:0)

创建自定义animatedProgressBar

public class AnimateProgressBar extends ProgressBar {

    private static final Interpolator DEFAULT_INTERPOLATER = new AccelerateDecelerateInterpolator();

    private ValueAnimator animator;
    private ValueAnimator animatorSecondary;
    private boolean animate = true;

    public AnimateProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AnimateProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AnimateProgressBar(Context context) {
        super(context);
    }

    public boolean isAnimate() {
        return animate;
    }

    public void setAnimate(boolean animate) {
        this.animate = animate;
    }

    @Override
    public synchronized void setProgress(int progress) {
        if (!animate) {
            super.setProgress(progress);
            return;
        }
        if (animator != null)
            animator.cancel();
        if (animator == null) {
            animator = ValueAnimator.ofInt(getProgress(), progress);
            animator.setInterpolator(DEFAULT_INTERPOLATER);
            animator.addUpdateListener(new AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    AnimateProgressBar.super.setProgress((Integer) animation.getAnimatedValue());
                }
            });
        } else
            animator.setIntValues(getProgress(), progress);
        animator.start();

    }

    @Override
    public synchronized void setSecondaryProgress(int secondaryProgress) {
        if (!animate) {
            super.setSecondaryProgress(secondaryProgress);
            return;
        }
        if (animatorSecondary != null)
            animatorSecondary.cancel();
        if (animatorSecondary == null) {
            animatorSecondary = ValueAnimator.ofInt(getProgress(), secondaryProgress);
            animatorSecondary.setInterpolator(DEFAULT_INTERPOLATER);
            animatorSecondary.addUpdateListener(new AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    AnimateProgressBar.super.setSecondaryProgress((Integer) animation
                            .getAnimatedValue());
                }
            });
        } else
            animatorSecondary.setIntValues(getProgress(), secondaryProgress);
        animatorSecondary.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (animator != null)
            animator.cancel();
        if (animatorSecondary != null)
            animatorSecondary.cancel();
    }

}

布局

<FrameLayout
        android:id="@+id/progress_bar_frame"
        android:layout_below="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="3dp">

        <my.gov.islam.smarthalal.ui.custom.AnimateProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_marginTop="-3dp"
            android:layout_gravity="top"
            android:max="100"
            android:progressDrawable="@drawable/progress_bar_webview"
            android:background="@android:color/transparent"/>

    </FrameLayout>

在代码中,只设置歌曲持续时间的最大值,例如currentTime / totaltime * 100

example : currentTime = 30 seconds
          totalTime = 280 seconds

progressBar.setMax(100); // set the maximum in persentage

then, in song callback method,

progressBar.setProgress(currentTime/280 * 100);