Android笔记之文本随滑块移动的SeekBar
效果图

FloatingTextSeekBar.java
package com.bu_ish.blog; import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar; import java.lang.reflect.Field; public class FloatingTextSeekBar extends LinearLayout {
private SeekBar sb;
private FloatingTextView ftv;
private String startText = "100";
private int max = 49;
private float floatingTextX;
private static final String TAG = FloatingTextSeekBar.class.getName(); public FloatingTextSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
sb = new SeekBar(context);
LayoutParams sbLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
sb.setLayoutParams(sbLayoutParams);
sb.setPadding(16, 0, 16, 0);
try {
Class cls = sb.getClass().getSuperclass().getSuperclass();
Field field = cls.getDeclaredField("mMaxHeight");
field.setAccessible(true);
field.set(sb, PixelTool.dpToPx(context, 5));
} catch (NoSuchFieldException | IllegalAccessException ex) {
Log.e(TAG, null, ex);
}
sb.setMax(max);
sb.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar_progress_drawable));
sb.setThumb(getResources().getDrawable(R.mipmap.ic_seek_bar_thumb));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
sb.setSplitTrack(false);
addView(sb);
ftv = new FloatingTextView(context);
LayoutParams ftvLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, PixelTool.dpToPx(context, 12));
ftvLayoutParams.topMargin = PixelTool.dpToPx(context, 2);
ftv.setLayoutParams(ftvLayoutParams);
ftv.setText(startText);
addView(ftv);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float step = seekBar.getWidth() / (float) max;
floatingTextX = progress * step;
ftv.setText((progress + Integer.parseInt(startText) / 100) * 100 + "");
} @Override
public void onStartTrackingTouch(SeekBar seekBar) {
} @Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
} public void setStartText(String startText) {
this.startText = startText;
} public void setMax(int max) {
this.max = max;
sb.setMax(max);
} private class FloatingTextView extends View {
private String text; private FloatingTextView(Context context) {
super(context);
} private void setText(String text) {
this.text = text;
invalidate();
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#ff5158e7"));
float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
paint.setTextSize(textSize);
paint.setTextAlign(Paint.Align.CENTER);
float textWidth = paint.measureText(text);
if (floatingTextX - textWidth / 2 < getLeft()) {
floatingTextX += textWidth / 2;
}
if (floatingTextX + textWidth / 2 > getRight()) {
floatingTextX -= textWidth / 2;
}
canvas.drawText(text, floatingTextX, getHeight(), paint);
}
}
}
seek_bar_progress_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<solid android:color="#ffebebeb" />
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<solid android:color="#ff5158e7" />
<corners
android:bottomLeftRadius="3dp"
android:bottomRightRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp" />
</shape>
</clip>
</item>
</layer-list>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="请选择到账金额(元)"
android:textColor="#ff1b1b1b"
android:textSize="14sp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"> <TextView
android:id="@+id/tvBorrowableMin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginRight="5dp"
android:textColor="#ff999999"
android:textSize="12sp"
tools:text="100" /> <com.bu_ish.blog.FloatingTextSeekBar
android:id="@+id/ftsb"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp" /> <TextView
android:id="@+id/tvBorrowableMax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="48dp"
android:textColor="#ff999999"
android:textSize="12sp"
tools:text="5000" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.bu_ish.blog; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
private TextView tvBorrowableMin, tvBorrowableMax;
private FloatingTextSeekBar ftsb;
private int borrowableMin = 500, borrowableMax = 10000; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvBorrowableMin = findViewById(R.id.tvBorrowableMin);
tvBorrowableMax = findViewById(R.id.tvBorrowableMax);
ftsb = findViewById(R.id.ftsb);
tvBorrowableMin.setText(borrowableMin + "");
tvBorrowableMax.setText(borrowableMax + "");
ftsb.setStartText(borrowableMin + "");
ftsb.setMax((borrowableMax - borrowableMin) / 100);
}
}
完整Demo链接:https://pan.baidu.com/s/1ohRtbYsSJi8105brvj3fkQ,提取码:lwif
P.S.
SeekBar.setProgressDrawable(Drawable),设置进度颜色、进度背景
SeekBar.setThumb(Drawable),设置滑块
Android笔记之文本随滑块移动的SeekBar的更多相关文章
- Android学习笔记(17):文本框TextView类
TextView继承自View.用于显示文本.它有很多的子类,掌握其属性是非常重要的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...
- Android笔记(十三) Android中的基本组件——文本
Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- Android之EditText文本变化的监听
监听EditText的文本变化需要给EditText控件加一个addTextChangeListener监听器 editText.addTextChangeListener(textWatcher); ...
- Android笔记:触摸事件的分析与总结----TouchEvent处理机制
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320 ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- Android笔记-3-EditText的属性介绍
[Android 基础]EditText的属性介绍 EditText继承TextView,所以EditText具有TextView的属性特点,下面主要介绍一些EditText的特有的输入法的属性特点 ...
- android笔记一 控件属性
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android= ...
- Android 笔记之 R 文件
Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...
随机推荐
- 标准C程序设计七---36
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- php set_time_limit(0) 设置程序执行时间的函数
一个简单的例子,在网页里显示1500条语句,如果未设置失效时间,则程序执行到791时结束了,如果把 set_time_limit(0); 前的注释符//去除,则程序直到1才结束. set_time ...
- Mongodb的使用(下)
高级操作 讲解关于mongodb的高级操作,包括聚合.主从复制.分片.备份与恢复.MR 完成python与mongodb的交互 聚合 aggregate 聚合(aggregate)主要用于计算数据,类 ...
- vSphere Client 更改 ESX/ESXi 主机的端口
https://blog.csdn.net/hanzheng260561728/article/details/51283808?locationNum=8&fps=1
- HDU6214 Smallest Minimum Cut
题目链接:HDU6214 留一个链式前向星+Dinic模板(希望不要被某人发现,嘿嘿嘿). #include <cstdio> #include <cstring> #incl ...
- L1-2. 点赞【求多组数据中出现次数最多的】
L1-2. 点赞 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持.每 ...
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-an ...
- springboot快速集成swagger
今天技术总监说:小明,我们本次3.0改造,使用swagger2.0作为前后端分离的接口规范,它可以一键生成前后端的API,一劳永逸--小明:??? Spring Boot 框架是目前非常流行的微服务框 ...
- Java创建和解析Json数据方法(四)——json-lib包的使用
(四)json-lib包的使用 既然json-lib包比org.json包重量级,那么json-lib包肯定有很多org.json包没有的类和方法,这篇笔记简单记录json-lib包中 ...
- screen状态变Attached连接会话失败
使用xshell远程登录主机,使用screen命令启动程序运行至后台,意外发现screen session的状态为Attached,使用命令screen -r <session-id>,提 ...