Android如何實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式

這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

目前創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、七星網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

Android如何實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式  

方法一、使用系統(tǒng)控件ViewFlipper方式:

布局文件:

<ViewFlipper
    android:id="@+id/view_flipper"
    android:layout_width="300dp"
    android:layout_height="35dp"
    android:layout_centerInParent="true"
    android:autoStart="true"
    android:background="@drawable/warning_bg"
    android:flipInterval="3000"
    android:inAnimation="@anim/slide_in_bottom"
    android:outAnimation="@anim/slide_out_top">
    <TextView
      android:id="@+id/tv_warning_content1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:ellipsize="middle"
      android:gravity="center"
      android:singleLine="true"
      android:text="有預(yù)警信息有預(yù)警信息有預(yù)警信息"
      android:textColor="#000000"
      android:textSize="16sp"/>
    <TextView
      android:id="@+id/tv_warning_content2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:ellipsize="middle"
      android:gravity="center"
      android:singleLine="true"
      android:text="當(dāng)前天氣狀況當(dāng)前天氣狀況當(dāng)前"
      android:textColor="#000000"
      android:textSize="16sp"/>
    <TextView
      android:id="@+id/tv_warning_content3"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:ellipsize="middle"
      android:gravity="center"
      android:singleLine="true"
      android:text="123456465"
      android:textColor="#000000"
      android:textSize="16sp"/>
  </ViewFlipper>

背景文件:warning_bg.xml

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="#34000000"/>
  <corners android:radius="80dp"/>
</shape>

切入動(dòng)畫:slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="1000"
    android:fromYDelta="100%p"
    android:toYDelta="0" />
</set>

切出動(dòng)畫:slide_out_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="-100%p" />
</set>

注意:如果不在布局文件里設(shè)置: android:autoStart="true", 可以在代碼中動(dòng)態(tài)設(shè)置開始循環(huán)mViewFlipper.startFlipping();

在Activity中顯示正常,但在fragment中可能會(huì)有重影的現(xiàn)象。

方法二、使用三方框架

Gradle:

compile 'com.sunfusheng:marqueeview:1.3.3'

屬性

Android如何實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式

XML

<com.sunfusheng.marqueeview.MarqueeView
  android:id="@+id/marqueeView"
  android:layout_width="match_parent"
  android:layout_height="30dp"
  app:mvAnimDuration="1000"
  app:mvDirection="bottom_to_top"
  app:mvInterval="3000"
  app:mvTextColor="@color/white"
  app:mvTextSize="14sp"
  app:mvSingleLine="true"/>

設(shè)置字符串列表數(shù)據(jù)

MarqueeView marqueeView = (MarqueeView) findViewById(R.id.marqueeView);
List<String> info = new ArrayList<>();
info.add("11111111111111");
info.add("22222222222222");
info.add("33333333333333");
info.add("44444444444444");
info.add("55555555555555");
info.add("66666666666666");
marqueeView.startWithList(info);
// 在代碼里設(shè)置自己的動(dòng)畫
marqueeView.startWithList(info, R.anim.anim_bottom_in, R.anim.anim_top_out);

設(shè)置字符串?dāng)?shù)據(jù)

String notice = "心中有陽光,腳底有力量!心中有陽光,腳底有力量!心中有陽光,腳底有力量!";
marqueeView.startWithText(notice);
// 在代碼里設(shè)置自己的動(dòng)畫
marqueeView.startWithText(notice, R.anim.anim_bottom_in, R.anim.anim_top_out);

設(shè)置事件監(jiān)聽

marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
  @Override
  public void onItemClick(int position, TextView textView) {
    Toast.makeText(getApplicationContext(), String.valueOf(marqueeView1.getPosition()) + ". " + textView.getText(), Toast.LENGTH_SHORT).show();
  }
});

重影問題可參考以下解決方案

@Override
public void onStart() {
  super.onStart(); 
  marqueeView.startFlipping();
}
@Override
public void onStop() {
  super.onStop();
  marqueeView.stopFlipping();
}

注意:這個(gè)庫主要還是繼承了ViewFlipper,類似的庫還有MarqueeViewLibrary,實(shí)現(xiàn)方法基本類似,在Activity中顯示正常,但在fragment中可能會(huì)有重影的現(xiàn)象。

方法三、使用系統(tǒng)控件TextSwitcher實(shí)現(xiàn)

布局文件

<TextSwitcher
      android:id="@+id/text_switcher"
      android:layout_width="285dp"
      android:layout_height="35dp"
      android:background="@drawable/warning_bg"/>

背景文件:warning_bg.xml

<?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <solid android:color="#34000000"/>
  <corners android:radius="80dp"/>
</shape>

代碼:

private int index = 0;//textview上下滾動(dòng)下標(biāo)
  private Handler handler = new Handler();
  private boolean isFlipping = false; // 是否啟用預(yù)警信息輪播
  private List<String> mWarningTextList = new ArrayList<>();
  private void setTextSwitcher() {
    mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));
    mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));
    mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
      @Override
      public View makeView() {
        TextView textView = new TextView(mContext);
        textView.setSingleLine();
        textView.setTextSize(12);//字號(hào)
        textView.setTextColor(Color.parseColor("#ffffff"));
        textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
        textView.setSingleLine();
        textView.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.gravity = Gravity.CENTER;
        textView.setLayoutParams(params);
        textView.setPadding(25, 0, 25, 0);
        return textView;
      }
    });
  }
    private Runnable runnable = new Runnable() {
    @Override
    public void run() {
      if (!isFlipping) {
        return;
      }
      index++;
      mTextSwitcher.setText(mWarningTextList.get(index % mWarningTextList.size()));
      if (index == mWarningTextList.size()) {
        index = 0;
      }
      startFlipping();
    }
  };
  //開啟信息輪播
  public void startFlipping() {
    if (mWarningTextList.size() > 1) {
      handler.removeCallbacks(runnable);
      isFlipping = true;
      handler.postDelayed(runnable, 3000);
    }
  }
  //關(guān)閉信息輪播
  public void stopFlipping() {
    if (mWarningTextList.size() > 1) {
      isFlipping = false;
      handler.removeCallbacks(runnable);
    }
  }
  //設(shè)置數(shù)據(jù)
  private void setData() {
    if (mWarningTextList.size() == 1) {
      mTextSwitcher.setText(mWarningTextList.get(0));
      index = 0;
    }
    if (mWarningTextList.size() > 1) {
      handler.postDelayed(new Runnable() {
        @Override
        public void run() {
          mTextSwitcher.setText(mWarningTextList.get(0));
          index = 0;
        }
      }, 1000);
      mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));
      mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));
      startFlipping();
    }
  }
    @Override
  public void onResume() {
    super.onResume();
    startFlipping();
  }
  @Override
  public void onStop() {
    super.onStop();
    stopFlipping();
  }

切入動(dòng)畫:slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="1000"
    android:fromYDelta="100%p"
    android:toYDelta="0" />
</set>

切出動(dòng)畫:slide_out_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="-100%p" />
</set>

注意:這種方法在Activity和Fragment中均使用正常,可以解決Android文字垂直滾動(dòng)、縱向走馬燈在Fragment中重疊的現(xiàn)象。

關(guān)于“Android如何實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

文章題目:Android如何實(shí)現(xiàn)文字垂直滾動(dòng)、縱向走馬燈效果的實(shí)現(xiàn)方式
轉(zhuǎn)載注明:http://muchs.cn/article12/ihsjgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、企業(yè)建站、App開發(fā)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司