国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android編程實(shí)現(xiàn)類似天氣預(yù)報(bào)圖文字幕垂直滾動(dòng)效果的方法

2019-10-22 18:30:43
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Android編程實(shí)現(xiàn)類似天氣預(yù)報(bào)圖文字幕垂直滾動(dòng)效果的方法。分享給大家供大家參考,具體如下:

在很多天氣或者新聞的應(yīng)用中,我們都能看到一些字幕滾動(dòng)的效果,最簡(jiǎn)單的實(shí)現(xiàn)為跑馬燈效果,用系統(tǒng)提供的屬性即可實(shí)現(xiàn). 復(fù)雜一些的就需要自己去用自定義控件實(shí)現(xiàn). 比如 讓TextView 實(shí)現(xiàn)垂直滾動(dòng). 這里我要講的是垂直滾動(dòng)的字幕效果,并且內(nèi)容并不僅為文字,還可以加入圖片或者其他元素. 廢話不多說,還是直接上效果圖:

Android,天氣預(yù)報(bào),圖文字幕,垂直滾動(dòng)

首先還是看一下核心的實(shí)現(xiàn):

目前我的做法是重寫了ScrollView,對(duì)外提供幾個(gè)重要的方法:

isScrolled()方法判斷當(dāng)前是否為滾動(dòng)狀態(tài)
setScrolled(boolean flag)設(shè)置滾動(dòng)的開關(guān)
setPeriod(long period)設(shè)置從開始滾動(dòng)到結(jié)束的時(shí)間
setSpeed(long speed)設(shè)置滾動(dòng)的速度

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

1.由于是定時(shí)操作,所以需要在Activity的對(duì)應(yīng)生命周期進(jìn)行處理: 當(dāng)界面由不可見到可見時(shí),設(shè)置setScrolled(true)打開滾動(dòng)開關(guān),由可見到不可見時(shí),setScrolled(false)關(guān)閉開關(guān)

2. 可根據(jù)自己需要調(diào)用setPeriod(long period)setSpeed(long speed)控制滾動(dòng)的速度

3. 由于是ScrollView實(shí)現(xiàn)的,中間放置的內(nèi)容同ScrollView,不僅僅可以設(shè)置文字,還可以添加圖片等其他元素,實(shí)現(xiàn)復(fù)雜的UI

4. 圖文混排, 目前這個(gè)DEMO我還沒做細(xì)致處理. 最主要的部分就是文字的處理,需要考慮中英文,全角半角,字體大小,段落處理,計(jì)算對(duì)應(yīng)的字符寬高等進(jìn)行排版

圖片等資源處理的部分就相對(duì)要簡(jiǎn)單,主要處理分辨率與計(jì)算寬高

關(guān)于這些部分,之后我會(huì)慢慢做細(xì)致講解.

這個(gè)Demo是我臨時(shí)寫的,UI和圖文混排包括具體的滾動(dòng)部分處理都相對(duì)簡(jiǎn)單,大家可以在這個(gè)例子的基礎(chǔ)上進(jìn)行擴(kuò)展,根據(jù)需求做出自己想要的效果:

demo示例代碼點(diǎn)擊此處本站下載。

下面是對(duì)應(yīng)的代碼:

首先是自定義View:

package com.tony.autoscroll;import android.content.Context;import android.os.Handler;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.ScrollView;/** * @author Tony * */public class AutoScrollView extends ScrollView {  private final Handler handler = new Handler();   private long duration   = 50;   private boolean isScrolled  = false;  private int currentIndex = 0;  private long period = 1000;  private int currentY = -1;  private double x;  private double y;  private int type = -1;  /**   * @param context   */   public AutoScrollView(Context context) {    this(context, null);  }   /**   * @param context   * @param attrs   */  public AutoScrollView(Context context, AttributeSet attrs) {     this(context, attrs, 0);  }  /**   * @param context    * @param attrs    * @param defStyle    */  public AutoScrollView(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);   }  public boolean onTouchEvent(MotionEvent event) {     int Action = event.getAction();    switch (Action) {      case MotionEvent.ACTION_DOWN:        x=event.getX();        y=event.getY();         if (type == 0) {           setScrolled(false);         }         break;       case MotionEvent.ACTION_MOVE:        double moveY = event.getY() - y;         double moveX = event.getX() - x;        if ((moveY>20||moveY<-20) && (moveX < 50 || moveX > -50) && getParent() != null) {           getParent().requestDisallowInterceptTouchEvent(true);         }         break;      case MotionEvent.ACTION_UP:         if (type == 0) {           currentIndex = getScrollY();          setScrolled(true);        }        break;      default:        break;     }     return super.onTouchEvent(event);  }  @Override  public boolean onInterceptTouchEvent(MotionEvent p_event)  {    return true;  }  /**   * 判斷當(dāng)前是否為滾動(dòng)狀態(tài)   *   * @return the isScrolled   */   public boolean isScrolled() {    return isScrolled;   }   /**   * 開啟或者關(guān)閉自動(dòng)滾動(dòng)功能   *   * @param isScrolled true為開啟,false為關(guān)閉   */   public void setScrolled(boolean isScrolled) {    this.isScrolled = isScrolled;    autoScroll();   }   /**   * 獲取當(dāng)前滾動(dòng)到結(jié)尾時(shí)的停頓時(shí)間,單位:毫秒   *   * @return the period   */   public long getPeriod() {    return period;   }   /**   * 設(shè)置當(dāng)前滾動(dòng)到結(jié)尾時(shí)的停頓時(shí)間,單位:毫秒   *   * @param period   * the period to set   */   public void setPeriod(long period) {     this.period = period;   }   /**   * 獲取當(dāng)前的滾動(dòng)速度,單位:毫秒,值越小,速度越快。    *   * @return the speed   */   public long getSpeed() {    return duration;   }   /**   * 設(shè)置當(dāng)前的滾動(dòng)速度,單位:毫秒,值越小,速度越快。   *   * @param speed   *      the duration to set   */   public void setSpeed(long speed) {     this.duration = speed;   }   public void setType(int type){     this.type = type;   }   private void autoScroll() {     handler.postDelayed(new Runnable() {      @Override       public void run() {        boolean flag = isScrolled;         if (flag) {          if (currentY == getScrollY()) {             try {              Thread.sleep(period);            } catch (InterruptedException e) {              e.printStackTrace();            }            currentIndex = 0;             scrollTo(0, 0);            handler.postDelayed(this, period);          } else {            currentY = getScrollY();            handler.postDelayed(this, duration);            currentIndex++;            scrollTo(0, currentIndex * 1);          }        } else {            //currentIndex = 0;            //scrollTo(0, 0);        }      }    }, duration);  }}

MainActivity:

package com.tony.autoscroll;import com.example.testautoscroll.R;import android.os.Bundle;import android.app.Activity;/** * link: blog.csdn.net/t12x3456 * @author Tony * */public class MainActivity extends Activity {  private AutoScrollView scrollView;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    scrollView = (AutoScrollView) findViewById(R.id.auto_scrollview);  }  @Override  protected void onStart() {    // TODO Auto-generated method stub    if(!scrollView.isScrolled()){      scrollView.setScrolled(true);    }    super.onStart();  }  @Override  protected void onStop() {    // TODO Auto-generated method stub    if(scrollView.isScrolled()){      scrollView.setScrolled(false);    }    super.onStop();  }}

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 姚安县| 博客| 峨眉山市| 东城区| 长宁区| 花莲县| 资兴市| 喜德县| 车致| 鞍山市| 呼和浩特市| 鞍山市| 盱眙县| 宿迁市| 仪陇县| 保康县| 集贤县| 阿瓦提县| 大厂| 石渠县| 高邮市| 洞口县| 离岛区| 长岛县| 乡城县| 泽普县| 苏州市| 来安县| 灵璧县| 孟连| 三江| 赫章县| 宜昌市| 金秀| 巩义市| 林甸县| 漳平市| 渝中区| 崇仁县| 大竹县| 泉州市|