当前位置:   article > 正文

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

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

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);

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/384689
推荐阅读
相关标签
  

闽ICP备14008679号