效果图

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的更多相关文章

  1. Android学习笔记(17):文本框TextView类

    TextView继承自View.用于显示文本.它有很多的子类,掌握其属性是非常重要的. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5 ...

  2. Android笔记(十三) Android中的基本组件——文本

    Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种 EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑 ...

  3. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  4. Android之EditText文本变化的监听

    监听EditText的文本变化需要给EditText控件加一个addTextChangeListener监听器 editText.addTextChangeListener(textWatcher); ...

  5. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  6. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  7. Android笔记-3-EditText的属性介绍

    [Android 基础]EditText的属性介绍 EditText继承TextView,所以EditText具有TextView的属性特点,下面主要介绍一些EditText的特有的输入法的属性特点 ...

  8. android笔记一 控件属性

    <?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android= ...

  9. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

随机推荐

  1. 获取当前网络中的电脑数目及MAC-通过MAC查找IP-通过IP查询机器名

    Microsoft Windows [版本 ] 版权所有 (c) Microsoft Corporation.保留所有权利. C:\Users\Administrator>netsh netsh ...

  2. AC日记——[JLOI2014]松鼠的新家 洛谷 P3258

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  3. AC日记——魔术球问题 洛谷 P2765

    题目描述 «问题描述: 假设有n根柱子,现要按下述规则在这n根柱子中依次放入编号为1,2,3,...的球. (1)每次只能在某根柱子的最上面放球. (2)在同一根柱子中,任何2个相邻球的编号之和为完全 ...

  4. AC日记——让我们异或吧 洛谷 P2420

    题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...

  5. js-在url后面添加时间戳清除浏览器打开页面的缓存

    这个解决办法还是在网上搜出来的,我还没有测试呢: 我有想既然可以添加时间戳,那可以添加随机数吗?我感觉是可以的,尽管没有测试过. 2018-3-13 几天前我就这个问题询问过我们的后台,加时间戳能否真 ...

  6. (4)ASP.NET HttpRequest 类

    HttpRequest 类的主要作用是读取客户端在 Web 请求期间发送的 HTTP 值. https://msdn.microsoft.com/zh-cn/library/system.web.ht ...

  7. Symmetric Tree(DFS,二叉树的构建以及测试代码)

    基础有待加强啊,由该题引发出来一些问题,现在来总结下. 首先是二叉树的结构: struct TreeNode { EleType val; TreeNode *left; TreeNode *righ ...

  8. andriod 获得应用程序名称

    import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; im ...

  9. Android学习笔记(35):Android活动条

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,当中有一个很好用的新功能就是引入的ActionBar,用于代替3.0之前的标题栏,并提供更为丰富的导航效果. ActionB ...

  10. c++ concurrency serial 1: introduction

    platform: vs2012 Code#include <iostream> #include <thread> using namespace std; void Fun ...