Android digital jumping TextView implementation

Android digital jumping TextView implementation

[[189084]]

Introduction

DancingNumberView is a control for displaying numbers in text in a dancing manner. It inherits from TextView. This control is generally used to display numbers that are sensitive to users, such as amounts, to make UI interactions more vivid.

It has the following features:

  • Automatically get all the numbers in the text and start jumping at the same time, eliminating the trouble of splicing multiple TextViews
  • Supports displaying numbers in a custom format, such as limiting the display to only two decimal places

Effect Preview

Import and use

Gradle

Step 1: Add the following to the appropriate location in the project's build.gradle file:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url "https://jitpack.io" }
  5. }
  6. }

Step 2: Add dependencies to the appropriate location in the app's build.gradle file

  1. dependencies {
  2. compile 'com.github.JianxunRao:DancingNumberView:V1.0.1'  
  3. }

How to use

Via XML layout

  1. <me.trojx.dancingnumber.DancingNumberView
  2. android:id= "@+id/dnv"  
  3. android:layout_width= "wrap_content"  
  4. android:layout_height= "wrap_content"  
  5. app:dnv_duration= "6000"  
  6. app:dnv_format= "%.2f" />

Via Java code

  1. DancingNumberView dnv = (DancingNumberView) findViewById(R.id.dnv);
  2. dnv.setText(text); //Set the display content
  3. dnv.setDuration(duration); //Set the duration of the completed jump (in ms)
  4. dnv.setFormat(format); //Set the display format of the number
  5. dnv.dance(); //Start the effect and start the digital dance

Key Code

  1. /**
  2. * The numbers in the text start to jump
  3.  
  4. */
  5.  
  6. public void dance() {
  7.  
  8. text = getText().toString();
  9.  
  10. numbers = new ArrayList<>();
  11.  
  12. Pattern pattern = Pattern.compile( "\\d+(\\.\\d+)?" );
  13.  
  14. Matcher matcher=pattern.matcher(text);
  15.  
  16. while (matcher.find()){
  17.  
  18. numbers. add ( Float . parseFloat(matcher. group ()));
  19.  
  20. }
  21.  
  22. textPattern = text.replaceAll( "\\d+(\\.\\d+)?" ,PLACEHOLDER);
  23.  
  24. numberTemp=new float [numbers. size ()];
  25.  
  26. ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(this, "factor" ,0,1);
  27.  
  28. objectAnimator.setDuration(duration);
  29.  
  30. objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
  31.  
  32. objectAnimator.start();
  33.  
  34. }
  35.  
  36. /**
  37.  
  38. * Get the arithmetic factor
  39.  
  40. * @return arithmetic factor
  41.  
  42. */
  43.  
  44. public   float getFactor() {
  45.  
  46. return factor;
  47.  
  48. }
  49.  
  50. /**
  51.  
  52. * Set the arithmetic factor, called by ObjectAnimator
  53.  
  54. * @see ObjectAnimator
  55.  
  56. * @param factor arithmetic factor
  57.  
  58. */
  59.  
  60. public void setFactor( float factor) {
  61.  
  62. String textNow=textPattern;
  63.  
  64. this.factor = factor;
  65.  
  66. for ( int i=0;i<numberTemp.length;i++){
  67.  
  68. numberTemp[i]=numbers.get(i)*factor;
  69.  
  70. textNow=textNow.replaceFirst(PLACEHOLDER,String.format(format,numberTemp[i]));
  71.  
  72. }
  73.  
  74. setText(textNow);
  75.  
  76. }

<<:  Deeply debug network requests using WireShark

>>:  Android uses Retrofit 2 to implement multiple file uploads

Recommend

Tardigrades become the first animals to achieve quantum entanglement?

Tardigrades become the first animals to achieve q...

Stop swallowing pills! These pills can also be chewed

Author: Huang Yanhong Duan Yuechu In daily life, ...

Why do many smokers not get lung cancer?

Smoking is the number one risk factor for lung ca...

Those who desperately promote 5G mobile phones are either stupid or evil.

These days, I don’t know what’s going on, but I’v...

5 general methods to increase user growth!

This article gives examples of 5 common methods t...

Wang Tong · Douyin SEO practical class, guide you step by step to do Douyin SEO

Wang Tong · Douyin SEO landing practice class, ha...

How to implement a lock screen widget for our App

One of the most requested features of iOS is a cu...

Can we follow the trend of using AI to fill in college applications?

Free volunteer application software, proven to be...