且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android实现TextView字符串波浪式跳动

更新时间:2022-07-01 02:15:14



Android实现TextView字符串波浪式跳动

在github上有一个开源项目:JumpingBeans,其项目主页是:https://github.com/frakbot/JumpingBeans
JumpingBeans将一个普通的Android TextView中显示的字符串可以做到波浪式跳动。JumpingBeans使用起来简单,仅仅在Android的Java代码中将一个普通Android TextView加载即可:

package zhangphil.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import net.frakbot.jumpingbeans.JumpingBeans;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		// 增加跳动的点
		final TextView textView1 = (TextView) findViewById(R.id.textView1);
		JumpingBeans jumpingBeans1 = JumpingBeans.with(textView1)
		        .appendJumpingDots()
		        .build();

		// 从第一个字符串到最后一个字符串波浪式循环跳动
		final TextView textView2 = (TextView) findViewById(R.id.textView2);
		JumpingBeans jumpingBeans2 = JumpingBeans.with(textView2)
		        .makeTextJump(0, textView2.getText().length())
		        .setIsWave(true)
		        .setLoopDuration(3000) 
		        .build();
	}
}


布局文件中仅仅有两个普通的Android TextView:

<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:orientation="vertical"
    tools:context="zhangphil.demo.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:gravity="center"
        android:text="Zhang Phil @ CSDN"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:gravity="center"
        android:text="Zhang Phil @ CSDN"
        android:textSize="20sp" />

</LinearLayout>



结果如图所示:

Android实现TextView字符串波浪式跳动