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

How can fitness institutions better integrate online and offline?

After market education in the first half of this ...

Can header images also increase open rates? It's not impossible!

The backend revisions can always be adapted to, a...

How to memorize words: International Phonetic Symbols + Phonics

: : : : : : : : : : : : : : : : : : : : : : : : : ...

Tencent practical case! Review of QQ dream expression design

The deep integration of "Internet + charity&...

What is the reason why Baidu bidding promotion has no effect?

Many promotion customers complain about the poor ...

What should be the bandwidth of an average server?

What should be the bandwidth of an ordinary serve...