有些应用里面会有一些上下滚动文字的广告,尤其是关于商城里面的容易出现,具体的效果相比大家也见过,先看看效果吧
这是怎么实现的呢,有人说是用TextView实现,有的说这是跑马灯效果。其实说TextView实现这种效果的同学只是说对了一半,这个效果的实现是离不开TextView的,因为文字的显示的大小和颜色都可以通过TextView来实现。但是只有TextView是万万不能实现的。这里还需要用到一种控件那就是TextSwitcher。TextSwitcher 字面理解是文本切换器,是ViewSwitcher的子类,ViewSwitcher的父类则是ViewAnimator,而ViewAnimator的父类又是FrameLayout。从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,TextView是View的子类,是用来交换TextView。ViewSwitcher 代表了视图切换组件, 又继承FrameLayout ,FrameLayout 的效果想必大家都知道,就是一层一层的将多个View叠在一起 ,每次只显示一个组件.由于ViewSwitcher 支持指定动画效果,当我们从一个View切换到另个View时,可以实现切换的动画效果。我们既然使用TextSwitcher进行文本切换,那么我们需要的文本肯定不是一个,于是TextSwitcher的父类ViewSwitcher给我们提供了一个ViewFactory()工厂方法,我们就可以使用ViewSwitcher的ViewFactory里面的makeView()方法创建View或添加自己创建的View。
这个上下滚动也可以实现无限的循环滚动的效果,虽然不用适配器去设置数量为int的最大值,但是我们可以设置从0开始,让数量一直增加到int的最大值效果是一样的。 具体的实现代码也不多,就把代码展示出来吧
MainActivity类
package com.lyxrobert.textshuffling;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity {
private ImageView img_notice;
private AnimationDrawable animationDrawable;
private TextSwitcher tv_notice;
private String[] mAdvertisements ;
private final int HOME_AD_RESULT = 1;
private int mSwitcherCount=0;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
// 广告
case HOME_AD_RESULT:
tv_notice.setText(mAdvertisements[mSwitcherCount % mAdvertisements.length]);
mSwitcherCount++;
mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3000);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
img_notice = (ImageView)findViewById(R.id.img_notice);
img_notice.setImageResource(R.drawable.notices);
animationDrawable = (AnimationDrawable)img_notice.getDrawable();
animationDrawable.start();
tv_notice = (TextSwitcher) findViewById(R.id.tv_notice);
tv_notice.setFactory(new ViewSwitcher.ViewFactory() {
// 这里用来创建内部的视图,这里创建TextView,用来显示文字
public View makeView() {
TextView tv = new TextView(getApplicationContext());
// 设置文字的显示单位以及文字的大小
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getResources()
.getDimension(R.dimen.font_size));
return tv;
}
});
tv_notice.setInAnimation(getApplicationContext(),
R.anim.slide_in_bottom);
tv_notice.setOutAnimation(getApplicationContext(), R.anim.slide_out_up);
mAdvertisements = new String[] { "海外助理服务,抢先体验","日本个签1799元三年多次","日本个签1999元五年多次" };
mHandler.sendEmptyMessage(HOME_AD_RESULT);
}
}
activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
tools:context="com.lyxrobert.textshuffling.MainActivity">
<ImageView
android:id="@+id/img_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/notices"/>
<TextSwitcher
android:id="@+id/tv_notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="24dp" />
</LinearLayout>
从底部进入的动画文件enter_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false"
android:zAdjustment="top"
>
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
</set>
从顶部离开的动画文件leave_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="false"
android:zAdjustment="top"
>
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%p" />
</set>
点击下载源码
具体的实现就是上面那么多。如果对你有帮助或者有疑问欢迎留言