需求:实现文字无缝连接无限循环播放,动态添加布局。
代码如下:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
</RelativeLayout>
MainActivity.java:
public class MainActivity extends Activity {
RelativeLayout rl;
int count = 2;//两个文字滚动布局
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//创建布局对象
rl = (RelativeLayout)findViewById(R.id.relativelayout);
rl.removeAllViews();
for(int i = 1; i <= count;i++){
//将HorseView1加入布局中
HorseView horseview1 = new HorseView(this);
rl.addView(horseview1);
//设置HorseView的布局属性
RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)horseview1.getLayoutParams();
layoutParams.setMargins(0, count*150-i*150, 0, 0);
layoutParams.width = 800;
layoutParams.height = 150;
horseview1.setLayoutParams(layoutParams);
horseview1.setScrollWidth(layoutParams.width); //滚动框的宽度
horseview1.setCoordinateY(150/2 + 50/2);
horseview1.setCurrentPosition(layoutParams.width);//设置滚动信息从右边出来
horseview1.setSpeed(4); //文字播放速度
horseview1.setColor(Color.parseColor(“#FFFF00”));
horseview1.setTextSize(50);
horseview1.setText("三号路依然长得没有尽头,梧桐荫还是枝繁叶茂,人间骄阳刚好,风过林梢,彼时他们正当年少。");
horseview1.setIndex(i);
horseview1.start();
}
}
}
创建一个HorseView类继承TextView,在HorseView中实现文字绘画。
public class HorseView extends TextView{
private boolean stopMarquee;//是否停止滚动
private String text;//文本内容
private float coordinateX;//当前滚动位置
private float coordinateY;//当前滚动位置
private float textWidth;//文本宽度
private int scrollWidth;//滚动区域宽度
private int speed;//滚动速度
private int index=0;
private int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public float getCurrentPosition(){
return coordinateX;
}
public void setCurrentPosition(float coordinateX){
this.coordinateX=coordinateX;
}
public float getCoordinateY() {
return coordinateY;
}
public void setCoordinateY(float coordinateY) {
this.coordinateY = coordinateY;
}
public int getScrollWidth() {
return scrollWidth;
}
public void setScrollWidth(int scrollWidth) {
this.scrollWidth = scrollWidth;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public HorseView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void setText(String text){
this.text=text;
textWidth =getPaint().measureText(text);
}
public void start(){
int offsettime=0;
offsettime=(index-1)*scrollWidth/speed*30; //第一个字从开始的位置滚动到滚动框最左边的时间
Log.d(VIEW_LOG_TAG, "=============offsettime is "+offsettime);
handler.sendEmptyMessageDelayed(0, offsettime);
}
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case 0:
//当文字走到最左边
if(coordinateX < (- textWidth - scrollWidth)){
coordinateX = 0;//文字滚动完了,重新开始滚动
invalidate();//无限循环
if(!stopMarquee){
sendEmptyMessageDelayed(0, 30);
}
}
else{
coordinateX -= speed;//开始文字滚动
invalidate();
if(!stopMarquee){
sendEmptyMessageDelayed(0, 30);
}
}
break;
}
super.handleMessage(msg);
}
};
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
if(!isEmpty(text)){
Log.d(VIEW_LOG_TAG, "=============draw index is "+index+"==============");
Paint p=getPaint();
p.setColor(getColor());
if(coordinateX < (- (textWidth - scrollWidth))){
text += text;
textWidth =getPaint().measureText(text);
invalidate();
}
textWidth =getPaint().measureText(text);
canvas.drawText(text, coordinateX, coordinateY, p);
}
}
}