android文本拖动,Android笔记之文本随滑块移动的SeekBar

博客内容展示了如何创建一个自定义的FloatingTextSeekBar,该控件继承自LinearLayout,内部包含一个SeekBar和一个FloatingTextView。SeekBar改变进度时,FloatingTextView会显示对应的百分比数值,并在屏幕内浮动显示。博客中还涉及到了Android的布局、属性设置、反射技术以及自定义View的绘制方法。
摘要由CSDN通过智能技术生成

packagecom.bu_ish.blog;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.os.Build;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.util.TypedValue;importandroid.view.View;importandroid.widget.LinearLayout;importandroid.widget.SeekBar;importjava.lang.reflect.Field;public class FloatingTextSeekBar extendsLinearLayout {privateSeekBar sb;privateFloatingTextView ftv;private String startText = "100";private int max = 49;private floatfloatingTextX;private static final String TAG = FloatingTextSeekBar.class.getName();publicFloatingTextSeekBar(Context context, AttributeSet attrs) {super(context, attrs);

setOrientation(VERTICAL);

sb= newSeekBar(context);

LayoutParams sbLayoutParams= newLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

sb.setLayoutParams(sbLayoutParams);

sb.setPadding(16, 0, 16, 0);try{

Class cls=sb.getClass().getSuperclass().getSuperclass();

Field field= cls.getDeclaredField("mMaxHeight");

field.setAccessible(true);

field.set(sb, PixelTool.dpToPx(context,5));

}catch (NoSuchFieldException |IllegalAccessException ex) {

Log.e(TAG,null, ex);

}

sb.setMax(max);

sb.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar_progress_drawable));

sb.setThumb(getResources().getDrawable(R.mipmap.ic_seek_bar_thumb));if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP)

sb.setSplitTrack(false);

addView(sb);

ftv= newFloatingTextView(context);

LayoutParams ftvLayoutParams= new LayoutParams(LayoutParams.MATCH_PARENT, PixelTool.dpToPx(context, 12));

ftvLayoutParams.topMargin= PixelTool.dpToPx(context, 2);

ftv.setLayoutParams(ftvLayoutParams);

ftv.setText(startText);

addView(ftv);

sb.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener() {

@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, booleanfromUser) {float step = seekBar.getWidth() / (float) max;

floatingTextX= progress *step;

ftv.setText((progress+ Integer.parseInt(startText) / 100) * 100 + "");

}

@Overridepublic voidonStartTrackingTouch(SeekBar seekBar) {

}

@Overridepublic voidonStopTrackingTouch(SeekBar seekBar) {

}

});

}public voidsetStartText(String startText) {this.startText =startText;

}public void setMax(intmax) {this.max =max;

sb.setMax(max);

}private class FloatingTextView extendsView {privateString text;privateFloatingTextView(Context context) {super(context);

}private voidsetText(String text) {this.text =text;

invalidate();

}

@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);

Paint paint= newPaint();

paint.setColor(Color.parseColor("#ff5158e7"));float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());

paint.setTextSize(textSize);

paint.setTextAlign(Paint.Align.CENTER);float textWidth =paint.measureText(text);if (floatingTextX - textWidth / 2

floatingTextX+= textWidth / 2;

}if (floatingTextX + textWidth / 2 >getRight()) {

floatingTextX-= textWidth / 2;

}

canvas.drawText(text, floatingTextX, getHeight(), paint);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值