亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能【附demo源碼下載】

 更新時(shí)間:2017年02月22日 12:14:36   作者:藍(lán)之風(fēng)  
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能,詳細(xì)分析了Android TextView垂直自動(dòng)滾動(dòng)功能的實(shí)現(xiàn)步驟與布局、功能相關(guān)技巧,并附帶了demo源碼供讀者下載,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)TextView垂直自動(dòng)滾動(dòng)功能。分享給大家供大家參考,具體如下:

在做android 應(yīng)用的開發(fā)的時(shí)候,橫向滾動(dòng)或者要做出跑馬燈的效果很簡(jiǎn)單,textview本身的屬性就支持,只要設(shè)置準(zhǔn)確就會(huì)滾動(dòng),開發(fā)起來(lái)比較簡(jiǎn)單,但是textview 不支持垂直滾動(dòng),那么垂直滾動(dòng)就需要自己來(lái)實(shí)現(xiàn)了,很多網(wǎng)友提供的垂直滾 動(dòng)方案都是千篇一律,使用ScrollView來(lái)進(jìn)行滾動(dòng),但是都不完美,做起來(lái)有些別扭。有一位網(wǎng)友給出的歌詞的滾動(dòng)思路明確,能從根本上解決問(wèn)題,因此我實(shí)現(xiàn)的這個(gè)滾動(dòng)是在這位網(wǎng)友的基礎(chǔ)上實(shí)現(xiàn),封裝了一個(gè)View,view繼承自TextView。先看看實(shí)現(xiàn)的效果:

實(shí)現(xiàn)圖中效果的關(guān)鍵點(diǎn)是:

1、重寫onDrow方法,計(jì)算每次的滾動(dòng)的距離。
2、計(jì)算view的Y軸的重點(diǎn),讓當(dāng)前顯示的處于高亮顯示狀態(tài)。
3、定時(shí)的刷新View使其界面不斷的刷先,出現(xiàn)滾動(dòng)的效果。
4、實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu),將數(shù)據(jù)傳給view。

下面看看主要代碼:

1、創(chuàng)建一個(gè)類繼承TextView

 /**
 * @author xushilin
 *
 * 垂直滾動(dòng)的TextView Widget
 */
public class VerticalScrollTextView extends TextView

2、實(shí)現(xiàn)構(gòu)造函數(shù):

 public VerticalScrollTextView(Context context) {
  super(context);
  init();
 }
 public VerticalScrollTextView(Context context, AttributeSet attr) {
  super(context, attr);
  init();
 }
 public VerticalScrollTextView(Context context, AttributeSet attr, int i) {
  super(context, attr, i);
  init();
 }
 private void init() {
  setFocusable(true);
  //這里主要處理如果沒有傳入內(nèi)容顯示的默認(rèn)值
  if(list==null){
   list=new ArrayList<Notice>();
   Notice sen=new Notice(0,"暫時(shí)沒有通知公告");
   list.add(0, sen);
  }
  //普通文字的字號(hào),以及畫筆顏色的設(shè)置
  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mPaint.setTextSize(16);
  mPaint.setColor(Color.BLACK);
  mPaint.setTypeface(Typeface.SERIF);
  //高亮文字的字號(hào),以及畫筆顏色的設(shè)置
  mPathPaint = new Paint();
  mPathPaint.setAntiAlias(true);
  mPathPaint.setColor(Color.RED);
  mPathPaint.setTextSize(16);
  mPathPaint.setTypeface(Typeface.SANS_SERIF);
 }

3、從寫onDraw方法,并計(jì)算文字的行距,并且將將普通文字和高亮文字,在這個(gè)方法中繪制出來(lái)

 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  canvas.drawColor(0xEFeffff);
  Paint p = mPaint;
  Paint p2 = mPathPaint;
  p.setTextAlign(Paint.Align.CENTER);
  if (index == -1)
   return;
  p2.setTextAlign(Paint.Align.CENTER);
   canvas.drawText(list.get(index).getName(), mX, middleY, p2);
  float tempY = middleY;
   for (int i = index - 1; i >= 0; i--) {
   tempY = tempY - DY;
   if (tempY < 0) {
    break;
   }
   canvas.drawText(list.get(i).getName(), mX, tempY, p);
  }
  tempY = middleY;
   for (int i = index + 1; i < list.size(); i++) {
   tempY = tempY + DY;
   if (tempY > mY) {
    break;
   }
   canvas.drawText(list.get(i).getName(), mX, tempY, p);
  }
 }

4、計(jì)算Y軸中值以及更新索引

 protected void onSizeChanged(int w, int h, int ow, int oh) {
  super.onSizeChanged(w, h, ow, oh);
  mX = w * 0.5f;
  mY = h;
  middleY = h * 0.5f;
 }
  private long updateIndex(int index) {
  if (index == -1)
   return -1;
  this.index=index;
  return index;
 }

5、定時(shí)更新view,并將接口暴露給客戶程序調(diào)用。

 public void updateUI(){
  new Thread(new updateThread()).start();
 }
  class updateThread implements Runnable {
  long time = 1000;
  int i=0;
  public void run() {
   while (true) {
    long sleeptime = updateIndex(i);
    time += sleeptime;
    mHandler.post(mUpdateResults);
    if (sleeptime == -1)
     return;
    try {
     Thread.sleep(time);
     i++;
     if(i==getList().size())
      i=0;
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }
 Handler mHandler = new Handler();
 Runnable mUpdateResults = new Runnable() {
  public void run() {
   invalidate();
  }
 };

6、xml布局文件中調(diào)用:

<?xml version="1.0" encoding="utf-8"?>
<!-- Demonstrates scrolling with a ScrollView. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">
 <com.demo.xsl.text.SampleView
 android:id="@+id/sampleView1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/selector"
 />
</LinearLayout>

7、java代碼中調(diào)用,傳遞數(shù)據(jù):

package com.demo.xsl.text;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
 public class VerticalScrollTextActivity extends Activity {
  SampleView mSampleView;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mSampleView = (SampleView) findViewById(R.id.sampleView1);
  List lst=new ArrayList<Sentence>();
  for(int i=0;i<30;i++){
   if(i%2==0){
    Sentence sen=new Sentence(i,i+"、金球獎(jiǎng)三甲揭曉 C羅梅西哈維入圍 ");
    lst.add(i, sen);
   }else{
    Sentence sen=new Sentence(i,i+"、公牛欲用三大主力換魔獸????");
    lst.add(i, sen);
   }
  }
  //給View傳遞數(shù)據(jù)
  mSampleView.setList(lst);
  //更新View
  mSampleView.updateUI();
 }
}

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

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android Service組件使用技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

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

相關(guān)文章

  • Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺

    Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺

    這篇文章主要為大家詳細(xì)介紹了Android自定義RecyclerView實(shí)現(xiàn)不固定刻度的刻度尺,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Android實(shí)現(xiàn)可點(diǎn)擊的幸運(yùn)大轉(zhuǎn)盤

    Android實(shí)現(xiàn)可點(diǎn)擊的幸運(yùn)大轉(zhuǎn)盤

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可點(diǎn)擊的幸運(yùn)大轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Android?Material組件庫(kù)日期選擇和時(shí)間選擇器的使用方法

    Android?Material組件庫(kù)日期選擇和時(shí)間選擇器的使用方法

    這篇文章主要介紹了Android?Material組件庫(kù)(日期選擇和時(shí)間選擇器)基本使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • Android數(shù)據(jù)庫(kù)操作工具類分享

    Android數(shù)據(jù)庫(kù)操作工具類分享

    這篇文章主要為大家詳細(xì)介紹了Android數(shù)據(jù)庫(kù)操作工具類的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Kotlin掛起函數(shù)的詳細(xì)介紹

    Kotlin掛起函數(shù)的詳細(xì)介紹

    掛起函數(shù)用狀態(tài)機(jī)以掛起點(diǎn)將協(xié)程的運(yùn)算邏輯拆分成不同的片段,每次執(zhí)行協(xié)程運(yùn)行不同的邏輯片段,由此可以知道協(xié)程是運(yùn)行在線程中的,線程的并發(fā)處理方式也可以用在協(xié)程上
    2022-09-09
  • Android系統(tǒng)自帶分享圖片功能

    Android系統(tǒng)自帶分享圖片功能

    這篇文章主要為大家詳細(xì)介紹了Android系統(tǒng)自帶分享圖片功能,圖片分享的工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • 詳解Flutter桌面應(yīng)用如何進(jìn)行多分辨率適配

    詳解Flutter桌面應(yīng)用如何進(jìn)行多分辨率適配

    這篇文章主要為大家介紹了Flutter桌面應(yīng)用如何進(jìn)行多分辨率適配的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Android仿微信錄制語(yǔ)音功能

    Android仿微信錄制語(yǔ)音功能

    這篇文章主要介紹了Android仿微信錄制語(yǔ)音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android實(shí)現(xiàn)簡(jiǎn)單的答題系統(tǒng)

    Android實(shí)現(xiàn)簡(jiǎn)單的答題系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單的答題系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android中LayoutInflater.inflater()的正確打開方式

    Android中LayoutInflater.inflater()的正確打開方式

    這篇文章主要給大家介紹了關(guān)于Android中LayoutInflater.inflater()的正確打開方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12

最新評(píng)論