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

Running x86 apps is futile! Win10 mobile is a complete tragedy

On December 8, at the Windows Hardware Engineerin...

Operation and development of cash loan products

After being exposed to cash loans for some time, ...

Huang Jie: Market Price + Position: The Final Battle is Over

Huang Jie: Quotes + Positions: The Final Battle R...

Xiaohongshu’s hot-selling notes trends!

There were Labor Day and other promotional events...

Toutiao’s addictive data mining

Due to some irresistible forces, Toutiao 's p...

Alibaba internal review | How to plan a large-scale brand event from scratch

Organizing various celebration activities is a pr...

The discovery of the Zhenghe Bamin bird rewrites the history of bird evolution

A bird that lived in Fujian 150 million years ago...

OPPO and vivo outperform Huawei: a victory of pragmatism

In 2016, while the overall structure of the globa...

Testin Cloud Testing CTO Xu Kun: Service-driven cloud testing

[[127491]] Xu Kun, CTO of Testin Cloud Testing. H...