首页垂直滚动TextView广告效果,使用TextSwicher+animation实现

来源:互联网 发布:仙剑奇侠传mac无文字 编辑:程序博客网 时间:2024/06/05 16:12

转自:http://blog.csdn.net/chudaijiang/article/details/51595296

垂直滚动TextView广告效果 ,两种思路:1.以前在网上找到一个垂直滚动的自定义ViewPager ,这种的好处是它支持自己滚动。但同时它有很多的问题:首先它是一个自定义viewGroup,那onMeasure 测量就是个很复杂的问题,因为还要先测量子控件的宽高。2.今天要推荐的就是TextSwicher+animation 动画 实现,这种很符合我们看到这种效果的第一印象,是不是?而且思路很简单,很合理。这篇文章亲测没有问题,访问量很少,推一下;



1.布局xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <TextSwitcher  
  2.             android:id="@+id/profileSwitcher"  
  3.             android:layout_below="@id/ll_path_container"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="wrap_content"  
  6.             android:layout_margin="@dimen/s2"  
  7.             android:background="@drawable/map_top_bg_shape"  
  8.             android:inAnimation="@anim/push_up_in"  
  9.             android:minHeight="48dp"  
  10.             android:outAnimation="@anim/push_up_out"></TextSwitcher>  

2.代码

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //===========================================================  
  2.     @InjectView(R.id.profileSwitcher)  
  3.     TextSwitcher textSwitcher;  
  4.     private BitHandler bitHandler;  
  5.     private String[] strings={"text00001","text00002"};  
  6.     private int index = 0;  
  7.     private void test(){  
  8.         textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {  
  9.             @Override  
  10.             public View makeView() {  
  11.                 TextView textView = new TextView(mContext);  
  12.                 textView.setSingleLine();  
  13.                 textView.setTextSize(15);  
  14.                 textView.setTextColor(Color.parseColor("#ff0000"));  
  15.                 textView.setEllipsize(TextUtils.TruncateAt.END);  
  16.                 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(  
  17.                         ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT  
  18.                 );  
  19.                 lp.gravity = Gravity.CENTER;  
  20.                 textView.setLayoutParams(lp);  
  21.                 return textView;  
  22.             }  
  23.         });  
  24.         bitHandler = new BitHandler();  
  25.         new myThread().start();  
  26.     }  
  27.   
  28.     class BitHandler extends Handler {  
  29.   
  30.         @Override  
  31.         public void handleMessage(Message msg) {  
  32.             super.handleMessage(msg);  
  33.             textSwitcher.setText(strings[index]);  
  34.             index++;  
  35.             if (index == strings.length) {  
  36.                 index = 0;  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41.     private class myThread extends Thread {  
  42.         @Override  
  43.         public void run() {  
  44.             super.run();  
  45.             while (index < strings.length) {  
  46.                 try {  
  47.                     synchronized (this) {  
  48.                         bitHandler.sendEmptyMessage(0);  
  49.                         this.sleep(2000);  
  50.                     }  
  51.                 } catch (InterruptedException e) {  
  52.                     e.printStackTrace();  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  

3.anim

push_up_in

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shareInterpolator="false"  
  4.     android:zAdjustment="top">  
  5.     <translate  
  6.         android:duration="400"  
  7.         android:fromYDelta="100%"  
  8.         android:interpolator="@android:anim/accelerate_interpolator"  
  9.         android:toYDelta="0"  
  10.   
  11.         />  
  12.   
  13.     <alpha  
  14.         android:duration="400"  
  15.         android:fromAlpha="0.0"  
  16.         android:toAlpha="1.0" />  
  17. </set>  

push_up_out

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shareInterpolator="false"  
  4.     android:zAdjustment="bottom">  
  5.     <translate  
  6.         android:duration="400"  
  7.         android:fromYDelta="0"  
  8.         android:interpolator="@android:anim/accelerate_interpolator"  
  9.         android:toYDelta="-100%"  
  10.   
  11.         />  
  12.   
  13.     <alpha  
  14.         android:duration="400"  
  15.         android:fromAlpha="1.0"  
  16.         android:toAlpha="0.0" />  
  17. </set>  

实现效果:


注意:1.  InAnimation和OutAnimation 的时间和TextSwitch转换TextView的时间不冲突,不交叉,流程是这样的textSwitch隔段时间替换textView后执行动画,所以不交叉。2.   textSwitch的宽度要固定,不能wrapcontent   会影响动画的协调性,没有滚动的效果。3.如果你是在滚动中实现上述效果的时候 inanimation 和outanimation要在java代码中实现,而不是xml中,因为在xml中再可见时会新起一个动画,这样会出现文字重叠的错误。

0 0