如何使Android搜索栏平滑移动?

vptzau2j  于 2023-03-21  发布在  Android
关注(0)|答案(6)|浏览(197)

我试图使一个SeekBar移动更顺利,而不是直接跳到位置。我这样做是因为我有一个滑块类型的实现有3个选项的SeekBar。
我期待着创建一个很好的3个选项平滑滑块“是”,“没有选项”,“没有”在我的应用程序和SeekBar功能看起来最好的和最简单的方法。
我试过看Android动画,它似乎有点复杂,所以如果有人能提供任何帮助,这将是很好的:)

xmd2e60i

xmd2e60i1#

我发现了下面的解决方案,可以使seekbar平滑移动,但仍然捕捉到有限的值范围。假设您的布局中有以下视图:

<SeekBar
  android:id="@+id/sldProgress"
  style="@style/MySliderStyle"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:max="100"
  android:progress="50" />

<TextView
  android:id="@+id/lblProgress"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="5" />

您可以在Activity中使用以下代码(根据您的需要更改上面xml中android:max的值,以及下面代码中的平滑因子-值越高=越平滑):

public class MyActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    sliderListener sldListener = new sliderListener();
    SeekBar sldProgress = (SeekBar) findViewById(R.id.sldProgress);
    sldProgress.setOnSeekBarChangeListener(sldListener);
  }
  private class sliderListener implements OnSeekBarChangeListener {
    private int smoothnessFactor = 10;
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
      progress = Math.round(progress / smoothnessFactor);
      TextView lblProgress = (TextView) findViewById(R.id.lblProgress);
      lblProgress.setText(String.valueOf(progress));
    }

    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    public void onStopTrackingTouch(SeekBar seekBar) {
      seekBar.setProgress(Math.round((seekBar.getProgress() + (smoothnessFactor / 2)) / smoothnessFactor) * smoothnessFactor);
    }
  }
}
hjzp0vay

hjzp0vay2#

你不需要自己做任何事情,只需要用户的android动画,它会为你做的事情
用Kotlin写的代码

mySeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
            //Todo: code
        }

        override fun onStartTrackingTouch(seekBar: SeekBar?) {
        }

        override fun onStopTrackingTouch(seekBar: SeekBar) {
            if (seekBar.progress > 70) {
                ObjectAnimator.ofInt(seekBar, "progress", 100).setDuration(100).start()
            } else if (seekBar.progress < 30) {
                ObjectAnimator.ofInt(seekBar, "progress", 0).setDuration(100).start()
            } else {
                ObjectAnimator.ofInt(seekBar, "progress", 50).setDuration(100).start()
            }
        }
    })
uurity8g

uurity8g3#

建立了我自己的控制。工作完成:)代码here

vqlkdk9b

vqlkdk9b4#

创建自定义SeekBar

public class SmoothSeekBar extends AppCompatSeekBar implements SeekBar.OnSeekBarChangeListener {

private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;

private boolean mNeedCallListener = true;

private ValueAnimator mAnimator;

public SmoothSeekBar(Context context) {
    super(context);
    init(context);
}

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

public SmoothSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}

public void init(Context context) {
    Context mContext = context;
}

@Override
public void setOnSeekBarChangeListener(
        SeekBar.OnSeekBarChangeListener onSeekBarChangeListener) {
    mOnSeekBarChangeListener = onSeekBarChangeListener;
    super.setOnSeekBarChangeListener(this);
}

@Override
public void setProgress(final int progress) {
    final int currentProgress = getProgress();
    if (mAnimator != null) {
        mAnimator.cancel();
        mAnimator.removeAllUpdateListeners();
        mAnimator.removeAllListeners();
        mAnimator = null;
        mNeedCallListener = true;
    }
    mAnimator = ValueAnimator.ofInt(currentProgress, progress);
    mAnimator.setDuration(getResources().getInteger(android.R.integer.config_longAnimTime));
    mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            int value = (int) animation.getAnimatedValue();
            if (value == progress) {
                mNeedCallListener = true;
            } else {
                mNeedCallListener = false;
            }
            Logger.e("ProgressBar value ", value + "");
            SmoothSeekBar.super.setProgress(value);
        }
    });
    mAnimator.start();

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if (fromUser || mNeedCallListener) {
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
        }
    }
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    if (mOnSeekBarChangeListener != null) {
        mOnSeekBarChangeListener.onStartTrackingTouch(seekBar);
    }
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    if (mOnSeekBarChangeListener != null) {
        mOnSeekBarChangeListener.onStopTrackingTouch(seekBar);
    }
}
}

现在添加XML格式的SeekBar并开始享受

<com.yourPackage.SmoothSeekBar
    android:id="@+id/seekBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
piztneat

piztneat5#

这对我来说很好用,相当简单和直接。

if (currentPosition > 0) {
    ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, "progress", currentPosition);
    animation.setDuration(400);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.start();
}
jhdbpxl9

jhdbpxl96#

我为平滑查找栏动画制作了一个自定义视图库。
试试看!
https://github.com/WoochanLee/SmoothSeekBar

  • settings.gradle
dependencyResolutionManagement {
    ...
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}
  • build.gradle
dependencies {
    ...
    implementation 'com.github.WoochanLee:SmoothSeekBar:1.0.0'
}
  • 在您XML中
<com.woody.lee.library.smoothseekbar.SmoothSeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

相关问题