更改 Android 上自定义垂直搜索栏进度颜色的方法,不适用于线程

发布于 2024-12-08 17:27:33 字数 1113 浏览 2 评论 0原文

我正在尝试更改自定义垂直 SeekBar 的进度颜色 如何获得工作Android 中的垂直 SeekBar? 如果我将此代码放入 View.OnTouchListener 中,它会正常工作:

VerticalSeekBar verticalSeekBar1 = (VerticalSeekBar)verticalSeekBar;
Rect bounds = verticalSeekBar1.getProgressDrawable().getBounds();
int progr = verticalSeekBar1.getProgress();
int max = verticalSeekBar1.getMax();
int width = verticalSeekBar1.getWidth(); 
int height = verticalSeekBar1.getHeight();
prDr = verticalSeekBar1.getProgressDrawable();
verticalSeekBar1.setProgressDrawable(r.getDrawable(R.drawable.myprogress));
verticalSeekBar1.getProgressDrawable().setBounds(bounds);
verticalSeekBar1.setMax(max);
verticalSeekBar1.setProgress(1);
verticalSeekBar1.setProgress(progr);
verticalSeekBar1.onSizeChanged(width, height);
verticalSeekBar1.refreshDrawableState();

但是,如果我将相同的代码放入另一个线程中,它会部分工作,因为搜索栏的拇指没有到达正确的位置,而是向下移动,但进度的颜色效果很好并到达最后一个位置。 我认为 onSizeChanged 方法可能是导致此问题的原因。我对其进行了调试,发现在这两种情况下, onSizeChanged 方法都有适当的参数,但在线程的代码中,它不会更新搜索栏。

我已经尝试将此代码移至 UI 线程,但也不起作用。

I'm trying to change the progress color of a custom vertical SeekBar How can I get a working vertical SeekBar in Android? and it works fine if I put this code in a View.OnTouchListener:

VerticalSeekBar verticalSeekBar1 = (VerticalSeekBar)verticalSeekBar;
Rect bounds = verticalSeekBar1.getProgressDrawable().getBounds();
int progr = verticalSeekBar1.getProgress();
int max = verticalSeekBar1.getMax();
int width = verticalSeekBar1.getWidth(); 
int height = verticalSeekBar1.getHeight();
prDr = verticalSeekBar1.getProgressDrawable();
verticalSeekBar1.setProgressDrawable(r.getDrawable(R.drawable.myprogress));
verticalSeekBar1.getProgressDrawable().setBounds(bounds);
verticalSeekBar1.setMax(max);
verticalSeekBar1.setProgress(1);
verticalSeekBar1.setProgress(progr);
verticalSeekBar1.onSizeChanged(width, height);
verticalSeekBar1.refreshDrawableState();

However, if I put this same code in another thread it works partially since the thumb of the seekbar does not go to the right position and instead goes down but the color of the progress works well and gets to the last position.
I figured that the method onSizeChanged is probably causing this. I debugged it and found out that in both scenarios the onSizeChanged method has the appropriate arguments but in the code from the thread it does not update the seekbar.

I already tried moving this code to UI's thread but does not work either.

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

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

发布评论

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

评论(2

雪化雨蝶 2024-12-15 17:27:33
    LinearLayout linearLayout = new LinearLayout(getApplicationContext());

    LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.FILL_PARENT);

    // linearLayout.setOrientation(LinearLayout.VERTICAL);

    VerticalSeekBar_1 verticalSeekBar_1 = new VerticalSeekBar_1(
            getApplicationContext());

    // ------------ custom seekbar
     LayerDrawable layer = (LayerDrawable) verticalSeekBar_1
     .getProgressDrawable();

     Drawable draw1 = (Drawable) layer
     .findDrawableByLayerId(android.R.id.progress);

     Bitmap bitmap1 = BitmapFactory.decodeResource(getApplicationContext()
     .getResources(), R.drawable.scroll_on);

     draw1 = new ClipDrawable(new BitmapDrawable(bitmap1),
     Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);

     layer.setDrawableByLayerId(android.R.id.progress, draw1);

     Drawable draw2 = (Drawable) layer
     .findDrawableByLayerId(android.R.id.background);

     Bitmap bitmap2 = BitmapFactory.decodeResource(getApplicationContext()
     .getResources(), R.drawable.scroll_off);

     draw2 = new BitmapDrawable(bitmap2);
     layer.setDrawableByLayerId(android.R.id.background, draw2);

    // --------------



    verticalSeekBar_1.setLayoutParams(rlp);

    verticalSeekBar_1.setMax(100);
    verticalSeekBar_1.setProgress(30);
    verticalSeekBar_1
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }
            });

    linearLayout.addView(verticalSeekBar_1);
    setContentView(linearLayout);
    LinearLayout linearLayout = new LinearLayout(getApplicationContext());

    LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.FILL_PARENT);

    // linearLayout.setOrientation(LinearLayout.VERTICAL);

    VerticalSeekBar_1 verticalSeekBar_1 = new VerticalSeekBar_1(
            getApplicationContext());

    // ------------ custom seekbar
     LayerDrawable layer = (LayerDrawable) verticalSeekBar_1
     .getProgressDrawable();

     Drawable draw1 = (Drawable) layer
     .findDrawableByLayerId(android.R.id.progress);

     Bitmap bitmap1 = BitmapFactory.decodeResource(getApplicationContext()
     .getResources(), R.drawable.scroll_on);

     draw1 = new ClipDrawable(new BitmapDrawable(bitmap1),
     Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);

     layer.setDrawableByLayerId(android.R.id.progress, draw1);

     Drawable draw2 = (Drawable) layer
     .findDrawableByLayerId(android.R.id.background);

     Bitmap bitmap2 = BitmapFactory.decodeResource(getApplicationContext()
     .getResources(), R.drawable.scroll_off);

     draw2 = new BitmapDrawable(bitmap2);
     layer.setDrawableByLayerId(android.R.id.background, draw2);

    // --------------



    verticalSeekBar_1.setLayoutParams(rlp);

    verticalSeekBar_1.setMax(100);
    verticalSeekBar_1.setProgress(30);
    verticalSeekBar_1
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }
            });

    linearLayout.addView(verticalSeekBar_1);
    setContentView(linearLayout);
甜是你 2024-12-15 17:27:33

我读到使用 Handler 或 AsyncTask 可以完成更新 UI 的工作,这确实达到了目的。

uiHandler.post(new Runnable() {
                VerticalSeekBar vs;
                Rect bounds;
                int progr;
                int max;
                int width;
                int height;
                 {
                    vs = verticalSeekBars.get(currentNote);
                    bounds = vs.getProgressDrawable().getBounds();
                    progr = vs.getProgress();
                    max = vs.getMax();
                    width = vs.getWidth(); 
                    height = vs.getHeight();
                }

                public void run() {
                    if(currentBytes != 0) {
                        VerticalSeekBar vsBack = verticalSeekBars.get(currentNote-1);
                        Rect bounds = vsBack.getProgressDrawable().getBounds();
                        int progr = vsBack.getProgress();
                        int max = vsBack.getMax();
                        int width = vsBack.getWidth(); 
                        int height = vsBack.getHeight();
                        vsBack.setProgressDrawable(prDrInactive);
                        vsBack.getProgressDrawable().setBounds(bounds);
                        vsBack.setMax(max);
                        vsBack.setProgress(1);
                        vsBack.setProgress(progr);
                        vsBack.onSizeChanged(width, height);
                        vsBack.refreshDrawableState();
                    }

绘画笔记。

                    prDrInactive = vs.getProgressDrawable();

                    vs.setProgressDrawable(prDrActive);
                    vs.getProgressDrawable().setBounds(bounds);
                    vs.setMax(max);
                    vs.setProgress(1);
                    vs.setProgress(progr);
                    vs.onSizeChanged(width, height);
                    vs.refreshDrawableState();
                }
            });

I read that using Handler or AsyncTask can do the work for updating UI and that did the trick.

uiHandler.post(new Runnable() {
                VerticalSeekBar vs;
                Rect bounds;
                int progr;
                int max;
                int width;
                int height;
                 {
                    vs = verticalSeekBars.get(currentNote);
                    bounds = vs.getProgressDrawable().getBounds();
                    progr = vs.getProgress();
                    max = vs.getMax();
                    width = vs.getWidth(); 
                    height = vs.getHeight();
                }

                public void run() {
                    if(currentBytes != 0) {
                        VerticalSeekBar vsBack = verticalSeekBars.get(currentNote-1);
                        Rect bounds = vsBack.getProgressDrawable().getBounds();
                        int progr = vsBack.getProgress();
                        int max = vsBack.getMax();
                        int width = vsBack.getWidth(); 
                        int height = vsBack.getHeight();
                        vsBack.setProgressDrawable(prDrInactive);
                        vsBack.getProgressDrawable().setBounds(bounds);
                        vsBack.setMax(max);
                        vsBack.setProgress(1);
                        vsBack.setProgress(progr);
                        vsBack.onSizeChanged(width, height);
                        vsBack.refreshDrawableState();
                    }

painting note.

                    prDrInactive = vs.getProgressDrawable();

                    vs.setProgressDrawable(prDrActive);
                    vs.getProgressDrawable().setBounds(bounds);
                    vs.setMax(max);
                    vs.setProgress(1);
                    vs.setProgress(progr);
                    vs.onSizeChanged(width, height);
                    vs.refreshDrawableState();
                }
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文