当前位置:网站首页>带文字的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>
边栏推荐
- Redis实现全局唯一ID
- Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
- PM2 deploy nuxt3 JS project
- Webrtc series - network transmission 5: select the optimal connection switching
- Groovy通过withCredentials获取Jenkins凭据
- 【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
- Niuke network realizes simple calculator function
- 荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
- PHP封装一个文件上传类(支持单文件多文件上传)
- Applet cloud data, data request a method to collect data
猜你喜欢

学习太极创客 — ESP8226 (十二)ESP8266 多任务处理

Mba-day25 best value problem - application problem

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

支持向量机(SVC,NuSVC,LinearSVC)

Redis实现全局唯一ID

2022.06.23 (traversal of lc_144,94145\

普通人没有学历,自学编程可以月入过万吗?

解决:jmeter5.5在win11下界面上的字特别小
Depens:*** but it is not going to be installed
![[noi Simulation Competition] send (tree DP)](/img/5b/3beb9f5fdad00b6d5dc789e88c6e98.png)
[noi Simulation Competition] send (tree DP)
随机推荐
【ES6闯关】Promise堪比原生的自定义封装(万字)
"I can't understand Sudoku, so I choose to play Sudoku."
Target of cmake command_ compile_ options
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
What do you mean by waiting for insurance records? Where should I go for filing?
Pytoch read data set (two modes: typical data set and user-defined data set)
P6698-[balticoi 2020 day2] virus [AC automata, DP, SPFA]
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
Linux MySQL installation
读CVPR 2022目标检测论文得到的亿点点启发
Ordinary people have no education background. Can they earn more than 10000 yuan a month by Self-taught programming?
软件系统依赖关系分析
可直接套用的Go编码规范
【输入法】迄今为止,居然有这么多汉字输入法!
P6117-[JOI 2019 Final]コイン集め【贪心】
[redis implements seckill business ①] seckill process overview | basic business implementation
每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
Every (), map (), forearch () methods. There are objects in the array
Lu Qi: I am most optimistic about these four major technology trends
Niuke network realizes simple calculator function