设置拇指焦点&以编程方式在自定义搜索栏上按下drawable

时间:2013-09-02 11:30:46

标签: android android-layout

我正在使用以下类创建垂直搜索条

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);

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

2 个答案:

答案 0 :(得分:4)

OnTouchListener添加到myZoomBar可以解决您的问题。像这样设置它的行为:

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;
    }
});

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

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

答案 1 :(得分: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);