ViewFlipper实现文字轮播效果

所属分类: 软件编程 / Android 阅读数: 62
收藏 0 赞 0 分享

ViewFlipper实现文字轮播(仿淘宝头条垂直滚动广告),供大家参考,具体内容如下

广告条目可以单独写成布局文件,然后在布局文件或者代码中添加到总布局中

从源码可以看出,其实ViewFlipper间接的继承了FrameLayout,也可以说ViewFlipper其实就是个FrameLayout,只不过在内部封装了动画实现和Handler实现一个循环而已。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <!--android:autoStart:设置自动加载下一个View-->
 <!--android:flipInterval:设置View之间切换的时间间隔-->
 <!--android:inAnimation:设置切换View的进入动画-->
 <!--android:outAnimation:设置切换View的退出动画-->
 <ViewFlipper

  android:id="@+id/view_flipper"
  android:layout_width="match_parent"
  android:layout_height="100dp"
  android:layout_centerInParent="true"
  android:autoStart="true"
  android:background="#808080"
  android:flipInterval="2000"
  android:inAnimation="@anim/slide_in_down"
  android:outAnimation="@anim/slide_out_up">

  <TextView
   android:id="@+id/first"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="白日依山尽"
   android:textColor="#FF00FF"
   android:textSize="50sp" />

  <TextView
   android:id="@+id/second"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="黄河入海流"
   android:textColor="#FF00FF"
   android:textSize="50sp" />

  <TextView
   android:id="@+id/third"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="欲穷千里目"
   android:textColor="#FF00FF"
   android:textSize="50sp" />

  <TextView
   android:id="@+id/forth"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="更上一层楼"
   android:textColor="#FF00FF"
   android:textSize="50sp" />
 </ViewFlipper>
</RelativeLayout>

这里介绍ViewFlipper用到的属性,这些属性其实都可以使用代码实现,只不过这里为了代码看上去美观,才放在布局里的

  • android:autoStart : 设置自动加载下一个View
  • android:flipInterval : 设置View之间切换的时间间隔
  • android:inAnimation : 设置切换View的进入动画
  • android:outAnimation : 设置切换View的退出动画

下面是ViewFlipper常用的方法介绍,除了可以设置上面的属性之外,还提供了其他方法

  • isFlipping : 判断View切换是否正在进行
  • setFlipInterval : 设置View之间切换的时间间隔
  • startFlipping : 开始View的切换,而且默认会循环进行
  • stopFlippiing : 停止View的切换
  • setOutAnimation : 设置切换View的退出动画
  • setInAnimation : 设置切换View的进入动画
  • showNext : 显示ViewFlipper里的下一个View
  • showPrevious : 显示ViewFlipper里的上一个View

这里还涉及到两个动画其实就是一个平移的动画,它们都保存在anim文件夹中

slide_in_down.xml 进入动画

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

slide_out_up.xml 退出动画

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

MainActivity

package com.nrf.mydemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

 }

}

运行之后,效果图

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

更多精彩内容其他人还在看

android开发之Json文件的读写的示例代码

这篇文章主要介绍了android开发之Json文件的读写的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android7.0指纹服务FingerprintService实例介绍

这篇文章主要介绍了Android7.0指纹服务FingerprintService介绍,需要的朋友可以参考下
收藏 0 赞 0 分享

Android JNI处理图片实现黑白滤镜的方法

这篇文章主要介绍了Android JNI处理图片实现黑白滤镜的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android引入OpenCV的示例

本篇文章主要介绍了Android引入OpenCV的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android Zip解压缩工具类分享

这篇文章主要为大家详细介绍了Android Zip解压缩工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Interval

这篇文章主要为大家详细介绍了Android RxJava创建操作符Interval的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

5分钟快速实现Android爆炸破碎酷炫动画特效的示例

本篇文章主要介绍了5分钟快速实现Android爆炸破碎酷炫动效的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android 指纹功能实例代码

本文通过一个demo给大家介绍了android指纹功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现倒计时CountDownTimer使用详解

这篇文章主要为大家详细介绍了Android实现倒计时CountDownTimer的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android RxJava创建操作符Timer的方法

这篇文章主要为大家详细介绍了Android RxJava创建操作符Timer的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多