Foreword:
Most of the current APPs, especially large-scale APPs like Taobao and Jingdong, have a text carousel interface, which realizes functions such as cyclically rotating multiple advertisement words; this space is commonly known as "marquee", and TextBannerView has realized vertical running. , A marquee that can run horizontally.
renderings
Source code download (star welcome): GitHub
Attributes property (called in the TextBannerView layout file)
Attributes | shape | describe |
---|---|---|
setInterval | integer | Text switching time interval, default 3000 |
setAnimDuration | integer | Animation duration, default 1500 |
setTextSize | dimension | set text size |
setTextColor | color | Set the text color, the default is black |
setSingleLine | boolean | Whether to display a single line |
setGravity | Text display position, the default left is centered; left, center, right can be set | |
setDirection | Text rotation direction, the default horizontal rotation from right to left: right_to_left; you can also set left_to_right (rotate from left to right), bottom_to_top (rotate from bottom to top), top_to_bottom (rotate from top to bottom) |
method:
method name | describe | Version restrictions |
---|---|---|
setDatas(List datas) | set data, type: List | without |
startViewAnimator () | Set start text switching (default automatic) | without |
stopViewAnimator() | Set Pause Text Toggle | without |
setItemOnClickListener(listener) | Set click listener event callback | without |
setDatasWithDrawableIcon() | Set data with icon; first parameter: data. The second parameter: drawable. The third parameter drawable size. Fourth parameter icon position (only Gravity.LEFT, Gravity.TOP, Gravity.RIGHT, Gravity.BOTTOM are supported) | 1.0.2 open beginning |
Steps for usage:
Step 1.依赖TextBannerView
Gradle
dependencies{
compile 'com.superluo:textbannerview:1.0.2' //最新版本
}
或者引用本地lib
compile project(':textbannerlibrary')
Maven
<dependency>
<groupId>com.superluo</groupId>
<artifactId>textbannerview</artifactId>
<version>1.0.2</version>
<type>pom</type>
</dependency>
Step 2.在布局文件中添加TextBannerView,可以设置自定义属性
<com.superluo.textbannerlibrary.TextBannerView
android:id="@+id/tv_banner"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="#cc8ac6"
app:setGravity="right"
app:setTextColor="#fff"/>
Step 3.在Activity或者Fragment中配置TextBannerView
//初始化TextBannerView
TextBannerView tvBanner = (TextBannerView) findViewById(R.id.tv_banner);
//设置数据
List<String> list = new ArrayList<>();
list.add("学好Java、Android、C#、C、ios、html+css+js");
list.add("走遍天下都不怕!!!!!");
list.add("不是我吹,就怕你做不到,哈哈");
list.add("superluo");
list.add("你是最棒的,奔跑吧孩子!");
//调用setDatas(List<String>)方法后,TextBannerView自动开始轮播
//注意:此方法目前只接受List<String>类型
tvBanner.setDatas(mList);
Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
/**这里可以设置带图标的数据(1.0.2新方法),比setDatas方法多了带图标参数;
第一个参数:数据 。
第二参数:drawable.
第三参数:drawable尺寸。
第四参数:图标位置仅支持Gravity.LEFT、Gravity.TOP、Gravity.RIGHT、Gravity.BOTTOM
*/
mTvBanner.setDatasWithDrawableIcon(mList,drawable,18, Gravity.LEFT);
//设置TextBannerView点击监听事件,返回点击的data数据, 和position位置
tvBanner.setItemOnClickListener(new ITextBannerItemClickListener() {
@Override
public void onItemClick(String data, int position) {
Log.i("点击了:",String.valueOf(position)+">>"+data);
}
});