字体滚动效果
最近在项目中遇见了字体水平滚动&字体垂直滚动效果,借此与大家分享
《跑马灯效果》
2、代码实现:
《垂直切换文字效果图》
2、代码实现:
public class VerticalSlidingView extends TextSwitcher implements ViewSwitcher.ViewFactory {
private static final String[] textContents = new String[]{"日照香炉生紫烟","遥看瀑布挂前川","飞流直下三千尺","疑似迎合落九天"};
public static final int MSG_START_SLIP = 1;
private Handler mHandler;
private int num;
private static final int TIME = 2 * 1000;
private Handler.Callback mCallBack = new Handler.Callback(){
@Override
public boolean handleMessage(Message message) {
switch (message.what){
case MSG_START_SLIP:
mHandler.sendEmptyMessageDelayed(MSG_START_SLIP , TIME);
if(num <= textContents.length -1) {
setText(textContents[num++]);
}else{
num = 0;
}
break;
}
return false;
}
};
public VerticalSlidingView(Context context){
this(context,null);
}
public VerticalSlidingView(Context context, AttributeSet attrs) {
super(context, attrs);
mHandler = new Handler(mCallBack);
initView();
}
@Override
public View makeView() {
TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.swith_text_view , null);
return textView;
}
private void initView(){
setFactory(this);
/*TextView textView1 = new TextView(getContext());
ViewGroup.LayoutParams params1 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT);
textView1.setTextSize(20);
textView1.setTextColor(Color.RED);
textView1.setLayoutParams(params1);
addView(textView1 , 0);
TextView textView2 = new TextView(getContext());
ViewGroup.LayoutParams params2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT);
textView2.setTextSize(30);
textView2.setTextColor(Color.BLACK);
textView2.setLayoutParams(params2);
addView(textView2 , 1);*/
Animation animationIn = loadAnimation(getContext() , R.anim.slide_in_top);
Animation animationOut = AnimationUtils.loadAnimation(getContext() , R.anim.slide_out_top);
setInAnimation(animationIn);
setOutAnimation(animationOut);
mHandler.sendEmptyMessageDelayed(MSG_START_SLIP,TIME);
if(num <= textContents.length - 1) {
setText(textContents[num++]);
}else{
num = 0;
}
}
}
***************************************************************************
《垂直切换字体颜色大小改变效果图》
代码上面也有我就不复制了,直接上步骤:
1、首先不设置setFactory,这样makeView()这个方法也就无法调用了,故makeView可以return null;
2、一定要初始化两个TextView不然系统会报错,然后addView(view , index)【index 通常是0,1】
3、如果是要改变TextView切换的margin则修改
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT , LinearLayout.LayoutParams.WRAP_CONTENT);
params1.setMargins();
4、如果在更新数据时发现布局混乱了,则可以试用这个方法:
removeAllView()然后再addView()【在更新地方设置】
这里我一个疑问,因为每次文字切换时,都会调用getNextView按理说这里可以修改下一个展示View的布局格式,但是我尝试了不行。不知各位是否有idea,也请你们告诉我,互相学习!!!