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

Alipay mini program operation strategy!

At the end of 2019, I switched from offline busin...

iOS Advanced - iOS Memory Management

1 It seems that everyone has considered the probl...

I had an Apple Watch when I was 20 years old.

Whatever it is, it’s an Apple Watch. Back to 1995...

Virtuality or reality is not important, promoting economic development is king

Recently, various opinions have emerged on the de...

Irritability, insomnia, depression...a mother trapped in menopause!

"If I had spent more time with her, she woul...

These "villain" animal professionals are all natives from South America

"The cunning fox took the poisoned apple bor...

Can Xiaomi avoid the death spiral after the sharp drop in shipments?

Lei Jun said at a press conference a few days ago...

Keep user operation system

In the past two years, because of the epidemic, e...

Talk about type placeholders in Swift

Swift’s type inference capabilities have been a c...

Advertising channel costs, optimization and techniques!

Regarding advertising, I shared some experiences ...

"Perfect Diary" brand marketing strategy

Even straight men may be familiar with the four w...