Android使用SeekBar时动态显示进度且随SeekBar一起移动

Android使用SeekBar时动态显示进度且随SeekBar一起移动

2023年7月15日发(作者:)

Android使⽤SeekBar时动态显⽰进度且随SeekBar⼀起移动好久没有更新博客了!在滑动SeekBar的时候,要同时显⽰进度,⽽且通常要求最⼩值⽐0⼩,按照惯例,先看看效果图:

⼀、⾃定义控件 ⽤于在SeekBar上⽅,跟随SeekBar⼀块移动的控件显⽰进度package r;import t;import uteSet;import oup;/** * @author: lijuan * @description: * @date: 2017-05-31 * @time: 17:49 */public class CustomLayout extends ViewGroup { public CustomLayout(Context context) { super(context); } public CustomLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { }}⼆、MainActivity,代码很简单!稍微看下就懂的了~package r;import ty;import ;import ;import ;import oup;import r;import ew;public class MainActivity extends Activity { private SeekBar seekbar = null; private int startValue = 0; private int endValue = 100; private int totalValue; private TextView text, mTvStartValue, mTvEndValue; private int screenWidth;//屏幕宽度 private CustomLayout textMoveLayout;//⾃定义随着拖动条⼀起移动的空间 private float moveStep = 0;//托动条的移动步调 private Params layoutParams; @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main); screenWidth = getWindowManager().getDefaultDisplay().getWidth(); text = new TextView(this); kgroundColor((245, 245, 245)); tColor((0, 161, 229)); tSize(16); layoutParams = new Params(screenWidth, 50); textMoveLayout = (CustomLayout) findViewById(yout); w(text, layoutParams); (0, 20, screenWidth, 80); seekbar = (SeekBar) findViewById(r); mTvStartValue = (TextView) findViewById(_value); mTvEndValue = (TextView) findViewById(_value); eekBarChangeListener(new OnSeekBarChangeListener()); init(); } public void init() { t(f(startValue)); t(f(endValue)); t(f(startValue)); bled(true); ((endValue)); gress(startValue); if (startValue < 0 && endValue < 0) { totalValue = (startValue) - (endValue); } else if (startValue < 0 && endValue > -1) { totalValue = endValue + (startValue); } else { totalValue = endValue - startValue; } moveStep = (float) (((float) screenWidth / totalValue) * 0.8); } private class OnSeekBarChangeListener implements BarChangeListener { // 触发操作,拖动 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { ((int) (progress * moveStep), 20, screenWidth, 80); t(check(progress)); } // 表⽰进度条刚开始拖动,开始拖动时候触发的操作 public void onStartTrackingTouch(SeekBar seekBar) { } // 停⽌拖动时候 public void onStopTrackingTouch(SeekBar seekBar) { } } private String check(int progress) { int curValue = totalValue * progress/(endValue); if (startValue <0 && endValue < 0) { curValue = startValue + curValue; } else if (startValue < 0 && endValue > -1) { curValue = curValue + startValue; } Log.e("check", totalValue+","+curValue+","+ ()); return f(curValue); }}三、po_ 四、seekbar_ 五、activity_ 好了,本篇⽂章就这样啦,存在总结不到位的地⽅还望指导,感谢^_^

发布者:admin,转转请注明出处:http://www.yc00.com/web/1689430525a247014.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信