android TextView文字跟随seekBar滑动条滑块的位置移动
来源:互联网 发布:朗读软件手机版 编辑:程序博客网 时间:2024/06/05 23:03
android有一个很有意思的控件:seekBar,它可以实现手动滑动进度条的进度,也可以自动调整滑块的位置,并能实现各种效果,适用于进度条,选择额度等情况,在这里我们就暂时不多说了,今天我们重点来实现一下与seekBar相关联的一种效果:文字跟随滑块的位置移动。
之前做过这种效果,用的原理是文字的中间位置与滑块中间位置相同,实现了文字随滑块滑动,但也有一些瑕疵:当滑动条宽度为占满屏幕且文字长度大于滑块的宽度时,如果滑块滑到最左边或最右边,文字显示不完全,思来想去,终于让我找到一种精确计算位置而且不会因为滑动条宽度造成文字显示不完全的问题,它的实现原理是:当滑块位于最左端时,文字与滑块左对齐,当滑动到右端时,文字与滑块右对齐,并随着滑块的移动,文字按照滑块的位置和文字长度按比例调整位置,实现平滑滚动。
1、布局文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/screen_bg" android:orientation="vertical"> <TextView android:id="@+id/tv_quota" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FF233845" android:textSize="15sp"/> <SeekBar android:id="@+id/sb_quota" style="@style/mprogress_horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:thumb="@mipmap/bulegress_button" /></LinearLayout>
2、java代码
private SeekBar sb_quota; private TextView tv_quota; tv_quota = (TextView) rootView.findViewById(R.id.tv_quota); sb_quota = (SeekBar) rootView.findViewById(R.id.sb_quota); sb_quota.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { tv_quota.setText("¥" + progress); quota = progress; int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); tv_quota.measure(spec, spec); int quotaWidth = tv_quota.getMeasuredWidth(); int spec2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); tv_quota.measure(spec2, spec2); int sbWidth = sb_quota.getMeasuredWidth(); LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv_quota.getLayoutParams(); params.leftMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax()); tv_quota.setLayoutParams(params); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } });
阅读全文
4 0
- android TextView文字跟随seekBar滑动条滑块的位置移动
- android开发之自定义Seekbar滑动条,文字跟随滑动按钮一起滑动
- android按钮跟随手势滑动改变位置
- android控件跟随手势滑动改变位置
- Android自定义Seekbar滑动条,Pop提示跟随滑动按钮一起滑动
- 跟随进度条进度移动的文字提示
- Android TextView的滑动
- textview随seekbar一起移动
- JavaScript实现跟随鼠标移动而移动的文字
- android引导页下方圆点位置跟随手指移动的实现
- android Seekbar改变thumb图片的位置
- android Seekbar双滑块滑动
- 【Android】SeekBar禁止滑动
- View滑动-跟随手指移动
- android 实现跟随手机滑动的小球
- Android 跟随手指滑动的控件
- 个性文字跟随鼠标移动
- Javascript_ex_文字跟随鼠标移动
- Tomcat 服务器搭建
- Selenide智能等待
- socket 知识点
- 剑指Offer-39
- 目前为止最全的微信小程序项目实例
- android TextView文字跟随seekBar滑动条滑块的位置移动
- ProgressCtrlST位图进度条控件类
- 动态内存与智能指针及其实现
- Android 代码混淆、过滤与警告处理
- Python基础-@property
- interlij idea创建给Web项目添加SS2H
- C#连接SQL Server并查询数据
- 【LeetCode】 Majority Element 系列
- Qt5.8 图像铺满