Sun*_*hra 7 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用在活动中创建搜索栏
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);
Run Code Online (Sandbox Code Playgroud)
问题是如何从代码中设置拇指按压和聚焦可绘制?
添加OnTouchListener
tomyZoomBar
应该可以解决您的问题。像这样设置它的行为:
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;
}
});
Run Code Online (Sandbox Code Playgroud)
您可以使用 aOnFocusChangeListener
来处理焦点的变化,但如果您只需要在按下 SeekBar 上的某个位置时更改拇指,则 OnTouchListener 就足够了。
在这种情况下,可绘制的状态选择器可能无法正常工作。因为,用户可能想通过单击某个位置(而不是滑动到那里)来跳转到该位置。而且我认为拇指无法处理状态变化。我尝试使用StateListDrawable
它创建一个带有状态的可绘制对象,但是使用它myZoomBar.setThumb(stateDrawable)
不起作用。