android数字累加,Android自己设置View之数字自动增长
第一次寫文,請(qǐng)多指教,有何問題及改進(jìn)建議都可以告訴我-.-
Idea來自金山詞霸App的單詞計(jì)數(shù),下面先放圖
autoNumber.gif
如上圖,就是,下面開始進(jìn)入自己設(shè)置View
自己設(shè)置View步驟
1. 自己設(shè)置屬性
2. 生成構(gòu)造方法
3. onMeasure(可選)
4. onSizeChanged(可選)
5. onLayout(可選)
6. onDraw
我這里只重寫了onSizeChanged,onMeasure和onLayout沒有重寫
1.自己設(shè)置屬性
values里面新建attrs //變化速度 //邊框顏色 //數(shù)字顏色
2.生成構(gòu)造方法public AutoNumberView(Context context) { super(context); } public AutoNumberView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //自己設(shè)置屬性 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(); }
初始化動(dòng)畫和畫筆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() { //根據(jù)屬性動(dòng)畫值重繪數(shù)字 valueAnimator = ValueAnimator.ofFloat(0,1).setDuration(autoSpeed); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { value = (float) animation.getAnimatedValue(); invalidate(); } }); }
3.onSizeChanged@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int min = Math.min(w, h); //中心點(diǎn)X,Y centerX = w / 2; centerY = h / 2; radius = (int) (min * 0.8f / 2); textPaint.setTextSize(radius / 2); //計(jì)算數(shù)字位于中心點(diǎn)的矩形 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.onDraw@Override protected void onDraw(Canvas canvas) { //移動(dòng)中心點(diǎn) canvas.translate(centerX, centerY); //邊框 canvas.drawCircle(0, 0, radius, paint); //數(shù)字 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); //設(shè)置數(shù)值 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) { //啟動(dòng) for (AutoNumberView auto : autoNumberView) { auto.startAnimation(); } } }); numberValue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //設(shè)置數(shù)值 value.setText("設(shè)置值:" + 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) { //設(shè)置速度 speed.setText("設(shè)置速度:" + progress + "* 100"); for (AutoNumberView auto : autoNumberView) { auto.setAutoSpeed(100 * progress); } } }); }}
最后一律代碼地址(GitHub - alaidev/AutoNumber)
總結(jié)
以上是生活随笔為你收集整理的android数字累加,Android自己设置View之数字自动增长的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑显示器贴合工艺要求(显示器全贴合)
- 下一篇: android 通知垃圾回收,Andro