您的位置:首页 > 其它

自动垂直滚动(autoText)II

2016-04-18 16:49 295 查看
自动垂直滚动(autoText)一,参见:http://blog.csdn.net/daweibalang717/article/details/50221865
自定义控件:

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.LinearLayout;
import android.widget.TextView;

/**
* @author silentyang
* 博客地址:http://blog.csdn.net/daweibalang717/article/details/50221865
* */
public class AutoTextLineTwo extends LinearLayout {

private ArrayList<TextView> listView;

//当前要ADD的集合
private ArrayList<TextView> curListView;

private Context mContext;

private int curTextPosition;

private int nextTextPosition;

private int MaxShow;

private final int MSG_START_ADD = 0;

private final int MSG_START_MOVE = 1;

private final int MSG_NEXT = 2;

//多长时间一切换
private int delay = 2000;

//切换速度
private int duartion = 500;

private LinearLayout mLinearLayout;

private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_START_ADD:
startAdd();
break;

case MSG_START_MOVE:
startMove();
break;

case MSG_NEXT:
nextView();
break;
}

};
};

public AutoTextLineTwo(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}

public AutoTextLineTwo(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}

public void init(Context context) {
listView = new ArrayList<>();
curListView = new ArrayList<>();
mContext = context;
}

public void SetData(ArrayList<String> list) {
//最大显示数为小于1,没有意义
if (MaxShow < 1) {
return;
}
mLinearLayout = new LinearLayout(mContext);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mLinearLayout.setLayoutParams(lp);
mLinearLayout.setGravity(Gravity.CENTER);
mLinearLayout.setOrientation(VERTICAL);
addView(mLinearLayout);
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() <= MaxShow) {
//获取第一个view 并立即开始
curTextPosition = 0;

for (int i = 0; i <= listView.size(); i++) {
TextView item = listView.get(i);
curListView.add(item);
}

mHandler.sendEmptyMessage(MSG_START_ADD);

} else {
curTextPosition = 0;
for (int i = 0; i < MaxShow; i++) {
TextView item = listView.get(i);
curListView.add(item);
}
mHandler.sendEmptyMessage(MSG_START_ADD);
}

}

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 startAdd() {
mLinearLayout.addView(curListView.get(curTextPosition));
nextTextPosition = ++curTextPosition;
//如果下一个索引,大于等于 当前要ADD的数据集合,那么就跳出
if (nextTextPosition > curListView.size()) {
return;
}
if (nextTextPosition < MaxShow) {
mHandler.sendEmptyMessageDelayed(MSG_START_ADD, delay);
} else {
mHandler.sendEmptyMessageDelayed(MSG_START_MOVE, delay);
}

}

public void startMove() {
//如果下一个索引,大于等于 数据集合,那么就跳出
if (nextTextPosition >= listView.size()) {
return;
}

//TextView outText = curListView.get(0);
// 从父窗口的(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.0f / mLinearLayout.getChildCount()));
//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);
mLinearLayout.removeView(curListView.get(0));
curListView.remove(0);
TextView inToText = listView.get(nextTextPosition);
mLinearLayout.addView(inToText);
curListView.add(inToText);
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);
//outText.startAnimation(out);
mLinearLayout.startAnimation(out);
//inToText.startAnimation(in);
}

public void nextView() {

nextTextPosition++;

startMove();

}

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;
}

public int getMaxShow() {
return MaxShow;
}

public void setMaxShow(int maxShow) {
MaxShow = maxShow;
}

}


调用方式:

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.AutoTextLineTwo;

/**
* @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 < 10; i++) {
data.add("标题\n 内容:今天雾霾" + i);
}
auto.SetData(data);*/

AutoTextLineTwo auto = (AutoTextLineTwo) findViewById(R.id.auto_txt_two);
ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
data.add("标题\n 内容:今天雾霾" + i);
}
auto.setMaxShow(2);
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);
}
}

<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.AutoTextLineTwo
android:background="@color/accent_material_dark"
android:id="@+id/auto_txt_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
</cn.silent.view.AutoTextLineTwo>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: