Welcome to StackOverflow!个
下面是一个quick and dirty
实现,让您对此有所了解.您将需要建立一个基于您的需求的better logic
.这里的 idea 是使用Handler
在word
(在下面的示例中是Hello
)的字符后面附加特定的delay
,以使其具有typewriter
种效果.
MainActivity.java个
public class MainActivity extends AppCompatActivity {
TextView typingt;
Button typingb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
typingb = findViewById(R.id.typingb);
typingt = findViewById(R.id.typingt);
typingb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
typingt.setText("H");
}
}, 300);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
typingt.append("e");
}
}, 400);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
typingt.append("e");
}
}, 500);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
typingt.append("l");
}
}, 600);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
typingt.append("o");
}
}, 700);
}
});
}
}
activity_main.xml个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/typingt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:text="Hello"
android:textColor="@color/black"
android:textSize="30dp" />
<Button
android:id="@+id/typingb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:background="@color/black"
android:padding="20dp"
android:text="Typing Animation" />
</LinearLayout>
希望这篇文章能给你一些启发.