Android 循环上下滚动

最近在作项目的时候遇到了一个上下滚动文字的需求,在网上找到了一个自定义的TextView,可是切换效果很图片,没有滚动的效果,考虑到html的marquee效果添加到TextView中,无奈没有效果,另外也浏览了js写的滚动,效果很好,可是应用起来很麻烦,毕竟是Android原生界面。最后,只能本身作一个了(注:此处是两句文字来回滚动)html

  • 1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fe0"
         >

        <TextView
            android:id="@+id/autoPlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="位移动画"
            android:padding="16dp"/>
         <TextView
            android:id="@+id/autoPlay1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="位移动画2"
            android:padding="16dp"/>
    </RelativeLayout>

</LinearLayout>

2.Activity中的代码android

private TextView aView;
private TextView aView1;
final Handler handler = new Handler() {
		public void handleMessage(Message msg) { // handle message
			switch (msg.what) {
			case 1:
				// UI操做
                          //此处两个TextView不分前后,只要与else里的顺序不一样便可
				if (curIndex == 1) {
					autoPlay(aView, aView1);
					curIndex++;
				} else {
					autoPlay(aView1, aView);
					curIndex--;
				}

				Message message = handler.obtainMessage(1);
				//此处延时应大于等于动画播放时间,不然会有卡顿现象
				// 发送message 
                                // 这样消息就能循环发送
                              handler.sendMessageDelayed(message, 3000); 																		}
			super.handleMessage(msg);
		}
	};

在onCreate方法里添加布局

aView = (TextView) findViewById(R.id.autoPlay);
aView1 = (TextView) findViewById(R.id.autoPlay1);
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // 发送message

3.autoPlay方法动画

public void autoPlay(TextView aView, TextView aView1) {
		TranslateAnimation inAnimation = new TranslateAnimation(0, 0, 100, 0);
		inAnimation.setDuration(2000);
		inAnimation.setFillAfter(true);
		TranslateAnimation outAnimation = new TranslateAnimation(0, 0, 0, -100);
		outAnimation.setDuration(2000);
		outAnimation.setFillAfter(true);
		aView1.clearAnimation();
		aView.clearAnimation();
		aView1.startAnimation(outAnimation);
		aView.startAnimation(inAnimation);
	}
相关文章
相关标签/搜索