Android TextView随进度条圆球移动

1.进度条范围0-39

   prams.leftMargin:TextView的左右移动大小

String lowValue = "0";
		String highValue = "39";
		String currentValue = "0";
		final String tempValue = String.valueOf(0 - Integer.parseInt(lowValue));
		sb_quota.setMax(Integer.parseInt(highValue) - Integer.parseInt(lowValue));
		sb_quota.setProgress(Integer.parseInt(currentValue) + Integer.parseInt(tempValue));
		sb_quota.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				tv_quota.setText(""+progress);
				Log.d(TAG,"progress = "+progress);
//				quota = progress;
				int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
				tv_quota.measure(spec, spec);
				int quotaWidth = tv_quota.getMeasuredWidth();

				int spec2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
				tv_quota.measure(spec2, spec2);
				int sbWidth = sb_quota.getMeasuredWidth();
				LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv_quota.getLayoutParams();
//				params.leftMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax());
				params.leftMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax() + 54);
				if (progress >= 10 && progress <= 38){
					params.leftMargin = params.leftMargin - 20;
				}else if (progress >= 39){
					params.leftMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax());
				}
				tv_quota.setLayoutParams(params);
				Log.d(TAG,"setLayoutParams = "+params.leftMargin);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				tv_quota.setVisibility(View.VISIBLE);
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				tv_quota.setVisibility(View.INVISIBLE);
			}
		});

2.进度条范围-14 -- 14;(从打印来看数值其实是0-28,想要负值mProgress = progress -14;)

 prams.topMargin:TextView的上下移动大小

String lowValue = "-14";
		String highValue = "14";
		String currentValue = "0";
		final String tempValue = String.valueOf(0 - Integer.parseInt(lowValue));
		sb_quota.setMax(Integer.parseInt(highValue) - Integer.parseInt(lowValue));
		sb_quota.setProgress(Integer.parseInt(currentValue) + Integer.parseInt(tempValue));
		sb_quota.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				int mProgress;
				mProgress = progress -14;
				tv_quota.setText(""+mProgress);
				Log.d(TAG,"progress = "+mProgress);
//				quota = progress;
				int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
				tv_quota.measure(spec, spec);
				int quotaWidth = tv_quota.getMeasuredWidth();

				int spec2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
				tv_quota.measure(spec2, spec2);
				int sbWidth = sb_quota.getMeasuredWidth();
				LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tv_quota.getLayoutParams();
//				params.leftMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax());
				params.topMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax() + 54);
				if (progress >= 10 && progress <= 38){
					params.topMargin = params.topMargin - 20;
				}else if (progress >= 39){
					params.topMargin = (int) (((double) progress / sb_quota.getMax()) * sbWidth - (double) quotaWidth * progress / sb_quota.getMax());
				}
				tv_quota.setLayoutParams(params);
				Log.d(TAG,"setLayoutParams = "+params.leftMargin);
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				tv_quota.setVisibility(View.VISIBLE);
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				tv_quota.setVisibility(View.INVISIBLE);
			}
		});

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值