SeekBar:ProgressDrawable 仅在第一个进度更改后显示

发布于 2024-12-27 11:27:56 字数 2406 浏览 1 评论 0原文

我想实现一个简单的 HSV 颜色选择器,非常适合我的应用程序。正如已知的颜色选择器形式,我想根据其他seekBars的值更改seekBars的背景。

但是当我开始活动时,我看到了这个:

在此处输入图像描述

第一次触摸其中一个搜索栏后(没有不管怎样)我得到这个结果,第一次触摸后,布局保持这样,并且不会随着进一步的触摸而改变(这也是我根据我的布局所期望的,顶部的 2 个eekBar 是 20dip,下面的 1 个是一样高作为拇指):

在此处输入图像描述

就像第二张图片中所示,我希望从一开始就拥有seekBar。

这是我的布局:

<SeekBar
    android:id="@+id/SbColorPickerHue"
    android:layout_width="match_parent"
    android:layout_height="20dip"
    android:max="360"
    android:thumb="@drawable/colorpickerthumb" />
<SeekBar
    android:id="@+id/SbColorPickerSaturation"
    android:layout_width="match_parent"
    android:layout_height="20dip"
    android:max="255" 
    android:thumb="@drawable/colorpickerthumb"/>
<SeekBar
    android:id="@+id/SbColorPickerValue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="255" 
    android:thumb="@drawable/colorpickerthumb"/>

在 onProgressChanged() 中使用 OnSeekBarChangeListener,当然在 onCreate 中(以初始化eekBar),我正在调用以下函数(为了安全起见,仅显示大量代码):

    private void setGradients() {
        mHueGradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,mColors);
        mHueGradient.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        mHueGradient.setCornerRadius(mRadius);
        mHueGradient.setStroke(mStrokeWidth, mStrokeColor);
        mSbHue.setBackgroundDrawable(mHueGradient);
    }

我已经尝试了许多不同的事情:

  • 在onCreate中设置渐变两次,
  • 在onRescume中再次设置渐变,
  • 如布局xml中所示,我将一些SeekBar设置为固定高度(20dp)并且其他的则为wrap_content。行为发生了一些变化(如图所示)。但还是不满足。
  • 使seekBar 或Drawable
  • Force Layout
  • 无效,测量drawable

结果始终相同。

我在这里做错了什么?我错过了什么吗?

编辑1:

SeekBar 和 ChangeListener 定义如下:

    mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
    setGradients();
    mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            setGradients();
        }
    });

I want to implement a easy hsv color picker that fit well into my app. As known form color Pickers I would like to change the background of the seekBars according to the value of the other seekBars.

But when I start the activity I am seeing this:

enter image description here

After the first touch of one of the seekBars (no matter which) I get this result and after the first touch the layout stays like this and does not change with further touches (which is also what I would have expected according to my layout, the 2 seekBars on top are 20dip and the 1 below is as high as the thumb):

enter image description here

Like seen in the second picture I would like to have the seekBar from beginning.

Here my layout:

<SeekBar
    android:id="@+id/SbColorPickerHue"
    android:layout_width="match_parent"
    android:layout_height="20dip"
    android:max="360"
    android:thumb="@drawable/colorpickerthumb" />
<SeekBar
    android:id="@+id/SbColorPickerSaturation"
    android:layout_width="match_parent"
    android:layout_height="20dip"
    android:max="255" 
    android:thumb="@drawable/colorpickerthumb"/>
<SeekBar
    android:id="@+id/SbColorPickerValue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="255" 
    android:thumb="@drawable/colorpickerthumb"/>

With a OnSeekBarChangeListener in onProgressChanged() and of course in onCreate (to initalize the seekBars) I am calling the following function (To safe you a lot of code only the Hue Gradient is shown):

    private void setGradients() {
        mHueGradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,mColors);
        mHueGradient.setGradientType(GradientDrawable.LINEAR_GRADIENT);
        mHueGradient.setCornerRadius(mRadius);
        mHueGradient.setStroke(mStrokeWidth, mStrokeColor);
        mSbHue.setBackgroundDrawable(mHueGradient);
    }

I tried many different things already:

  • set the Gradients twice in onCreate
  • set the Gradients again in onRescume
  • as can be seen in the layout xml I set some of the SeekBar to a fixed height (20dp) and others to wrap_content. The behaviour changed a little (as can be seen in the pictures). But still not satisfying.
  • Invalidate either the seekBar or the Drawable
  • Force Layout
  • Measure the drawable

Result is always the same.

What am I doing wrong here? Am I missing somthing?

Edit 1:

The SeekBar and the ChangeListener are defined as follows:

    mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
    setGradients();
    mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        public void onStopTrackingTouch(SeekBar seekBar) {
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            setGradients();
        }
    });

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

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

发布评论

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

评论(1

不念旧人 2025-01-03 11:27:56

如果将 setGradients() 移到侦听器之后,它就会起作用。

mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        setGradients();
    }
});
setGradients();

If you move setGradients() after the listener it works.

mSbHue = (SeekBar)findViewById(R.id.SbColorPickerHue);
mSbHue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

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