Android自定义textview实现竖直滚动跑马灯效果
程序员文章站
2023-10-27 17:58:58
本文实例为大家分享了android自定义textview实现跑马灯效果的具体代码,供大家参考,具体内容如下
xml布局
本文实例为大家分享了android自定义textview实现跑马灯效果的具体代码,供大家参考,具体内容如下
xml布局
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="myandroidstituds.test3.mainactivity"> <linearlayout android:layout_centerinparent="true" android:layout_width="180dp" android:layout_height="50dp"> <textview android:padding="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#55000000" /> </linearlayout> <myandroidstituds.test3.marqueelayout android:id="@+id/mar" android:layout_centerinparent="true" android:layout_width="180dp" android:layout_height="50dp"> <textview android:id="@+id/tv1" android:textsize="20sp" android:gravity="center" android:background="@android:color/transparent" android:layout_width="match_parent" android:layout_height="match_parent" android:textcolor="#000000" android:visibility="gone" android:text="两个黄鹂鸣翠柳" /> <textview android:id="@+id/tv2" android:textsize="20sp" android:background="@android:color/transparent" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:textcolor="#000000" android:visibility="gone" android:text="一行白鹭上青天" /> </myandroidstituds.test3.marqueelayout> </relativelayout>
自定义marqueelayout
public class marqueelayout extends framelayout implements viewtreeobserver.ongloballayoutlistener{ private view view1; private view view2; public int interval = 2000; public marqueelayout(context context) { this(context,null); } public marqueelayout(context context, attributeset attrs) { this(context, attrs,0); } public marqueelayout(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); getviewtreeobserver().addongloballayoutlistener(this); } @override protected void onfinishinflate() { super.onfinishinflate(); if(getchildcount()!=2){ throw new illegalargumentexception("marqueelayout should have 2 child!"); } view1 = getchildat(0); view2 = getchildat(1); } @override public void ongloballayout() { getviewtreeobserver().removeglobalonlayoutlistener(this); view2.settranslationy(view2.getheight()); } private handler handler = new handler(){ @override public void handlemessage(message msg) { super.handlemessage(msg); startanim(); } }; boolean isaniming = false; /** * 开始滚动 */ public void start(){ getviewtreeobserver().addongloballayoutlistener(new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { getviewtreeobserver().removeglobalonlayoutlistener(this); startanim(); } }); } public void startanim(){ if(isaniming)return; isaniming = true; int targety1 = view1.gettranslationy()==0?-view1.getheight():0; int targety2 = view2.gettranslationy()==0?-view2.getheight():0; viewcompat.animate(view1) .translationy(targety1) .setlistener(new viewpropertyanimatorlisteneradapter(){ @override public void onanimationend(view view) { super.onanimationend(view); //移动到下方 resetview(view); } }) .setduration(400).start(); viewcompat.animate(view2) .translationy(targety2) .setlistener(new viewpropertyanimatorlisteneradapter(){ @override public void onanimationend(view view) { super.onanimationend(view); //移动到下方 resetview(view); isaniming = false; handler.sendemptymessagedelayed(0,interval); } }) .setduration(400) .start(); } private void resetview(view view) { if(view.gettranslationy()==-view.getheight()){ view.settranslationy(view.getheight()*2); } } public void stop(){ handler.removecallbacksandmessages(null); } }
在activity中的使用
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); marqueelayout marqueelayout = (marqueelayout) findviewbyid(r.id.mar); textview tv1 = (textview) findviewbyid(r.id.tv1); textview tv2 = (textview) findviewbyid(r.id.tv2); marqueelayout.start(); tv1.setvisibility(tv1.gettranslationy() == 0 ? view.visible : view.gone); tv2.setvisibility(tv2.gettranslationy() == 0 ? view.visible : view.gone); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 无线网卡连接不上的图文解决方法
下一篇: 基于 Redis 实现 CAS 操作