Android 自定义SeekBar显示进度百分比

// Set if slider content number indicator

// TODO

if (showNumberIndicator) {

numberIndicator = new NumberIndicator(getContext());

}

}

@Override

public void invalidate() {

ball.invalidate();

super.invalidate();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if (!placedBall)

placeBall();

if (value == min) {

// Crop line to transparent effect

Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(),

canvas.getHeight(), Bitmap.Config.ARGB_8888);

Canvas temp = new Canvas(bitmap);

Paint paint = new Paint();

paint.setColor(Color.parseColor(“#B0B0B0”));

paint.setStrokeWidth(Utils.dpToPx(2, getResources()));

temp.drawLine(getHeight() / 2, getHeight() / 2, getWidth()

  • getHeight() / 2, getHeight() / 2, paint);

Paint transparentPaint = new Paint();

transparentPaint.setColor(getResources().getColor(

android.R.color.transparent));

transparentPaint.setXfermode(new PorterDuffXfermode(

PorterDuff.Mode.CLEAR));

temp.drawCircle(ViewHelper.getX(ball) + ball.getWidth() / 2,

ViewHelper.getY(ball) + ball.getHeight() / 2,

ball.getWidth() / 2, transparentPaint);

canvas.drawBitmap(bitmap, 0, 0, new Paint());

} else {

Paint paint = new Paint();

paint.setColor(Color.parseColor(“#B0B0B0”));

paint.setStrokeWidth(Utils.dpToPx(2, getResources()));

canvas.drawLine(getHeight() / 2, getHeight() / 2, getWidth()

  • getHeight() / 2, getHeight() / 2, paint);

paint.setColor(backgroundColor);

float division = (ball.xFin - ball.xIni) / (max - min);

int value = this.value - min;

canvas.drawLine(getHeight() / 2, getHeight() / 2, value * division

  • getHeight() / 2, getHeight() / 2, paint);

}

if (press && !showNumberIndicator) {

Paint paint = new Paint();

paint.setColor(backgroundColor);

paint.setAntiAlias(true);

canvas.drawCircle(ViewHelper.getX(ball) + ball.getWidth() / 2,

getHeight() / 2, getHeight() / 3, paint);

}

invalidate();

}

@Override

public boolean onTouchEvent(MotionEvent event) {

isLastTouch = true;

if (isEnabled()) {

if (event.getAction() == MotionEvent.ACTION_DOWN

|| event.getAction() == MotionEvent.ACTION_MOVE) {

if (numberIndicator != null

&& numberIndicator.isShowing() == false)

numberIndicator.show();

if ((event.getX() <= getWidth() && event.getX() >= 0)) {

press = true;

// calculate value

int newValue = 0;

float division = (ball.xFin - ball.xIni) / (max - min);

if (event.getX() > ball.xFin) {

newValue = max;

} else if (event.getX() < ball.xIni) {

newValue = min;

} else {

newValue = min

  • (int) ((event.getX() - ball.xIni) / division);

}

if (value != newValue) {

value = newValue;

if (onValueChangedListener != null)

onValueChangedListener.onValueChanged(newValue);

}

// move ball indicator

float x = event.getX();

x = (x < ball.xIni) ? ball.xIni : x;

x = (x > ball.xFin) ? ball.xFin : x;

ViewHelper.setX(ball, x);

ball.changeBackground();

// If slider has number indicator

if (numberIndicator != null) {

// move number indicator

numberIndicator.indicator.x = x;

numberIndicator.indicator.finalY = Utils

.getRelativeTop(this) - getHeight() / 2;

numberIndicator.indicator.finalSize = getHeight() / 2;

numberIndicator.numberIndicator.setText(“”);

}

} else {

press = false;

isLastTouch = false;

if (numberIndicator != null)

numberIndicator.dismiss();

}

} else if (event.getAction() == MotionEvent.ACTION_UP

|| event.getAction() == MotionEvent.ACTION_CANCEL) {

if (numberIndicator != null)

numberIndicator.dismiss();

isLastTouch = false;

press = false;

}

}

return true;

}

/**

  • Make a dark color to press effect

  • @return

*/

protected int makePressColor() {

int r = (this.backgroundColor >> 16) & 0xFF;

int g = (this.backgroundColor >> 8) & 0xFF;

int b = (this.backgroundColor >> 0) & 0xFF;

r = (r - 30 < 0) ? 0 : r - 30;

g = (g - 30 < 0) ? 0 : g - 30;

b = (b - 30 < 0) ? 0 : b - 30;

return Color.argb(70, r, g, b);

}

private void placeBall() {

ViewHelper.setX(ball, getHeight() / 2 - ball.getWidth() / 2);

ball.xIni = ViewHelper.getX(ball);

ball.xFin = getWidth() - getHeight() / 2 - ball.getWidth() / 2;

ball.xCen = getWidth() / 2 - ball.getWidth() / 2;

placedBall = true;

}

// GETERS & SETTERS

public OnValueChangedListener getOnValueChangedListener() {

return onValueChangedListener;

}

public void setOnValueChangedListener(

OnValueChangedListener onValueChangedListener) {

this.onValueChangedListener = onValueChangedListener;

}

public int getValue() {

return value;

}

public void setValue(final int value) {

if (placedBall == false)

post(new Runnable() {

@Override

public void run() {

setValue(value);

}

});

else {

this.value = value;

float division = (ball.xFin - ball.xIni) / max;

ViewHelper.setX(ball,

value * division + getHeight() / 2 - ball.getWidth() / 2);

ball.changeBackground();

}

}

public int getMax() {

return max;

}

public void setMax(int max) {

this.max = max;

}

public int getMin() {

return min;

}

public void setMin(int min) {

this.min = min;

}

public boolean isShowNumberIndicator() {

return showNumberIndicator;

}

public void setShowNumberIndicator(boolean showNumberIndicator) {

this.showNumberIndicator = showNumberIndicator;

numberIndicator = (showNumberIndicator) ? new NumberIndicator(

getContext()) : null;

}

@Override

public void setBackgroundColor(int color) {

backgroundColor = color;

if (isEnabled())

beforeBackground = backgroundColor;

}

boolean placedBall = false;

class Ball extends View {

float xIni, xFin, xCen;

public Ball(Context context) {

super(context);

setBackgroundResource(R.drawable.background_switch_ball_uncheck);

}

public void changeBackground() {

if (value != min) {

setBackgroundResource(R.drawable.background_checkbox);

LayerDrawable layer = (LayerDrawable) getBackground();

GradientDrawable shape = (GradientDrawable) layer

.findDrawableByLayerId(R.id.shape_bacground);

shape.setColor(backgroundColor);

} else {

setBackgroundResource(R.drawable.background_switch_ball_uncheck);

}

}

}

// Slider Number Indicator

class NumberIndicator extends Dialog {

Indicator indicator;

TextView numberIndicator;

public NumberIndicator(Context context) {

super(context, android.R.style.Theme_Translucent);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE);

super.onCreate(savedInstanceState);

setContentView(R.layout.number_indicator_spinner);

setCanceledOnTouchOutside(false);

RelativeLayout content = (RelativeLayout) this

.findViewById(R.id.number_indicator_spinner_content);

indicator = new Indicator(this.getContext());

content.addView(indicator);

numberIndicator = new TextView(getContext());

numberIndicator.setTextColor(Color.WHITE);

numberIndicator.setGravity(Gravity.CENTER);

content.addView(numberIndicator);

indicator.setLayoutParams(new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.FILL_PARENT,

RelativeLayout.LayoutParams.FILL_PARENT));

}

@Override

public void dismiss() {

super.dismiss();

indicator.y = 0;

indicator.size = 0;

indicator.animate = true;

}

@Override

public void onBackPressed() {

}

}

class Indicator extends RelativeLayout {

// Position of number indicator

float x = 0;

float y = 0;

// Size of number indicator

float size = 0;

// Final y position after animation

float finalY = 0;

// Final size after animation

float finalSize = 0;

boolean animate = true;

boolean numberIndicatorResize = false;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

我坚信,坚持学习,每天进步一点,滴水穿石,我们离成功都很近!
以下是总结出来的字节经典面试题目,包含:计算机网络,Kotlin,数据结构与算法,Framework源码,微信小程序,NDK音视频开发,计算机网络等。

字节高级Android经典面试题和答案


《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

转存中…(img-uV8gfcne-1713765033697)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

[外链图片转存中…(img-HEV51cs0-1713765033698)]

最后

我坚信,坚持学习,每天进步一点,滴水穿石,我们离成功都很近!
以下是总结出来的字节经典面试题目,包含:计算机网络,Kotlin,数据结构与算法,Framework源码,微信小程序,NDK音视频开发,计算机网络等。

字节高级Android经典面试题和答案

[外链图片转存中…(img-NAB1V9ZV-1713765033698)]
[外链图片转存中…(img-g2o5XTBa-1713765033699)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值