Seekbar for two values [-50, 0, 50]

前端 未结 3 1340
自闭症患者
自闭症患者 2020-12-01 17:24

I need to place \"start point\" in center of seek bar.
How can i do like this?

\"seek

3条回答
  •  离开以前
    2020-12-01 17:54

    customSeekBar

    As Above all said, it can be achieved my implementing our own custom seekbar.

    I tried in the following way and it worked out for me.

    activity_main.xml

     
    
     
    
    
    

    MainActivity.java

    package com.example.customseekbar;
    
    import com.example.suricustomseekbar.R;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.SeekBar;
    
    public class MainActivity extends Activity {
    
    SeekBar mseekBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mseekBar = (SeekBar) findViewById(R.id.customSeekBar);
        mseekBar.setProgress(50);
         }
      }
    

    CustomSeekBar.java

    package com.example.customseekbar;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.widget.SeekBar;
    
      public class CustomSeekBar extends SeekBar {
    
    private Rect rect;
    private Paint paint ;
    private int seekbar_height; 
    
    public CustomSeekBar(Context context) {
        super(context);
    
    }
    
    public CustomSeekBar(Context context, AttributeSet attrs) {
    
        super(context, attrs);
        rect = new Rect();
        paint = new Paint();
        seekbar_height = 6;
    }
    
    public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected synchronized void onDraw(Canvas canvas) {
    
        rect.set(0 + getThumbOffset(), 
                (getHeight() / 2) - (seekbar_height/2), 
                getWidth()- getThumbOffset(), 
                (getHeight() / 2) + (seekbar_height/2));
    
        paint.setColor(Color.GRAY);
    
        canvas.drawRect(rect, paint);
    
    
    
        if (this.getProgress() > 50) {
    
    
            rect.set(getWidth() / 2,
                    (getHeight() / 2) - (seekbar_height/2), 
                    getWidth() / 2 + (getWidth() / 100) * (getProgress() - 50),
                    getHeight() / 2 + (seekbar_height/2));
    
            paint.setColor(Color.CYAN);
            canvas.drawRect(rect, paint);
    
        }
    
        if (this.getProgress() < 50) {
    
            rect.set(getWidth() / 2 - ((getWidth() / 100) * (50 - getProgress())),
                    (getHeight() / 2) - (seekbar_height/2),
                     getWidth() / 2,
                     getHeight() / 2 + (seekbar_height/2));
    
            paint.setColor(Color.CYAN);
            canvas.drawRect(rect, paint);
    
        }
    
        super.onDraw(canvas);
        }
    }
    

提交回复
热议问题