当前位置:网站首页>带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
2022-06-24 08:11:00 【Mars-xq】
自定义view
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatSeekBar;
public class TextSeekbar extends AppCompatSeekBar {
// 画笔
private Paint mPaint;
// 进度文字位置信息
private final Rect mProgressTextRect = new Rect();
// 滑块按钮宽度
private int mThumbWidth = 100;
public TextSeekbar(@NonNull Context context) {
this(context, null);
}
public TextSeekbar(@NonNull Context context, AttributeSet attrs) {
this(context, attrs, -1);
}
public TextSeekbar(@NonNull Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new TextPaint();
mPaint.setAntiAlias(true);
mPaint.setColor(getResources().getColor(R.color.color_FF000000));
mPaint.setTextSize(getResources().getDimension(R.dimen.dp_19));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// 若是不设置padding,当滑动到最左边或最右边时,滑块会显示不全
Drawable thumb = getThumb();
if (thumb != null) {
mThumbWidth = thumb.getIntrinsicWidth();
setPadding(mThumbWidth / 2, 0, mThumbWidth / 2, 0);
}
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
String progressText = getProgress() + "°";
mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);
// 进度百分比
float progressRatio = (float) getProgress() / getMax();
// thumb偏移量
float thumbOffset = (mThumbWidth - mProgressTextRect.width()) / 2.0f
- mThumbWidth * progressRatio;
float thumbX = getWidth() * progressRatio + thumbOffset;
float thumbY = getHeight() / 2f + mProgressTextRect.height() / 2f;
canvas.drawText(progressText, thumbX, thumbY, mPaint);
}
}
布局使用:
<TextSeekbar android:id="@+id/textSeekbar" android:layout_width="@dimen/dp_420" android:layout_height="@dimen/dp_52" android:layout_marginVertical="@dimen/dp_20" android:max="360" android:progressDrawable="@drawable/progress_drawable1" android:thumb="@drawable/thumb1" />
drawable/progress_drawable1:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#993D4249" />
<corners android:radius="@dimen/dp_8" />
<size android:width="@dimen/dp_420" android:height="@dimen/dp_52" />
</shape>
drawable/thumb1:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFD4D8E1" />
<corners android:radius="@dimen/dp_8" />
<size android:width="@dimen/dp_84" android:height="@dimen/dp_52" />
</shape>
边栏推荐
- 2021-05-20computed and watch applications and differences
- 【ES6闯关】Promise堪比原生的自定义封装(万字)
- [redis implements seckill business ①] seckill process overview | basic business implementation
- 【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
- 零基础自学SQL课程 | HAVING子句
- P6698-[BalticOI 2020 Day2]病毒【AC自动机,dp,SPFA】
- Zero foundation self-study SQL course | syntax sequence and execution sequence of SQL statements
- php文件锁
- Leetcode -- wrong set
- Support vector machine (SVC, nusvc, linearsvc)
猜你喜欢

NETRCA: AN EFFECTIVE NETWORK FAULT CAUSE LOCALIZATION之论文阅读

Ordinary people have no education background. Can they earn more than 10000 yuan a month by Self-taught programming?

tp5 使用post接收数组数据时报variable type error: array错误的解决方法

EasyExcel单sheet页与多sheet页写出
![[use picgo+ Tencent cloud object to store cos as a map bed]](/img/14/d650960cc77385504ea5e2e138bd46.jpg)
[use picgo+ Tencent cloud object to store cos as a map bed]

【ES6闯关】Promise堪比原生的自定义封装(万字)

Remote connection of raspberry pie without display by VNC viewer

jupyter入门常见的几个坑:

【LeetCode】541. Reverse string II

零基础自学SQL课程 | 子查询
随机推荐
Leetcode -- wrong set
PhpStrom代码格式化设置
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
Recommendation - Secret of curiosity: how many dancing angels can stand on the tip of a needle?
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试
L01_一条SQL查询语句是如何执行的?
L01_ How is an SQL query executed?
【LeetCode】387. First unique character in string
4274. suffix expression
Ordinary people have no education background. Can they earn more than 10000 yuan a month by Self-taught programming?
Unable to change the virtual machine power status and report an error solution
金仓KFS replicator安装(Oracle-KES)
活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中
When programmers are asked if they can repair computers... | daily anecdotes
每周推荐短视频:谈论“元宇宙”要有严肃认真的态度
Implementation process of tcpdump packet capturing
leetcode--字符串
Some common pitfalls in getting started with jupyter:
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
php文件锁