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

Whether iOS16 is worth updating depends on whether you like these features.

x dm, the long-awaited iOS16 official version is ...

iPod creator: I can save Google Glass

[[142104]] On July 26, despite the huge attention...

Analysis of 14 excellent case strategies in 7 major information flow industries!

This article shares with you excellent case analy...

How can content operations advance from zero foundation to the workplace?

First, let’s interpret the first part: The past a...

ASO keyword selection skills!

As we all know, the first step for everyone after...

Analysis of the Douyin app operation and promotion plan!

Short videos have become the hottest track in rec...

A complete analysis of the Toutiao search account setup and delivery ideas

As a new search platform launched this year, Tout...