常用网站推广方法电商,微信商城小程序官网,天津招聘网人才招聘官网,洛阳作公司网站第一次写文#xff0c;请多指教#xff0c;有何问题及改进建议都可以告诉我-.-Idea来自金山词霸App的单词计数#xff0c;下面先放图autoNumber.gif如上图#xff0c;就是#xff0c;下面开始进入自己设置View自己设置View步骤1. 自己设置属性2. 生成构造方法3. onMeasure…第一次写文请多指教有何问题及改进建议都可以告诉我-.-Idea来自金山词霸App的单词计数下面先放图autoNumber.gif如上图就是下面开始进入自己设置View自己设置View步骤1. 自己设置属性2. 生成构造方法3. onMeasure(可选)4. onSizeChanged(可选)5. onLayout(可选)6. onDraw我这里只重写了onSizeChangedonMeasure和onLayout没有重写1.自己设置属性values里面新建attrs //变化速度 //边框颜色 //数字颜色 2.生成构造方法public AutoNumberView(Context context) { super(context); } public AutoNumberView(Context context, Nullable AttributeSet attrs) { super(context, attrs); //自己设置属性 TypedArray typedArray context.obtainStyledAttributes(attrs, R.styleable.AutoNumberView); strokeColor typedArray.getColor(R.styleable.AutoNumberView_stroke_color, context.getResources().getColor(R.color.colorPrimaryDark)); autoSpeed typedArray.getInteger(R.styleable.AutoNumberView_auto_speed, 1000); textColor typedArray.getColor(R.styleable.AutoNumberView_text_color, context.getResources().getColor(R.color.black)); typedArray.recycle(); init(); initAnimation(); }初始化动画和画笔private void init() { paint new Paint(); paint.setColor(strokeColor); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(10); paint.setAntiAlias(true); textPaint new Paint(); textPaint.setColor(textColor); textPaint.setStyle(Paint.Style.STROKE); textPaint.setTextAlign(Paint.Align.CENTER); textPaint.setAntiAlias(true); } private void initAnimation() { //根据属性动画值重绘数字 valueAnimator ValueAnimator.ofFloat(0,1).setDuration(autoSpeed); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { Override public void onAnimationUpdate(ValueAnimator animation) { value (float) animation.getAnimatedValue(); invalidate(); } }); }3.onSizeChangedOverride protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int min Math.min(w, h); //中心点XY centerX w / 2; centerY h / 2; radius (int) (min * 0.8f / 2); textPaint.setTextSize(radius / 2); //计算数字位于中心点的矩形 targetRect new Rect(-min / 2, -min / 2, min / 2, min / 2); Paint.FontMetricsInt fontMetrics textPaint.getFontMetricsInt(); //中线 baseline (targetRect.bottom targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2; }4.onDrawOverride protected void onDraw(Canvas canvas) { //移动中心点 canvas.translate(centerX, centerY); //边框 canvas.drawCircle(0, 0, radius, paint); //数字 canvas.drawText(String.valueOf((int)(value * number)), targetRect.centerX(), baseline, textPaint); }5.使用方法public class MainActivity extends AppCompatActivity { ... Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //设置数值 autoNumberView.get(0).setNumber((int) (Math.random() * 500 1000)); autoNumberView.get(1).setNumber((int) (Math.random() * 500 1000)); autoNumberView.get(2).setNumber((int) (Math.random() * 500 1000)); showLoading.setOnClickListener(new View.OnClickListener() { Override public void onClick(View v) { //启动 for (AutoNumberView auto : autoNumberView) { auto.startAnimation(); } } }); numberValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //设置数值 value.setText(设置值: progress * Math.random() * 1000); for (AutoNumberView auto : autoNumberView) { auto.setNumber((int) ((Math.random() * 1000) * progress)); } } }); autoSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //设置速度 speed.setText(设置速度: progress * 100); for (AutoNumberView auto : autoNumberView) { auto.setAutoSpeed(100 * progress); } } }); }}最后一律代码地址(GitHub - alaidev/AutoNumber)