当前位置:网站首页>Seekbar with text: customize progressdrawable/thumb: solve incomplete display
Seekbar with text: customize progressdrawable/thumb: solve incomplete display
2022-06-24 09:35:00 【Mars-xq】
Customize 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 {
// paint brush
private Paint mPaint;
// Progress text location information
private final Rect mProgressTextRect = new Rect();
// Slider button width
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);
// If not set padding, When sliding to the far left or right , The slider will not display completely
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);
// Percentage of progress
float progressRatio = (float) getProgress() / getMax();
// thumb Offset
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);
}
}
Layout use :
<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>
边栏推荐
- 每周推薦短視頻:談論“元宇宙”要有嚴肅認真的態度
- Learning Tai Chi Maker - esp8226 (XIII) OTA
- Ggplot2 color setting summary
- When programmers are asked if they can repair computers... | daily anecdotes
- Weekly recommended short video: is the ultimate form of computing "meta universe"?
- The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
- PHP封装一个文件上传类(支持单文件多文件上传)
- Go language project development practice directory
- Framework tool class obtained by chance for self use
- 零基础自学SQL课程 | 相关子查询
猜你喜欢

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

浮点数表示法(总结自CS61C和CMU CSAPP)

12、 Demonstration of all function realization effects

Support vector machine (SVC, nusvc, linearsvc)

Learning Tai Chi Maker - esp8226 (XIII) OTA

latex公式及表格识别

Event registration Apache pulsar x kubesphere online meetup hot registration

深入解析 Apache BookKeeper 系列:第三篇——读取原理

活动报名|Apache Pulsar x KubeSphere 在线 Meetup 火热报名中

零基础自学SQL课程 | 相关子查询
随机推荐
Cmake命令之target_compile_options
Go language project development practice directory
Oracle数据库EXPDP只导出表的方法
零基础自学SQL课程 | 子查询
jupyter入门常见的几个坑:
12、 Demonstration of all function realization effects
Zero foundation self-study SQL course | having clause
带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
【Eureka注册中心】
百度AI模板 获取知识理解
PostgreSQL
Go 语言项目开发实战目录
Oracle查看数据文件头SCN信息
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
零基础自学SQL课程 | 相关子查询
谈谈数字化转型晓知识
ThinkPHP5多语言切换项目实战
Event registration Apache pulsar x kubesphere online meetup hot registration
Cdga | how can we do well in data governance?
Vidéo courte recommandée chaque semaine: Soyez sérieux en parlant de "métaunivers"