Detecting thumb position in SeekBar prior to API version 16

后端 未结 4 1997
清酒与你
清酒与你 2021-02-07 11:11

Basically, I need to detect when the progress changes in the SeekBar and draw a text view on top of the thumb indicating the progress value.

I do this by implementing a

4条回答
  •  孤城傲影
    2021-02-07 12:08

    I was able to use my own class to get the Thumb:

    MySeekBar.java

    package mobi.sherif.seekbarthumbposition;
    
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.widget.SeekBar;
    
    public class MySeekBar extends SeekBar {
    
        public MySeekBar(Context context) {
            super(context);
        }
        public MySeekBar(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        public MySeekBar(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        Drawable mThumb;
        @Override
        public void setThumb(Drawable thumb) {
            super.setThumb(thumb);
            mThumb = thumb;
        }
        public Drawable getSeekBarThumb() {
            return mThumb;
        }
    
    }
    

    In the activity this works perfectly:

    package mobi.sherif.seekbarthumbposition;
    
    import android.app.Activity;
    import android.graphics.Rect;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    public class MainActivity extends Activity implements OnSeekBarChangeListener {
        MySeekBar mSeekBar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mSeekBar = (MySeekBar) findViewById(R.id.seekbar);
            mSeekBar.setOnSeekBarChangeListener(this);
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
            Rect thumbRect = mSeekBar.getSeekBarThumb().getBounds();
            Log.v("sherif", "(" + thumbRect.left + ", " + thumbRect.top + ", " + thumbRect.right + ", " + thumbRect.bottom + ")");
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
    
        }
    }
    

提交回复
热议问题