How to make a color gradient in a SeekBar?

后端 未结 2 1726
遥遥无期
遥遥无期 2020-12-15 15:03

I want to use a SeekBar (i.e. old school Java Slider) into a color gradient picker. I have seen some examples like this but they all require making new classes and such. T

相关标签:
2条回答
  • 2020-12-15 15:26

    I figured it out here is how you do it.

    You create a standard seekbar in your XML.

    <SeekBar
           android:id="@+id/seekbar_font"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_margin="10px"
           android:layout_below="@id/color_font_text"
           android:max="100"
           android:progress="50"></SeekBar>
    

    Then you customize the seekbar in your onCreate() by creating a boxShape and then force a LinearGradient inside it.

    LinearGradient test = new LinearGradient(0.f, 0.f, 300.f, 0.0f,  
    
          new int[] { 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF,
          0xFFFF0000, 0xFFFF00FF, 0xFFFFFF00, 0xFFFFFFFF}, 
          null, TileMode.CLAMP);
    ShapeDrawable shape = new ShapeDrawable(new RectShape());
    shape.getPaint().setShader(test);
    
    SeekBar seekBarFont = (SeekBar)findViewById(R.id.seekbar_font);
    seekBarFont.setProgressDrawable( (Drawable)shape );
    

    Here is an image of the current code up above SeekBar Color Gradient

    0 讨论(0)
  • 2020-12-15 15:32

    This is in addition to the solution provided using LinearGradient. Try this logic for translating the progress to rgb:

    lineColorSeekbar.setMax(256*7-1);
            lineColorSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    if(fromUser){
                        int r = 0;
                        int g = 0;
                        int b = 0;
    
                        if(progress < 256){
                            b = progress;
                        } else if(progress < 256*2) {
                            g = progress%256;
                            b = 256 - progress%256;
                        } else if(progress < 256*3) {
                            g = 255;
                            b = progress%256;
                        } else if(progress < 256*4) {
                            r = progress%256;
                            g = 256 - progress%256;
                            b = 256 - progress%256;
                        } else if(progress < 256*5) {
                            r = 255;
                            g = 0;
                            b = progress%256;
                        } else if(progress < 256*6) {
                            r = 255;
                            g = progress%256;
                            b = 256 - progress%256;
                        } else if(progress < 256*7) {
                            r = 255;
                            g = 255;
                            b = progress%256;
                        }
    
                        lineColorSeekbar.setBackgroundColor(Color.argb(255, r, g, b));
                    }
                }
    
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
    
                }
    
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
    
                }
            });
    
    0 讨论(0)
提交回复
热议问题