Android 自定义SeekBar显示进度百分比,移动开发工程师薪酬

// Set background Color

// Color by resource

int bacgroundColor = attrs.getAttributeResourceValue(ANDROIDXML,

“background”, -1);

if (bacgroundColor != -1) {

setBackgroundColor(getResources().getColor(bacgroundColor));

} else {

// Color by hexadecimal

int background = attrs.getAttributeIntValue(ANDROIDXML,

“background”, -1);

if (background != -1)

setBackgroundColor(background);

}

showNumberIndicator = attrs.getAttributeBooleanValue(MATERIALDESIGNXML,

“showNumberIndicator”, false);

min = attrs.getAttributeIntValue(MATERIALDESIGNXML, “min”, 0);

max = attrs.getAttributeIntValue(MATERIALDESIGNXML, “max”, 0);

value = attrs.getAttributeIntValue(MATERIALDESIGNXML, “value”, min);

ball = new Ball(getContext());

RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,

getResources()), Utils.dpToPx(20, getResources()));

params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

ball.setLayoutParams(params);

addView(ball);

// 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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,可以通过自定义SeekBar实现数字滑动功能。首先,在布局文件中定义自定义SeekBar的样式,可以使用ProgressBar来实现。如下所示: ``` <ProgressBar android:id="@+id/customSeekBar" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:progressDrawable="@drawable/custom_seekbar_progress" android:thumb="@drawable/custom_seekbar_thumb" /> ``` 在drawable文件夹下创建custom_seekbar_progress.xml和custom_seekbar_thumb.xml来定义SeekBar的背景和滑块样式。在custom_seekbar_progress.xml中,可以使用shape和gradient标签来定义进度条的背景样式。在custom_seekbar_thumb.xml中,可以使用shape标签来定义滑块的样式。 接下来,在Activity或Fragment中找到SeekBar的实例,并设置OnSeekBarChangeListener监听器。在监听器中,通过getProgress方法获取SeekBar进度值,并根据需要进行相应的处理。例如,可以在TextView中显示SeekBar进度值,如下所示: ``` SeekBar customSeekBar = findViewById(R.id.customSeekBar); final TextView progressTextView = findViewById(R.id.progressTextView); customSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progressTextView.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // 当开始滑动SeekBar时执行的操作 } @Override public void onStopTrackingTouch(SeekBar seekBar) { // 当结束滑动SeekBar时执行的操作 } }); ``` 通过设置OnSeekBarChangeListener监听器,可以在SeekBar滑动时实时更新进度值,并进行相应的处理操作。根据自己的需求,可以在onProgressChanged、onStartTrackingTouch和onStopTrackingTouch方法中添加自定义的逻辑。 以上就是使用自定义SeekBar实现数字滑动的简单方法。可以根据自己的需求进行进一步的定制和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值