Android 仿网易新闻v3.5:上下滑动的引导页

       在很多天气或者新闻的应用中,我们都能看到一些字幕滚动的效果,最简单的实现为跑马灯效果,用系统提供的属性即可实现. 复杂一些的就需要自己去用自定义控件实现. 比如 让TextView 实现垂直滚动. 这里我要讲的是垂直滚动的字幕效果,并且内容并不仅为文字,还可以加入图片或者其他元素. 废话不多说,还是直接上效果图:

                                 

  

首先还是看一下核心的实现:

目前我的做法是重写了ScrollView,对外提供几个重要的方法:

isScrolled()方法判断当前是否为滚动状态

setScrolled(boolean flag)设置滚动的开关

setPeriod(long period)设置从开始滚动到结束的时间

setSpeed(long speed)设置滚动的速度

下面说一些需要注意的地方:

1.由于是定时操作,所以需要在Activity的对应生命周期进行处理: 当界面由不可见到可见时,设置setScrolled(true)打开滚动开关,由可见到不可见时,setScrolled(false)

关闭开关

2. 可根据自己需要调用setPeriod(long period)setSpeed(long speed)控制滚动的速度

3. 由于是ScrollView实现的,中间放置的内容同ScrollView,不仅仅可以设置文字,还可以添加图片等其他元素,实现复杂的UI

4. 图文混排, 目前这个DEMO我还没做细致处理. 最主要的部分就是文字的处理,需要考虑中英文,全角半角,字体大小,段落处理,计算对应的字符宽高等进行排版

    图片等资源处理的部分就相对要简单,主要处理分辨率与计算宽高

    关于这些部分,之后我会慢慢做细致讲解.

这个Demo是我临时写的,UI和图文混排包括具体的滚动部分处理都相对简单,大家可以在这个例子的基础上进行扩展,根据需求做出自己想要的效果:

Demo下载地址:http://download.csdn.net/detail/t12x3456/5875157

下面是对应的代码:

首先是自定义View:

  1. package com.tony.autoscroll;  
  2.   
  3.   
  4. import android.content.Context;  
  5. import android.os.Handler;  
  6. import android.util.AttributeSet;  
  7. import android.util.Log;  
  8. import android.view.MotionEvent;  
  9. import android.widget.ScrollView;  
  10.   
  11.   
  12. /** 
  13.  * @author Tony 
  14.  * 
  15.  */  
  16. public class AutoScrollView extends ScrollView {  
  17.     private final Handler handler = new Handler();  
  18.      private long duration     = 50;  
  19.      private boolean isScrolled   = false;  
  20.     private int currentIndex = 0;  
  21.     private long period = 1000;  
  22.     private int  currentY = -1;  
  23.     private double  x;  
  24.     private double  y;  
  25.     private int type = -1;  
  26.     /** 
  27.      * @param context 
  28.       */  
  29.      public AutoScrollView(Context context) {  
  30.         this(context, null);  
  31.     }  
  32.       
  33.      /** 
  34.       * @param context 
  35.      * @param attrs 
  36.       */  
  37.     public AutoScrollView(Context context, AttributeSet attrs) {  
  38.          this(context, attrs, 0);  
  39.     }  
  40.       
  41.     /** 
  42.       * @param context 
  43.        * @param attrs 
  44.        * @param defStyle 
  45.        */  
  46.     public AutoScrollView(Context context, AttributeSet attrs, int defStyle) {  
  47.         super(context, attrs, defStyle);  
  48.      }  
  49.       
  50.     public boolean onTouchEvent(MotionEvent event) {  
  51.          int Action = event.getAction();  
  52.         switch (Action) {  
  53.             case MotionEvent.ACTION_DOWN:  
  54.                 x=event.getX();  
  55.                 y=event.getY();  
  56.                  if (type == 0) {  
  57.                      setScrolled(false);  
  58.                                 }  
  59.                  break;  
  60.              case MotionEvent.ACTION_MOVE:  
  61.                 double moveY = event.getY() - y;  
  62.                  double moveX = event.getX() - x;  
  63.                    
  64.                 if ((moveY>20||moveY<-20) && (moveX < 50 || moveX > -50) && getParent() != null) {  
  65.                      getParent().requestDisallowInterceptTouchEvent(true);    
  66.                                  }  
  67.               
  68.                  break;  
  69.             case MotionEvent.ACTION_UP:  
  70.                  if (type == 0) {  
  71.                      currentIndex = getScrollY();  
  72.                     setScrolled(true);  
  73.                 }  
  74.                 break;  
  75.             default:  
  76.                 break;  
  77.         }  
  78.                   return super.onTouchEvent(event);    
  79.        }  
  80.          @Override    
  81.          public boolean onInterceptTouchEvent(MotionEvent p_event)    
  82.         {    
  83.            
  84.             return true;    
  85.         }    
  86.     /** 
  87.       * 判断当前是否为滚动状态 
  88.       *  
  89.       * @return the isScrolled 
  90.       */  
  91.      public boolean isScrolled() {  
  92.         return isScrolled;  
  93.      }  
  94.       
  95.      /** 
  96.       * 开启或者关闭自动滚动功能 
  97.       *  
  98.       * @param isScrolled true为开启,false为关闭 
  99.       */  
  100.      public void setScrolled(boolean isScrolled) {  
  101.         this.isScrolled = isScrolled;  
  102.         autoScroll();  
  103.      }  
  104.       
  105.      /** 
  106.       * 获取当前滚动到结尾时的停顿时间,单位:毫秒 
  107.       *  
  108.       * @return the period 
  109.       */  
  110.     public long getPeriod() {  
  111.         return period;  
  112.      }   
  113.       
  114.     /** 
  115.       * 设置当前滚动到结尾时的停顿时间,单位:毫秒 
  116.       *  
  117.       * @param period 
  118.       *  the period to set 
  119.      */  
  120.     public void setPeriod(long period) {  
  121.          this.period = period;  
  122.      }  
  123.       
  124.       /** 
  125.       * 获取当前的滚动速度,单位:毫秒,值越小,速度越快。 
  126.        *  
  127.       * @return the speed 
  128.       */  
  129.      public long getSpeed() {  
  130.         return duration;  
  131.      }  
  132.       
  133.      /** 
  134.       * 设置当前的滚动速度,单位:毫秒,值越小,速度越快。 
  135.       *  
  136.       * @param speed 
  137.       *            the duration to set 
  138.       */  
  139.      public void setSpeed(long speed) {  
  140.          this.duration = speed;  
  141.      }  
  142.      public void setType(int type){  
  143.          this.type = type;  
  144.      }  
  145.      private void autoScroll() {  
  146.          handler.postDelayed(new Runnable() {  
  147.             @Override  
  148.              public void run() {  
  149.                 boolean flag = isScrolled;  
  150.                  if (flag) {  
  151.                     if (currentY == getScrollY()) {  
  152.                          try {  
  153.                             Thread.sleep(period);  
  154.                         } catch (InterruptedException e) {  
  155.                             e.printStackTrace();  
  156.                         }  
  157.                         currentIndex = 0;  
  158.                          scrollTo(0, 0);  
  159.                         handler.postDelayed(this, period);  
  160.                     } else {  
  161.                         currentY = getScrollY();  
  162.                         handler.postDelayed(this, duration);  
  163.                         currentIndex++;  
  164.                         scrollTo(0, currentIndex * 1);  
  165.                     }  
  166.                 } else {  
  167.                                        //currentIndex = 0;  
  168.                                        //scrollTo(0, 0);  
  169.                 }  
  170.             }  
  171.         }, duration);  
  172.     }  
  173. }  

MainActivity:

  1. package com.tony.autoscroll;  
  2.   
  3. import com.example.testautoscroll.R;  
  4.   
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7.   
  8. /** 
  9.  * link: blog.csdn.net/t12x3456 
  10.  * @author Tony 
  11.  * 
  12.  */  
  13. public class MainActivity extends Activity {  
  14.   
  15.     private AutoScrollView scrollView;  
  16.       
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.       
  23.         scrollView = (AutoScrollView) findViewById(R.id.auto_scrollview);  
  24.     }  
  25.   
  26.     @Override  
  27.     protected void onStart() {  
  28.         // TODO Auto-generated method stub  
  29.           
  30.         if(!scrollView.isScrolled()){  
  31.             scrollView.setScrolled(true);  
  32.         }  
  33.         super.onStart();  
  34.     }  
  35.       
  36.     @Override  
  37.     protected void onStop() {  
  38.         // TODO Auto-generated method stub  
  39.           
  40.         if(scrollView.isScrolled()){  
  41.             scrollView.setScrolled(false);  
  42.         }  
  43.         super.onStop();  
  44.     }  
  45. }

如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456
原文地址:https://www.cnblogs.com/Free-Thinker/p/5571846.html