源码地址 http://download.csdn.net/detail/daweibalang717/9337789
另一种效果:http://blog.csdn.net/daweibalang717/article/details/51180792
自定义控件:
package cn.silent.view;
import java.util.ArrayList;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;
/**
* @author silentyang
* */
public class AutoTextLine extends FrameLayout {
private ArrayList<TextView> listView;
private Context mContext;
private TextView curTextView;
private TextView nextTextView;
private final int MSG_START = 0;
private final int MSG_NEXT = 1;
//多长时间一切换
private int delay = 5000;
//切换速度
private int duartion = 500;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_START:
start();
break;
case MSG_NEXT:
nextView();
break;
}
};
};
public AutoTextLine(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public AutoTextLine(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
public void init(Context context) {
listView = new ArrayList<>();
mContext = context;
}
public void SetData(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
TextView txt = getView(list.get(i));
if (i == list.size() - 1) {//最后一个VIEW
txt.setTag(0);
} else {
txt.setTag(i + 1);
}
listView.add(txt);
}
if (listView.size() > 1) {
//获取第一个view 并立即开始
curTextView = listView.get(0);
nextTextView = listView.get(1);
addView(curTextView);
mHandler.sendEmptyMessage(MSG_START);
} else if (listView.size() == 1) {
//只有一个TextView
curTextView = listView.get(0);
addView(curTextView);
}
}
public TextView getView(String string) {
TextView txt = new TextView(mContext);
txt.setText(string);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
txt.setLayoutParams(lp);
txt.setGravity(Gravity.CENTER);
return txt;
}
public void start() {
//addView(curTextView);
addView(nextTextView);
// 从父窗口的(0,0)的位置移动父窗口X轴0%Y轴-120%的距离
TranslateAnimation out = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, -1.2f);
//TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 100);
out.setDuration(duartion);
/*anim.setRepeatCount(2);
anim.setRepeatMode(Animation.REVERSE);*/
out.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
//TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 0);
//curTextView.setAnimation(anim);
removeView(curTextView);
mHandler.sendEmptyMessageDelayed(MSG_NEXT, delay);
}
});
TranslateAnimation in = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1.2f, Animation.RELATIVE_TO_PARENT, 0);
in.setDuration(duartion);
curTextView.startAnimation(out);
nextTextView.startAnimation(in);
}
public void nextView() {
curTextView = nextTextView;
int nextIndex = (int) curTextView.getTag();
nextTextView = listView.get(nextIndex);
start();
}
public void stop() {
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
public int getDuartion() {
return duartion;
}
public void setDuartion(int duartion) {
this.duartion = duartion;
}
}
调用方式:
package com.example.autoscroll;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import cn.silent.view.AutoTextLine;
/**
* @author silent yang
*
* */
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoTextLine auto = (AutoTextLine) findViewById(R.id.auto_txt);
ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 2; i++) {
data.add("标题\n 内容:今天雾霾" + i);
}
auto.SetData(data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<cn.silent.view.AutoTextLine
android:id="@+id/auto_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
</cn.silent.view.AutoTextLine>
</LinearLayout>
不会做效果图,大概意思到了就行了。 运行滚动其实很平缓: