Android自定义View实现广告信息上下滚动效果
程序员文章站
2024-03-01 18:32:28
先看看效果:
实现代码:
public class scrollbanner extends linearlayout {
private tex...
先看看效果:
实现代码:
public class scrollbanner extends linearlayout { private textview mbannertv1; private textview mbannertv2; private handler handler; private boolean isshow; private int starty1, endy1, starty2, endy2; private runnable runnable; private list<string> list; private int position = 0; private int offsety = 100; public scrollbanner(context context) { this(context, null); } public scrollbanner(context context, attributeset attrs) { this(context, attrs, 0); } public scrollbanner(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); view view = layoutinflater.from(context).inflate(r.layout.view_scroll_banner, this); mbannertv1 = (textview) view.findviewbyid(r.id.tv_banner1); mbannertv2 = (textview) view.findviewbyid(r.id.tv_banner2); handler = new handler(); runnable = new runnable() { @override public void run() { isshow = !isshow; if (position == list.size()) position = 0; if (isshow) { mbannertv1.settext(list.get(position++)); } else { mbannertv2.settext(list.get(position++)); } starty1 = isshow ? 0 : offsety; endy1 = isshow ? -offsety : 0; objectanimator.offloat(mbannertv1, "translationy", starty1, endy1).setduration(300).start(); starty2 = isshow ? offsety : 0; endy2 = isshow ? 0 : -offsety; objectanimator.offloat(mbannertv2, "translationy", starty2, endy2).setduration(300).start(); handler.postdelayed(runnable, 3000); } }; } public list<string> getlist() { return list; } public void setlist(list<string> list) { this.list = list; } public void startscroll() { handler.post(runnable); } public void stopscroll() { handler.removecallbacks(runnable); } }
view_scroll_banner.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="10dp"> <textview android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:text="公告" android:textcolor="#e76712" android:textsize="16sp" /> <view android:id="@+id/view_divider" android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:layout_torightof="@id/tv_title" android:background="#dddddd" /> <textview android:id="@+id/tv_banner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_torightof="@+id/view_divider" android:ellipsize="end" android:singleline="true" android:text="热烈欢迎领导光临" /> <textview android:id="@+id/tv_banner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centervertical="true" android:layout_torightof="@+id/view_divider" android:ellipsize="end" android:singleline="true" android:text="喜庆国庆欢乐大酬宾" /> </relativelayout>
使用方法:
scrollbanner.setlist(list); scrollbanner.startscroll();
以上就是本文的全部内容,希望对大家学习android软件编程有所帮助,也希望大家多多支持。