查看: 439|回复: 0

[Android教程] Android自定义textview实现竖直滚动跑马灯效果

发表于 2017-11-28 08:00:01

本文实例为大家分享了Android自定义textview实现跑马灯效果的具体代码,供大家参考,具体内容如下

xml布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/activity_main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context="myandroidstituds.test3.MainActivity">
  9. <LinearLayout
  10. android:layout_centerInParent="true"
  11. android:layout_width="180dp"
  12. android:layout_height="50dp">
  13. <TextView
  14. android:padding="10dp"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:background="#55000000"
  18. />
  19. </LinearLayout>
  20. <myandroidstituds.test3.MarqueeLayout
  21. android:id="@+id/mar"
  22. android:layout_centerInParent="true"
  23. android:layout_width="180dp"
  24. android:layout_height="50dp">
  25. <TextView
  26. android:id="@+id/tv1"
  27. android:textSize="20sp"
  28. android:gravity="center"
  29. android:background="@android:color/transparent"
  30. android:layout_width="match_parent"
  31. android:layout_height="match_parent"
  32. android:textColor="#000000"
  33. android:visibility="gone"
  34. android:text="两个黄鹂鸣翠柳"
  35. />
  36. <TextView
  37. android:id="@+id/tv2"
  38. android:textSize="20sp"
  39. android:background="@android:color/transparent"
  40. android:gravity="center"
  41. android:layout_width="match_parent"
  42. android:layout_height="match_parent"
  43. android:textColor="#000000"
  44. android:visibility="gone"
  45. android:text="一行白鹭上青天"
  46. />
  47. </myandroidstituds.test3.MarqueeLayout>
  48. </RelativeLayout>
复制代码

自定义MarqueeLayout

  1. public class MarqueeLayout extends FrameLayout implements ViewTreeObserver.OnGlobalLayoutListener{
  2. private View view1;
  3. private View view2;
  4. public int interval = 2000;
  5. public MarqueeLayout(Context context) {
  6. this(context,null);
  7. }
  8. public MarqueeLayout(Context context, AttributeSet attrs) {
  9. this(context, attrs,0);
  10. }
  11. public MarqueeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  12. super(context, attrs, defStyleAttr);
  13. getViewTreeObserver().addOnGlobalLayoutListener(this);
  14. }
  15. @Override
  16. protected void onFinishInflate() {
  17. super.onFinishInflate();
  18. if(getChildCount()!=2){
  19. throw new IllegalArgumentException("MarqueeLayout should have 2 child!");
  20. }
  21. view1 = getChildAt(0);
  22. view2 = getChildAt(1);
  23. }
  24. @Override
  25. public void onGlobalLayout() {
  26. getViewTreeObserver().removeGlobalOnLayoutListener(this);
  27. view2.setTranslationY(view2.getHeight());
  28. }
  29. private Handler handler = new Handler(){
  30. @Override
  31. public void handleMessage(Message msg) {
  32. super.handleMessage(msg);
  33. startAnim();
  34. }
  35. };
  36. boolean isAniming = false;
  37. /**
  38. * 开始滚动
  39. */
  40. public void start(){
  41. getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  42. @Override
  43. public void onGlobalLayout() {
  44. getViewTreeObserver().removeGlobalOnLayoutListener(this);
  45. startAnim();
  46. }
  47. });
  48. }
  49. public void startAnim(){
  50. if(isAniming)return;
  51. isAniming = true;
  52. int targetY1 = view1.getTranslationY()==0?-view1.getHeight():0;
  53. int targetY2 = view2.getTranslationY()==0?-view2.getHeight():0;
  54. ViewCompat.animate(view1)
  55. .translationY(targetY1)
  56. .setListener(new ViewPropertyAnimatorListenerAdapter(){
  57. @Override
  58. public void onAnimationEnd(View view) {
  59. super.onAnimationEnd(view);
  60. //移动到下方
  61. resetView(view);
  62. }
  63. })
  64. .setDuration(400).start();
  65. ViewCompat.animate(view2)
  66. .translationY(targetY2)
  67. .setListener(new ViewPropertyAnimatorListenerAdapter(){
  68. @Override
  69. public void onAnimationEnd(View view) {
  70. super.onAnimationEnd(view);
  71. //移动到下方
  72. resetView(view);
  73. isAniming = false;
  74. handler.sendEmptyMessageDelayed(0,interval);
  75. }
  76. })
  77. .setDuration(400)
  78. .start();
  79. }
  80. private void resetView(View view) {
  81. if(view.getTranslationY()==-view.getHeight()){
  82. view.setTranslationY(view.getHeight()*2);
  83. }
  84. }
  85. public void stop(){
  86. handler.removeCallbacksAndMessages(null);
  87. }
  88. }
复制代码

在Activity中的使用

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. MarqueeLayout marqueeLayout = (MarqueeLayout) findViewById(R.id.mar);
  7. TextView tv1 = (TextView) findViewById(R.id.tv1);
  8. TextView tv2 = (TextView) findViewById(R.id.tv2);
  9. marqueeLayout.start();
  10. tv1.setVisibility(tv1.getTranslationY() == 0 ? View.VISIBLE : View.GONE);
  11. tv2.setVisibility(tv2.getTranslationY() == 0 ? View.VISIBLE : View.GONE);
  12. }
  13. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条