【Android之垂直翻页公告】
2016-06-14 11:41
288 查看
源码github地址是:https://github.com/sfsheng0322/MarqueeView
该源码是android studio版本,本文最后提供的是eclipse版本,以供大家学习参考。
效果如图:
源码分析:
核心类:MarqueeView.java
核心思想是根据公告内容的数量动态生成相应数量的TextView对象,将所有的TextView对象加入到MarqueeView对象中,并且为每一个TextView设置监听点击事件,然后翻页轮播。
该类继承了ViewFlipper <--- ViewAnimator <--- FrameLayout
该类常用的api如下:
setInAnimation() ---> 设置进场动画
setOutAnimation() ---> 设置出场动画
setFlipInterval() ---> 设置间隔时间
自定义属性attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MarqueeViewStyle">
<attr name="mvInterval" format="integer|reference" />
<attr name="mvAnimDuration" format="integer|reference" />
<attr name="mvTextSize" format="dimension|reference" />
<attr name="mvTextColor" format="color|reference" />
</declare-styleable>
</resources>
进场动画anim_marquee_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0"/>
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.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="500"
android:fromYDelta="0"
android:toYDelta="-100%p"/>
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
在layout文件中的定义:
<com.svse.ch01.views.MarqueeView
android:id="@+id/marqueeView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@+id/iv_loudspeaker1"
app:mvAnimDuration="1000"
app:mvInterval="3000"
app:mvTextColor="@color/white"
app:mvTextSize="14sp" />
在java代码中的调用:
MarqueeView marqueeView = (MarqueeView) findViewById(R.id.marqueeView1);
List<String> info = new ArrayList<String>();
info.add("1. 大家好,我是bear。");
info.add("2. 欢迎大家关注我哦!");
info.add("3. blog.csdn.net/bear_huangzhen");
marqueeView.startWithList(info);
整个使用起来还是很方便快捷的,感谢原作者的辛勤劳动。
下面是eclipse版本的示例源码,有需要的同学可以直接下载导入。
android之垂直翻页公告下载
该源码是android studio版本,本文最后提供的是eclipse版本,以供大家学习参考。
效果如图:
源码分析:
核心类:MarqueeView.java
核心思想是根据公告内容的数量动态生成相应数量的TextView对象,将所有的TextView对象加入到MarqueeView对象中,并且为每一个TextView设置监听点击事件,然后翻页轮播。
该类继承了ViewFlipper <--- ViewAnimator <--- FrameLayout
该类常用的api如下:
setInAnimation() ---> 设置进场动画
setOutAnimation() ---> 设置出场动画
setFlipInterval() ---> 设置间隔时间
自定义属性attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MarqueeViewStyle">
<attr name="mvInterval" format="integer|reference" />
<attr name="mvAnimDuration" format="integer|reference" />
<attr name="mvTextSize" format="dimension|reference" />
<attr name="mvTextColor" format="color|reference" />
</declare-styleable>
</resources>
进场动画anim_marquee_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0"/>
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.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="500"
android:fromYDelta="0"
android:toYDelta="-100%p"/>
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
在layout文件中的定义:
<com.svse.ch01.views.MarqueeView
android:id="@+id/marqueeView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@+id/iv_loudspeaker1"
app:mvAnimDuration="1000"
app:mvInterval="3000"
app:mvTextColor="@color/white"
app:mvTextSize="14sp" />
在java代码中的调用:
MarqueeView marqueeView = (MarqueeView) findViewById(R.id.marqueeView1);
List<String> info = new ArrayList<String>();
info.add("1. 大家好,我是bear。");
info.add("2. 欢迎大家关注我哦!");
info.add("3. blog.csdn.net/bear_huangzhen");
marqueeView.startWithList(info);
整个使用起来还是很方便快捷的,感谢原作者的辛勤劳动。
下面是eclipse版本的示例源码,有需要的同学可以直接下载导入。
android之垂直翻页公告下载
相关文章推荐
- android报找不到so的错误
- Data Binding Android - Type parameter T has incompatible upper bounds : ViewDataBinding and MainActi
- Android项目开发之UI设计器
- Android UDP连接发送请求和接受请求的例子
- Android与JS互相调用
- android快速索引的实现
- Android各种访问权限Permission详解
- Android_menu_SubMenu
- Android EditText默认数字键盘,能输入字母和数字
- Android_menu_optionMenu
- Android项目基本结构详解
- Android实现点击AlertDialog上按钮时不关闭对话框的方法
- 关于谷歌发布的750的开源图标
- Android_Menu_contextMenu
- Fragment 与 Activity 生命周期对比
- 【Android】Lambda表达式
- android 主线程是线程不安全的
- Android 内存泄露检查之StrictMode模式
- Android 基于Bmob的聊天——用户登陆+将用户信息保存至本地
- Android 6.0 双向通话自动录音