使用TextSwitcher打造竖向滚动广告

TextSwitcher 、 viewFilpper 、 imageSwitcher 比较

运行效果

一、 继承体系

二.TextSwitcher的具体使用步骤

应用分为三步:

1. 在需要使用的地方添加 TextSwitcher 控件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent" >  

        <!-- 定义一个TextSwitcher,并制定了文本切换时的动画效果 -->  

        <TextSwitcher  
            android:id="@+id/textSwitcher"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:textAlignment="center"  
            android:layout_centerHorizontal="true"  
            android:layout_centerVertical="true"  
            android:inAnimation="@android:anim/slide_in_left"  
            android:outAnimation="@android:anim/slide_out_right"   
            android:onClick="next"  
            >  
        </TextSwitcher>  

</RelativeLayout>  

2.得到 TextSwitcher 实例对象

    TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher); 

3.为switcher指定ViewSwitcher.ViewFactory工厂,该工厂会产生出转换时需要的View

  textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

        @Override
        public View makeView() {   //在这里返回需要定义的view的样式,注意最终必须是 TextView 的子类
            TextView tv = new TextView(SettingActivity.this);
            tv.setTextSize(40);
            // 字体颜色品红
            tv.setTextColor(Color.MAGENTA);
            return tv;
        }
    });

4.为switcher设定显示的内容,该方法执行,就会切换到下个View

 switcher.setText(String.valueOf(new Random().nextInt()));

调用这个方法可以直接切换下一个view

TextSwitcher 一些常用的方法

参数名或方法名 描述 入参类型 出参类型
setCurrentText() 给当前的testview设置文本 String
setText() 切换下一个textview并设置文本 void
addView() 向队列中添加一个TextView void
setInAnimation() TextView为TextSwitcher添加切入动画 void
setOutAnimation() TextView为TextSwitcher添加切出动画 void

运行效果

5. 高级功能 设置 TextSwitcher 移入和移出的动画

第一种方式 xml文件定义的方式

在anim中定义动画文件,存放在anim文件夹中

anim_marquee_in.xml 文件

  <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="1500"
            android:fromYDelta="100%p"
            android:toYDelta="0"/>
    </set>

anim_marquee_out.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="1500"
            android:fromYDelta="0"
            android:toYDelta="-100%p"/>
    </set>

布局文件中引用动画文件

     android:inAnimation="@android:anim/slide_in_left"  
     android:outAnimation="@android:anim/slide_out_right"  

第二种 在java文件中通过 setInAnimation()、setInAnimation() 方法设置切入切出动画

5.1 先定义动画

        /**   
         * 定义从右侧进入的动画效果   
         * @return   
         */    
        protected Animation inFromRightAnimation() {    
            Animation inFromRight = new TranslateAnimation(    
                    Animation.RELATIVE_TO_PARENT, +1.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f);    
            inFromRight.setDuration(200);    
            inFromRight.setInterpolator(new AccelerateInterpolator());    
            return inFromRight;    
        }    

        /**   
         * 定义从左侧退出的动画效果   
         * @return   
         */    
        protected Animation outToLeftAnimation() {    
            Animation outtoLeft = new TranslateAnimation(    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, -1.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f);    
            outtoLeft.setDuration(200);    
            outtoLeft.setInterpolator(new AccelerateInterpolator());    
            return outtoLeft;    
        }    

        /**   
         * 定义从左侧进入的动画效果   
         * @return   
         */    
        protected Animation inFromLeftAnimation() {    
            Animation inFromLeft = new TranslateAnimation(    
                    Animation.RELATIVE_TO_PARENT, -1.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f);    
            inFromLeft.setDuration(200);    
            inFromLeft.setInterpolator(new AccelerateInterpolator());    
            return inFromLeft;    
        }    

        /**   
         * 定义从右侧退出时的动画效果   
         * @return   
         */    
        protected Animation outToRightAnimation() {    
            Animation outtoRight = new TranslateAnimation(    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, +1.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f,    
                    Animation.RELATIVE_TO_PARENT, 0.0f);    
            outtoRight.setDuration(200);    
            outtoRight.setInterpolator(new AccelerateInterpolator());    
            return outtoRight;    
        }    

     }  

猜你喜欢

转载自blog.csdn.net/bencheng06/article/details/80621279