转载请说明出处:http://blog.csdn.net/ddxxii/article/details/50890488
demo:http://download.csdn.net/detail/ddxxii/9461516
前些时间由于要使用到垂直滚动的textview,然而网络上查询来的呢是用画笔画的之类的,没有达到想要的类似某宝主界面文字通知滚动效果。所以就自己写了下,今天分享给大家,相信大家可以扩展更多。
public class VerticalScrollTextView extends RelativeLayout{
private TextView tv1;
private TextView tv2;
private String textColor="#398DEE";
private float textSize=20;
private ArrayList<String> list=new ArrayList<String>();
public VerticalScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VerticalScrollTextView(Context context) {
super(context);
init();
}
public VerticalScrollTextView(Context context, AttributeSet attr, int i) {
super(context, attr, i);
init();
}
public void setTime(ArrayList list) {
this.list=list;
tv1.setText(""+list.get(0));
tv2.setText(""+list.get(1));
}
private void init() {
RelativeLayout.LayoutParams lpLtv=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
lpLtv.addRule(RelativeLayout.CENTER_IN_PARENT);
tv1=new TextView(getContext());
tv1.setGravity(Gravity.CENTER);
tv1.setTextColor(Color.parseColor(textColor));
tv1.setMaxLines(1);
tv1.setTextSize(textSize);
tv1.setLayoutParams(lpLtv);
tv2=new TextView(getContext());
tv2.setGravity(Gravity.CENTER);
tv2.setTextColor(Color.parseColor(textColor));
tv2.setMaxLines(1);
tv2.setTextSize(textSize);
tv2.setLayoutParams(lpLtv);
this.addView(tv2);
this.addView(tv1);
}
private Handler handler=new Handler();
private boolean isShowTime=true;
ObjectAnimator animator2;
ObjectAnimator animator1;
private Runnable r=new Runnable() {
@Override
public void run() {
float fY=-getHeight()/2-15;
if(isShowTime) {
animator1 = ObjectAnimator.ofFloat(tv1, "translationY", 0, fY);
}else {
animator1 = ObjectAnimator.ofFloat(tv2, "translationY", 0, fY);
}
animator1.setDuration(3000);
animator1.start();
float llCurTranslationY=-fY;
if(isShowTime) {
animator2 = ObjectAnimator.ofFloat(tv2, "translationY",llCurTranslationY,0);
}else {
animator2 = ObjectAnimator.ofFloat(tv1, "translationY",llCurTranslationY,0);
}
animator2.setDuration(3000);
animator2.start();
if(isShowTime) {
isShowTime=false;
}else {
isShowTime=true;
}
handler.postDelayed(r, 5*1000);
}
};
private int isOneInit=1;//初始化view动画开始
public void starAni() {
if(isOneInit==1) {
handler.post(r);
isOneInit++;
}
}
}
这里呢主要是有一个relativelayout来实现的,设置动画,规定时间循环滚动。
然后给程序提供一个接口供开始滚动: starAni();
使用就在xml文件里引入,不必多说。
<myweiget.VerticalScrollTextView
android:id="@+id/ll_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_main"
android:orientation="vertical"
android:text="@string/hello_world" />
接下来我们看主界面:
public class MainActivity extends Activity {
private VerticalScrollTextView ll_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll_text=(VerticalScrollTextView) findViewById(R.id.ll_text);
}
@Override
protected void onResume() {
super.onResume();
}
private int FocusMy=1;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus&&FocusMy==1) {
handler.post(rSetText);
ll_text.starAni();
FocusMy++;
}
}
private ArrayList<String> list=new ArrayList<String>();
/*
*设置时间
*/
private void setText() {
if(list.size()==0) {
list.add("你好");
list.add("大家好才是真的好");
}
ll_text.setTime(list);
}
private Handler handler=new Handler();
private Runnable rSetText=new Runnable() {
@Override
public void run() {
setText();
handler.postDelayed(rSetText, 1000);
}
};
}
我们调用到了onWindowFocusChanged()这个方法,相信不用我说明大家就知道这个方法的作用,这里主要是为了获取控件高度,因为在这个之前,是view里面获取不到控件高度的。但是为了防止重复调用,我们需要手动添加参数。
完成,现在这个只是个比较简单的textview垂直滚动例子 ,提供了两个textview的切换,有思想才有进步,这还可以扩充到多个view的动态添加和滑动。