水平进度条:在确定和不确定模式之间切换

7

我正在尝试在两种模式之间切换SeekBar,以显示流媒体的状态。

我已经确定,在XML中定义android:indeterminate标签时,正确的图形会显示:

<SeekBar
    android:id="@+id/seekBar1"
    android:indeterminate="false"
    android:progressDrawable="@android:drawable/progress_horizontal"
    android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
    android:indeterminateBehavior="cycle"
    android:indeterminateOnly="false"
    android:progress="33"
    android:secondaryProgress="66"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"></SeekBar>

示例

问题在于,当尝试通过调用setIndeterminate(true)来切换时,绘制物似乎不能正确地更改。在不确定到确定的情况下,动画停止了,在确定到不确定的情况下,什么也没有发生。

我做错了什么?

4个回答

2

我曾经遇到过同样的问题,通过调用postInvalidate()来强制刷新视图,我解决了这个问题。我使用的是Android 2.3.4。

// Configure the SeekBar
seekBar.setIndeterminate(true);
seekBar.postInvalidate();

1

SeekBar的onSizeChanged()仅初始化当前选定的背景可绘制对象,并且在SeekBar初始化时只调用一次。 因此,修复方法可能是:

public class CorrectedSeekBar extends SeekBar {

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

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

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

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        boolean current = isIndeterminate();
        setIndeterminate(!current);
        super.onSizeChanged(w, h, oldw, oldh);
        setIndeterminate(current);
        super.onSizeChanged(w, h, oldw, oldh);
    }

}

在布局的xml文件中使用<yourpackage.CorrectedSeekBar ...

这看起来不太好,可能有其他解决方法,但我认为这是主要问题(bug?)。


0

解决方案实际上非常简单,您必须使用启动动画可绘制对象的相同技巧。这个问题似乎只影响Android 2.3(也可能影响早期版本的Android)。

每当您想要设置:

progressBar.setIndeterminate(true);

请使用以下代码部分:
this.progressBar.post(new Runnable()
{
    @Override
    public void run()
    {
        progressBar.setIndeterminate(true);
    }
});

0

不要将pd设置为不确定状态,而是切换其背景。如果您想要可绘制的动画效果,我建议您查看TransitionDrawable,链接在这里:TransitionDrawable。因此,使用它实际上是添加2个不同的可绘制对象并动画它们之间的过渡。我认为这对您的情况应该有效。但是,我没有在进度对话框上执行此类操作的经验,所以无法确定。


嗨,谢谢回复。那不是我想要的;它似乎是一个混乱的hack。设置背景不起作用,因为背景是上面图片中的黑色区域。用过渡drawable替换进度条本身的空闲状态drawable 可能会起作用,但是然后我必须将主要和次要进度设置为0,然后触发过渡drawable。这是不必要的复杂 - 如果我必须这样做,我可能会使用两个seekbar和一个viewflipper。 - user668660
我所说的背景实际上是指更改进度条样式,即主进度和次进度可绘制对象。您可以根据需要在运行时动态设置它们。至于过渡可绘制对象,我认为这比使用ViewFlipper更好,而创建这样的可绘制资源只需要不到10行代码,因此我认为这并不复杂。这肯定比使用2个进度条并跟踪它们要容易。 - DArkO
是的,我刚试过了,不起作用。progressdrawable实际上是一个图层列表,所以当它被包装在转换绘制时,子绘制对象似乎无法暴露给进度条。你最终只能看到默认的可绘制对象。出于同样的原因,不定绘制对象也不会动画化。 - user668660
好的,就像我说的,我自己没有尝试过,但如果你找到另一个可行的解决方案,我很感兴趣。 - DArkO

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接