首页垂直滚动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
- <TextSwitcher
- android:id="@+id/profileSwitcher"
- android:layout_below="@id/ll_path_container"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/s2"
- android:background="@drawable/map_top_bg_shape"
- android:inAnimation="@anim/push_up_in"
- android:minHeight="48dp"
- android:outAnimation="@anim/push_up_out"></TextSwitcher>
2.代码-
- @InjectView(R.id.profileSwitcher)
- TextSwitcher textSwitcher;
- private BitHandler bitHandler;
- private String[] strings={"text00001","text00002"};
- private int index = 0;
- private void test(){
- textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
- @Override
- public View makeView() {
- TextView textView = new TextView(mContext);
- textView.setSingleLine();
- textView.setTextSize(15);
- textView.setTextColor(Color.parseColor("#ff0000"));
- textView.setEllipsize(TextUtils.TruncateAt.END);
- FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
- ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
- );
- lp.gravity = Gravity.CENTER;
- textView.setLayoutParams(lp);
- return textView;
- }
- });
- bitHandler = new BitHandler();
- new myThread().start();
- }
-
- class BitHandler extends Handler {
-
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- textSwitcher.setText(strings[index]);
- index++;
- if (index == strings.length) {
- index = 0;
- }
- }
- }
-
- private class myThread extends Thread {
- @Override
- public void run() {
- super.run();
- while (index < strings.length) {
- try {
- synchronized (this) {
- bitHandler.sendEmptyMessage(0);
- this.sleep(2000);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
3.animpush_up_in
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false"
- android:zAdjustment="top">
- <translate
- android:duration="400"
- android:fromYDelta="100%"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:toYDelta="0"
-
- />
-
- <alpha
- android:duration="400"
- android:fromAlpha="0.0"
- android:toAlpha="1.0" />
- </set>
push_up_out- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:shareInterpolator="false"
- android:zAdjustment="bottom">
- <translate
- android:duration="400"
- android:fromYDelta="0"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:toYDelta="-100%"
-
- />
-
- <alpha
- android:duration="400"
- android:fromAlpha="1.0"
- android:toAlpha="0.0" />
- </set>
实现效果:注意:1. InAnimation和OutAnimation 的时间和TextSwitch转换TextView的时间不冲突,不交叉,流程是这样的textSwitch隔段时间替换textView后执行动画,所以不交叉。2. textSwitch的宽度要固定,不能wrapcontent 会影响动画的协调性,没有滚动的效果。3.如果你是在滚动中实现上述效果的时候 inanimation 和outanimation要在java代码中实现,而不是xml中,因为在xml中再可见时会新起一个动画,这样会出现文字重叠的错误。
0 0