4
I would like the text of the textView present in my Activity not to be displayed all at once, but gradually, something like a Power Point transition.
An example of what I want to do exactly would be dialogue texts from GBA Pokémon games SEE FROM 1:33 AM not necessarily one character at a time as displayed in the video, but one word at a time until the end of the text.
I would like to know if you can limit the total writing time of the text, so that if the limit is exceeded, the rest of the text is written instantly (for very long texts).
Activity:
package genesysgeneration.font;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(android.R.layout.activity_list_item);
tv=(TextView)findViewById(R.id.tv);
tv.setText("Lorem Ipsum é simplesmente uma simulação de texto da indústria tipográfica e de impressos, e vem sendo utilizado desde o século XVI, quando um impressor desconhecido pegou uma bandeja de tipos e os embaralhou para fazer um livro de modelos de tipos. Lorem Ipsum sobreviveu não só a cinco séculos, como também ao salto para a editoração eletrônica, permanecendo essencialmente inalterado. Se popularizou na década de 60, quando a Letraset lançou decalques contendo passagens de Lorem Ipsum, e mais recentemente quando passou a ser integrado a softwares de editoração eletrônica como Aldus PageMaker.");
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Edit:
I tried the one suggested by @Mr_anderson, but I was unsuccessful.
Several lines presented errors:
Mainactivity:
package genesysgeneration.pokemaos;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TypeWriter t = (TypeWriter)findViewById(R.id.meuTxt);
t.setCharacterDelay(100);
t.animateText("Olha só");
}
}
Class:
package genesysgeneration.pokemaos;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import java.util.logging.Handler;
public class TypeWriter extends TextView {
private CharSequence mText;
private int mIndex;
private long mDelay = 500;
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;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="genesysgeneration.pokemaos.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.Pokemaos.view.custom.TypeWriter
android:id="@+id/meuTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
You can use this answer as the source: http://stackoverflow.com/questions/6700374/android-character-by-character-display-text-animation
– Mr_Anderson