gpt4 book ai didi

android - 以编程方式在自定义搜索栏上设置拇指焦点和按下可绘制对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:13 27 4
gpt4 key购买 nike

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

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

然后在 Activity 中使用创建搜索栏

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

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

最佳答案

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 就足够了。

State selector drawable 在这种情况下可能无法正常工作。因为,用户可能想通过点击跳到某个位置(而不是滑动到那里)。而且我认为拇指不能处理状态变化。我尝试使用 StateListDrawable 创建一个带有状态的可绘制对象,但是将它与 myZoomBar.setThumb(stateDrawable) 一起使用没有效果。

关于android - 以编程方式在自定义搜索栏上设置拇指焦点和按下可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18572214/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com