本文实例为大家分享了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,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); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义textview实现竖直滚动跑马灯效果全部内容,希望文章能够帮你解决Android自定义textview实现竖直滚动跑马灯效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)