RollingText Android Library

repository·master·Indexed 21 days ago

https://github.com/yvescheung/rollingtext

An Android library providing RollingTextView, which displays text changes via animated rolling transitions. It supports customizable character transition strategies via the CharOrderStrategy interface and predefined character orders such as Alphabet, Number, Hex, and Binary. Users can adjust animation smoothness using a factor parameter and configure the view via XML or Java. The library also includes an optional Uinspector plugin for runtime property inspection.

Tokens
2.3K
Snippets
8
Records
13
Agent score
74%

What's inside RollingText

  1. Configure Animation Strategies and Character Orders

    master

    RollingText allows you to define how characters transition from one state to another.

    Animation Strategies

    • Default Behavior: Small characters roll down to become large characters, and vice versa.
    • Directional Control: You can specify strategies to make rolling occur in a single direction.
    • Carry/Increment Animation: Supports incrementing from low-value digits to high-value digits (not limited to decimal). Note: This is only for strings with length < 10 to prevent integer overflow and requires the character sequence to include '0'.

    Character Orders

    You must define the sequence of characters to tell the view how to transition between the original and target characters. You can add multiple orders; the first one that matches both the source and target characters takes priority.

    Commonly used orders available in CharOrder constants:

    • CharOrder.Alphabet
    • CharOrder.UpperAlphabet
    • CharOrder.Number
    • CharOrder.Hex
    • CharOrder.Binary

    To implement custom behavior, implement the CharOrderStrategy interface.

    alphaBetView.addCharOrder(CharOrder.Alphabet);
    alphaBetView.addCharOrder(CharOrder.UpperAlphabet);
    alphaBetView.addCharOrder(CharOrder.Number);
    alphaBetView.addCharOrder(CharOrder.Hex);
    alphaBetView.addCharOrder(CharOrder.Binary);
  2. How character orders work

    master

    To tell RollingTextView how to transition between characters, you must provide a sequence of characters using addCharOrder.

    Common sequences are available in the CharOrder constant class (e.g., Alphabet, Number, Hex, Binary). If multiple orders are applicable to a character transition, the order in which they were added determines precedence (the previously added order has higher precedence).

    alphaBetView.addCharOrder(CharOrder.Alphabet);
    alphaBetView.addCharOrder(CharOrder.UpperAlphabet);
    alphaBetView.addCharOrder(CharOrder.Number);
    alphaBetView.addCharOrder(CharOrder.Hex);
    alphaBetView.addCharOrder(CharOrder.Binary);
  3. How animation strategies work

    master

    Rolling effects are determined by Strategy.

    • Default behavior: Rolls down when characters change from small to large, and vice versa.
    • Custom direction: You can specify rolling in the same direction.
    • Carry animation: Can work from low digits to high digits (not just for decimals).
      • Constraints: Only works for strings with length < 10 to prevent integer overflow, and the sequence must contain '0' for the carry calculation to be meaningful.
  4. Install RollingText via JitPack

    master

    To use RollingTextView in your Android project, add the JitPack repository to your allprojects block and then add the dependency to your app-level build.gradle file. Replace x.y.z with the latest version.

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }
    
    dependencies {
        implementation "com.github.YvesCheung.RollingText:RollingText:x.y.z"
    }
  5. Debug RollingTextView with Uinspector

    master

    You can inspect RollingTextView properties at runtime using Uinspector. Add it as a debugImplementation dependency to ensure it only runs in debug builds.

    dependencies {
        // debugImplementation because Inspector should only run in debug builds.
        debugImplementation "com.github.YvesCheung.RollingText:RollingTextInspector:x.y.z"
    }
  6. Install RollingTextView via JitPack

    master

    To use RollingTextView in your Android project, add the JitPack repository to your allprojects block and then add the dependency to your dependencies block. Replace x.y.z with the latest version.

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }
    
    dependencies {
      implementation "com.github.YvesCheung.RollingText:RollingText:x.y.z"
    }
  7. Use RollingTextView in Java code

    master

    To trigger rolling animations, use the setText method. You can customize the animation duration, strategy, character order, and interpolator via the API. The setText method will perform a rolling animation from the current text to the new text.

    final RollingTextView rollingTextView = findViewById(R.id.alphaBetView);
    rollingTextView.setAnimationDuration(2000L);
    rollingTextView.setCharStrategy(Strategy.NormalAnimation);
    rollingTextView.addCharOrder(CharOrder.Alphabet);
    rollingTextView.setAnimationInterpolator(new AccelerateDecelerateInterpolator());
    rollingTextView.addAnimatorListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            // finish
        }
    });
    rollingTextView.setText("i am a text");
  8. Configure RollingTextView in XML

    master

    You can use RollingTextView in your layout files just like a standard TextView. It supports common attributes such as android:textSize, android:textColor, android:textStyle, android:gravity, and shadow properties.

    <com.yy.mobile.rollingtextview.RollingTextView
        android:id="@+id/alphaBetView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="i am text"
        android:textSize="25sp" 
        android:textColor="#1d1d1d"
        android:textStyle="bold"
        android:gravity="center"
        android:shadowColor="#ff44ffdd"
        android:shadowDx="10"
        android:shadowDy="10"
        android:shadowRadius="10"/>
  9. Inspect RollingTextView properties via UInspector

    master

    The RollingTextInspectService is a plugin for the UInspector framework that allows developers to inspect the runtime properties of RollingTextView instances. When integrated, it exposes the following properties in the inspector UI:

    • text: The current text content (quoted).
    • textSize: The text size in sp units.
    • textColor: The text color converted to a string representation.
    • isBold: Boolean string ("true") if the typeface is bold.
    • isItalic: Boolean string ("true") if the typeface is italic.
    • letterSpacingExtra: The extra letter spacing value (if non-zero).
    • strategy: The simple name of the character strategy class being used.
    • char order: A list representing the character sets used (e.g., [Number], [Hex], [Binary], [Alphabet], [UpperAlphabet]).
    • duration: The animation duration.