Android znak po znaku wyświetlanie animacji tekstowej

Ktoś zna jakąś skuteczną metodę wykonania animacji, która polega na wyświetlaniu tekstu, znak po znaku?, jak:

T
Th
Thi
To
This I
To jest
...

I tak dalej.

Dzięki!

Author: zegnus, 2011-07-15

7 answers

Może nie jest to najbardziej eleganckie rozwiązanie, ale najprostszym jest prawdopodobnie szybka podklasa TextView z Handler, która aktualizuje tekst co jakiś czas, aż zostanie wyświetlona pełna sekwencja:

public class Typewriter extends TextView {

    private CharSequence mText;
    private int mIndex;
    private long mDelay = 500; //Default 500ms delay


    public Typewriter(Context context) {
        super(context);
    }

    public Typewriter(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private Handler mHandler = new Handler();
    private Runnable characterAdder = new Runnable() {
        @Override
        public void run() {
            setText(mText.subSequence(0, mIndex++));
            if(mIndex <= mText.length()) {
                mHandler.postDelayed(characterAdder, mDelay);
            }
        }
    };

    public void animateText(CharSequence text) {
        mText = text;
        mIndex = 0;

        setText("");
        mHandler.removeCallbacks(characterAdder);
        mHandler.postDelayed(characterAdder, mDelay);
    }

    public void setCharacterDelay(long millis) {
        mDelay = millis;
    }
}

Możesz użyć tego w takim ćwiczeniu:

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Typewriter writer = new Typewriter(this);
        setContentView(writer);

        //Add a character every 150ms
        writer.setCharacterDelay(150);
        writer.animateText("Sample String");
    }
}

Jeśli chcesz mieć efekty animacji z każdą dodaną literą, może przyjrzyj się podklasowaniu TextSwitcher.

Mam nadzieję, że to pomoże!
 94
Author: Devunwired,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-07-14 22:29:32

Użyj klasy Maszyny do pisania Devunwired

Następnie w układzie:

<com.example.fmm.Typewriter
    android:id="@+id/typewriter"
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

Kod w działaniu:

Typewriter writer = (Typewriter)findViewById(R.id.typewriter);
        //Add a character every 150ms
        writer.setCharacterDelay(150);
        writer.animateText("Sample String");
 15
Author: K-RAD,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-28 23:18:45

Nie trzeba ustawiać dodatkowej klasy Użyj tego, tutaj tv jest textview w twoim układzie po prostu zadzwoń

SetCharacterDelay (150);
animateText ("Sample String");

private Handler mHandler = new Handler();
private Runnable characterAdder = new Runnable() {
@Override
public void run() {
    tv.setText(mText.subSequence(0, mIndex++));
    if(mIndex <= mText.length()) {
        mHandler.postDelayed(characterAdder, mDelay);
    }
}
};

public void animateText(CharSequence text) {
mText = text;
mIndex = 0;

tv.setText("");
mHandler.removeCallbacks(characterAdder);
mHandler.postDelayed(characterAdder, mDelay);
}

public void setCharacterDelay(long millis) {
mDelay = millis;
}
 8
Author: Viswanath Lekshmanan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-15 18:15:09

Nowa kopia dla Devunwired z układem xml

    public class Typewriter extends TextView {

    private CharSequence mText;
    private int mIndex;
    private long mDelay = 500; //Default 500ms delay


    public Typewriter(Context context) {
        super(context);
    }

    public Typewriter(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private Handler mHandler = new Handler();
    private Runnable characterAdder = new Runnable() {
        @Override
        public void run() {
            setText(mText.subSequence(0, mIndex++));
            if(mIndex <= mText.length()) {
                mHandler.postDelayed(characterAdder, mDelay);
            }
        }
    };

    public void animateText(CharSequence text) {
        mText = text;
        mIndex = 0;

        setText("");
        mHandler.removeCallbacks(characterAdder);
        mHandler.postDelayed(characterAdder, mDelay);
    }

    public void setCharacterDelay(long millis) {
        mDelay = millis;
    }
}

Użycie kodu

        textView = (Typewriter)findViewById(R.id.textView1);
    //Add a character every 150ms
    textView.setCharacterDelay(150);
    textView.animateText("Sample String");

Następnie zdefiniuj textView w classStart

 3
Author: Baha'a Odeh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-27 14:11:21

Teoretycznie byłoby to


string text = "hello"
string temp = "h"

iterate: temp += if (text.length > temp.length) text[temp.length]; wait
Oczywiście zrobisz iterację w swojej metodzie runmethod.
 0
Author: RobotRock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-26 09:21:59

Większość rozwiązań podanych powyżej powoduje różne błędy. Myślę, że rozwiązania są stare. Natknąłem się na tę wtyczkę android studio i działa jak urok.

1.Instalacja AutoTypeTextView jest bardzo prosta. Wystarczy dodać w build.gradle

Compile ' com.krsticdragan: autotypetextview:1.1 '

2.Dodaj nową przestrzeń nazw, której będziesz używać do dodawania AutoTypeTextView i używania jego tagów.

Xmlns: attv="http://schemas.android.com/apk/res-auto"

Stąd twój root layout powinien wyglądać tak

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:attv="http://schemas.android.com/apk/res-auto"

  1. Dodaj to do pliku xml.

    <com.dragankrstic.autotypetextview.AutoTypeTextView android:id="@+id/lblTextWithoutMistakes" android:layout_width="wrap_content" android:layout_height="wrap_content" attv:animateTextTypeWithoutMistakes="Hello World!" />

/ Align = "left" / Możesz sprawdzić dokumentację tutaj, aby uzyskać więcej informacji
 0
Author: Mesonrale Ope Seun,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-28 18:49:58

Wiem, że jest już za późno, ale ktoś może przyjechać tutaj z Google. Właściwie też potrzebowałem czegoś takiego do mojej aplikacji, więc sam zrobiłem. Wypróbuj Fade-In TextView , sprawi, że każda postać pojawi się z płynną animacją Alfa. Użycie jest również dość proste.

W układzie XML

    <believe.cht.fadeintextview.TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:textColor="@android:color/black"

        app:letterDuration="250"/>

W aktywności / fragmencie

believe.cht.fadeintextview.TextView textView = (believe.cht.fadeintextview.TextView) findViewById(R.id.textView);

textView.setLetterDuration(250); // sets letter duration programmatically
textView.isAnimating(); // returns current animation state (boolean)
textView.setText(); // sets the text with animation

Więcej informacji

Fade-in TextView library dziedziczy swoje właściwości bezpośrednio z natywnej klasy TextView, co oznacza, że wszystkie natywne metody TextView są obsługiwane. Nie ma praktycznie żadnych ograniczeń, w tym obsługa wielu linii. Biblioteka posiada również kilka własnych metod i atrybutów, które oferują pełną kontrolę nad widokiem.

 0
Author: cht,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-19 10:31:49