7

我正在使用以下类创建一个垂直搜索栏

public class VerticalSeekBar extends SeekBar {

    public VerticalSeekBar(Context context) {
        super(context);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec,
            int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);
        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            setProgress(getMax()
                    - (int) (getMax() * event.getY() / getHeight()));
            onSizeChanged(getWidth(), getHeight(), 0, 0);
            break;

        case MotionEvent.ACTION_CANCEL:
            break;
        }
        return true;
    }
}

然后使用在活动中创建搜索栏

VerticalSeekBar myZoomBar = new VerticalSeekBar(this);
Drawable drawable = getResources().getDrawable(R.drawable.green_bar);
ClipDrawable clip = new ClipDrawable(drawable, Gravity.LEFT,ClipDrawable.HORIZONTAL);
Drawable drawable2 = getResources().getDrawable(R.drawable.white_bar);
InsetDrawable d1 = new InsetDrawable(drawable2, 5, 5, 5, 5);
myZoomBar.setThumb(getResources().getDrawable(R.drawable.whitecircle));
LayerDrawable mylayer = new LayerDrawable(new Drawable[] { d1, clip });
myZoomBar.setProgressDrawable(mylayer);
myZoomBar.setMax(100);
myZoomBar.setProgress(50);
LinearLayout.LayoutParams zoomBarParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
zoomBarParams.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout zoomLayout = new LinearLayout(this);
zoomLayout.addView(myZoomBar, zoomBarParams);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,Gravity.CENTER);
addContentView(zoomLayout, frameLayoutParams);

问题是如何从代码中设置拇指按下并聚焦可绘制?

4

2 回答 2

4

添加OnTouchListenertomyZoomBar应该可以解决您的问题。像这样设置它的行为:

myZoomBar.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch(event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            // Pressed state
            myZoomBar.setThumb(getResources().getDrawable(
                          R.drawable.pressed_state));
            break;

        case MotionEvent.ACTION_UP:
            myZoomBar.setThumb(getResources().getDrawable(
                          R.drawable.));
            break;
        }

        return false;
    }
});

您可以使用 aOnFocusChangeListener来处理焦点的更改,但如果您只需要在按下 SeekBar 上的某个位置时更改拇指,则 OnTouchListener 就足够了。

在这种情况下,状态选择器可绘制对象可能无法正常工作。因为,用户可能希望通过单击(而不是在那里滑动)来跳转到某个位置。而且我不认为拇指可以处理状态变化。我尝试使用StateListDrawable创建带有状态的可绘制对象,但使用它myZoomBar.setThumb(stateDrawable)不起作用。

于 2013-09-07T09:59:28.897 回答
1

从资源或以编程方式创建 Drawable 并在完成后使用

setThumb(your_drawable);

我不知道是否有可能以编程方式创建带有状态(聚焦和选择)的可绘制对象,但来自 xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/state_focused" android:state_focused="true">
  <item android:drawable="@drawable/state_selected" android:state_selected="true">
  <item android:drawable="@drawable/state_normal"></item>
</item></selector>

getResources().getDrawable(R.drawable.your_drawable_id);
于 2013-09-06T13:49:40.290 回答