gpt4 book ai didi

java - VerticalSeekbar 在水平 ScrollView 中工作不流畅

转载 作者:行者123 更新时间:2023-12-01 14:35:03 25 4
gpt4 key购买 nike

我正在尝试在水平 ScrollView 中实现垂直搜索栏,
但是搜索栏在其中运行不流畅。

这是我的垂直 sekkbar 类(class)......仅来自 stackoverflow 的来源。

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

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

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

这就是我实现 seekbar 的方式。

    seekbar3.setMax(5000);
seekbar3.setProgress(mEqualizer.getBandLevel(band));
seekbar3.setFocusable(false);
seekbar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
horizontalscroll.setOnTouchListener(null);
horizontalscroll.setEnabled(false);
mHandler.removeCallbacks(mUpdateTimeTask);
seekbar3.setProgress(mEqualizer.getBandLevel(band));

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//horizontalscroll.setEnabled(false);
/*horizontalscroll.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});*/
ObjectAnimator animator=ObjectAnimator.ofInt(horizontalscroll, "scrollX",8,8 );
animator.setDuration(2000);
animator.start();
// seekbar3.setProgress(mEqualizer.getBandLevel(band));
mHandler.removeCallbacks(mUpdateTimeTask);


// Enable Scrolling by removing the OnTouchListner
// horizontalscroll.setOnTouchListener(null);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mEqualizer.setBandLevel(band, (short)(progress+min));
seekbar3top.setText((progress+min)+"mB");
seekbar3.setProgress(progress);
// seekbar3.onDraw(c)
/* horizontalscroll.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}});*/
}
});

}

任何人都可以建议我如何以简单的方式去做。我的 verticalseekbar 没有像它应该移动的那样平滑移动。提前致谢:)

最佳答案

终于解决了这个问题我将我的垂直搜索栏类更改为此

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;

public class VerticalSeekBar extends SeekBar {
private boolean mIsMovingThumb = false;
static private float THUMB_SLOP = 25;
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);
}

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

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

super.onDraw(c);
}
private boolean isWithinThumb(MotionEvent event) {
final float progress = getProgress();
final float density = this.getResources().getDisplayMetrics().density;
final float height = getHeight();
final float y = event.getY();
final float max = getMax();
if (progress >= max - (int)(max * (y + THUMB_SLOP * density) / height)
&& progress <= max - (int)(max * (y - THUMB_SLOP * density) / height))
return true;
else
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
boolean handled=false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isWithinThumb(event)) {
getParent().requestDisallowInterceptTouchEvent(true);
mIsMovingThumb = true;
handled = true;
}
break;
case MotionEvent.ACTION_MOVE:

case MotionEvent.ACTION_UP:
setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
if (mIsMovingThumb) {
setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
handled = true;
}
break;
case MotionEvent.ACTION_CANCEL:
getParent().requestDisallowInterceptTouchEvent(false);
mIsMovingThumb = false;
handled = true;
break;

}
return true;
}
}

关于java - VerticalSeekbar 在水平 ScrollView 中工作不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32745522/

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