日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android数字累加,Android自己设置View之数字自动增长

發布時間:2023/12/2 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android数字累加,Android自己设置View之数字自动增长 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一次寫文,請多指教,有何問題及改進建議都可以告訴我-.-

Idea來自金山詞霸App的單詞計數,下面先放圖

autoNumber.gif

如上圖,就是,下面開始進入自己設置View

自己設置View步驟

1. 自己設置屬性

2. 生成構造方法

3. onMeasure(可選)

4. onSizeChanged(可選)

5. onLayout(可選)

6. onDraw

我這里只重寫了onSizeChanged,onMeasure和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.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); //中心點X,Y 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.onDraw@Override 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)

總結

以上是生活随笔為你收集整理的android数字累加,Android自己设置View之数字自动增长的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。