前言:在我还在zz做炒股软件的时候,有个需求是垂直滚动显示3指数,当时我使用了ListView的自动滚动来实现,现在一想当时做的可真费劲,又是屏蔽手势传递又是处理自动滚动,其实这种效果用ViewFlipper实现真是太简单不过了,ViewFlipper的继承关系

效果图

实现代码

java代码

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ViewFlipper mViewFlipper = (ViewFlipper) findViewById(R.id.marquee_viewFlipper);LinearLayout shenzhenIndexLayout = (LinearLayout) View.inflate(this, R.layout.marquee_item_layout1, null);LinearLayout shangzhenIndexLayout = (LinearLayout) View.inflate(this, R.layout.marquee_item_layout2, null);LinearLayout cyIndexLayout = (LinearLayout) View.inflate(this, R.layout.marquee_item_layout3, null);mViewFlipper.addView(shenzhenIndexLayout);mViewFlipper.addView(shangzhenIndexLayout);mViewFlipper.addView(cyIndexLayout);}}

XML布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"android:gravity="center"android:background="@android:color/white"tools:context="com.qfxl.android.marqueeView.MainActivity"><ViewFlipperandroid:id="@+id/marquee_viewFlipper"android:layout_width="match_parent"android:layout_height="30dp"android:autoStart="true"android:background="#e8e8e8"android:flipInterval="2000"android:inAnimation="@anim/anim_in"android:outAnimation="@anim/anim_out"/>
</LinearLayout>

anim代码

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

简单分析

   <ViewFlipperandroid:id="@+id/marquee_viewFlipper"android:layout_width="match_parent"android:layout_height="30dp"android:autoStart="true"android:background="#e8e8e8"android:flipInterval="2000"android:inAnimation="@anim/anim_in"android:outAnimation="@anim/anim_out"/>
  • autoStart 是否自动开启轮播,这个方法设置为true的时候在源码中,也可以调用java代码setAutoStart(boolean autoStart)
  • flipInterval 轮播时间
  • inAnimation ViewFlipper中子View进入时的动画
  • outAnimation ViewFlipper中子View离开时的动画

ViewFlipper设置autoStart之后源码分析

首先设置了autoStart之后,在ViewFlipper中的onAttachedToWindow的源码中

  @Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();// Listen for broadcasts related to user-presencefinal IntentFilter filter = new IntentFilter();filter.addAction(Intent.ACTION_SCREEN_OFF);filter.addAction(Intent.ACTION_USER_PRESENT);// OK, this is gross but needed. This class is supported by the// remote views machanism and as a part of that the remote views// can be inflated by a context for another user without the app// having interact users permission - just for loading resources.// For exmaple, when adding widgets from a user profile to the// home screen. Therefore, we register the receiver as the current// user not the one the context is for.getContext().registerReceiverAsUser(mReceiver, android.os.Process.myUserHandle(),filter, null, getHandler());if (mAutoStart) {//设置autoStart为true// Automatically start when requestedstartFlipping();}}

接下来进入startFlipping,将mStarted状态位变为true

 /*** Start a timer to cycle through child views*/public void startFlipping() {mStarted = true;updateRunning();}

updateRunning中调用了updateRuning(true)方法

 /*** Internal method to start or stop dispatching flip {@link Message} based* on {@link #mRunning} and {@link #mVisible} state.*/private void updateRunning() {updateRunning(true);}

updateRunning(true)方法比较重要了

/*** Internal method to start or stop dispatching flip {@link Message} based* on {@link #mRunning} and {@link #mVisible} state.** @param flipNow Determines whether or not to execute the animation now, in*            addition to queuing future flips. If omitted, defaults to*            true.*/private void updateRunning(boolean flipNow) {/**mVisible在onWindowVisibilityChanged中赋值可见度一致*mStarted在设置autoStart之后变更*mUserPresent是监听了系统的广播Intent.ACTION_SCREEN_OFF跟Intent.ACTION_USER_PRESENT*/boolean running = mVisible && mStarted && mUserPresent;if (running != mRunning) {//mRunning默认为false,改变的地方只有下面if (running) {showOnly(mWhichChild, flipNow);//这个是在父类中实现的postDelayed(mFlipRunnable, mFlipInterval);//runnable} else {removeCallbacks(mFlipRunnable);}mRunning = running;//唯一改变mRunning标志的地方}if (LOGD) {Log.d(TAG, "updateRunning() mVisible=" + mVisible + ", mStarted=" + mStarted+ ", mUserPresent=" + mUserPresent + ", mRunning=" + mRunning);}}

showOnly方法做了什么,点进去看看,源码解释为只显示指定的View其余的View在屏幕上不可见,你可以设置View进入退出的动画

/*** Shows only the specified child. The other displays Views exit the screen,* optionally with the with the {@link #getOutAnimation() out animation} and* the specified child enters the screen, optionally with the* {@link #getInAnimation() in animation}.** @param childIndex The index of the child to be shown.* @param animate Whether or not to use the in and out animations, defaults*            to true.*/void showOnly(int childIndex, boolean animate) {final int count = getChildCount();for (int i = 0; i < count; i++) {final View child = getChildAt(i);if (i == childIndex) {if (animate && mInAnimation != null) {child.startAnimation(mInAnimation);//View进入ViewFlipper中的动画}child.setVisibility(View.VISIBLE);//当前View可见mFirstTime = false;} else {if (animate && mOutAnimation != null && child.getVisibility() == View.VISIBLE) {child.startAnimation(mOutAnimation);//View退出ViewFlipper中的动画} else if (child.getAnimation() == mInAnimation)child.clearAnimation();child.setVisibility(View.GONE);//当前View不可见}}}

showOnly看完看看这个runnable做了什么,直接跳到showNext中去

 private final Runnable mFlipRunnable = new Runnable() {@Overridepublic void run() {if (mRunning) {showNext();postDelayed(mFlipRunnable, mFlipInterval);}}};

showNext其实就是显示下一个子View了

public void showNext() {setDisplayedChild(mWhichChild + 1);}

点开setDisplayedChild

public void setDisplayedChild(int whichChild) {mWhichChild = whichChild;//这两个判断实现了ViewFlipper的循环if (whichChild >= getChildCount()) {mWhichChild = 0;} else if (whichChild < 0) {mWhichChild = getChildCount() - 1;}boolean hasFocus = getFocusedChild() != null;// This will clear old focus if we had itshowOnly(mWhichChild);//继续调用showOnlyif (hasFocus) {// Try to retake focus if we had itrequestFocus(FOCUS_FORWARD);}}

总结

ViewFlipper的源码其实不难,这里只是用来实现垂直的翻滚,也可以左右翻滚等,只要动画功底好,很多酷炫的效果可以做出来,写这篇博客的目的是为了提供垂直广告实现的又一种方案。
针对此效果,做了一个封装:MarqueeView

ViewFlipper实现垂直轮播广告效果相关推荐

  1. php广告轮播效果,使用swiper组件实现轮播广告效果

    这次给大家带来使用swiper组件实现轮播广告效果,使用swiper组件实现轮播广告效果的注意事项有哪些,下面就是实战案例,一起来看一下. 1.安装swipernpm install swiper@3 ...

  2. Android之——史上最简单图片轮播广告效果实现

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/48049913 如今的Android开发需求越来越来多,实现效果越来越酷炫,很多An ...

  3. ViewFlipper实现文字轮播(仿淘宝头条垂直滚动广告)

    ViewFlipper实现文字轮播(仿淘宝头条垂直滚动广告) 广告条目可以单独写成布局文件,然后在布局文件或者代码中添加到总布局中 从源码可以看出,其实ViewFlipper间接的继承了FrameLa ...

  4. Android 循环滚动控件ViewFlipper,可实现跑马灯或轮播图效果

    ViewFlipper--Android循环滚动控件 1.效果如下: 2.实现方法 (1)创建进出动画 上下滚动动画 y_in.xml <?xml version="1.0" ...

  5. 利用RecyclerView实现无限轮播广告条

    代码地址如下: http://www.demodashi.com/demo/14771.html 前言: 公司产品需要新增悬浮广告条的功能,要求是可以循环滚动,并且点击相应的浮条会跳转到相应的界面,在 ...

  6. css33d图片轮播_手把手教你用纯css3实现轮播图效果

    首先先看demo吧,点击查看demo 一.随便说几句 css3动画效果的强大不言而喻,自它出现一直热度不减,它与js动画的优劣也一直成为前端界争论的话题,不可置疑的是css3动画的出现在一定程度上降低 ...

  7. java轮播添加图片_给网站首页添加图片轮播的效果

    网站的首页有图片轮播的效果,可以很好的起到广告的作用也可以起到推荐优秀内容的作用. 可是一般的建站程序,首页的幻灯片效果都很一般不是很好看,有的时候就需要我们自己改一下代码. 太复杂的代码自己改不好, ...

  8. 广告轮播java_[springboot 开发单体web shop] 6. 商品分类和轮播广告展示

    商品分类&轮播广告 因最近又被困在了OSGI技术POC,更新进度有点慢,希望大家不要怪罪哦. 上节 我们实现了登录之后前端的展示,如: 接着,我们来实现左侧分类栏目的功能. 商品分类|Prod ...

  9. 仿淘宝、头条上下轮播广告

    仿淘宝.头条上下滚动自动轮播广告效果 前言:想必大家工作久了,都会接触横向走马灯.纵向走马灯的效果.在这里介绍系统自带的控件来实现纵向走马灯(上下滚动)效果. 布局: <ViewFlippera ...

最新文章

  1. 机器学习狗太苦逼了!自动化调参哪家强?
  2. gitlab 推送本地代码到远程仓库
  3. 简约之美Jodd-http--深入源码理解http协议
  4. 在 Windows 上直接运行 Linux,有命令行就是贼香
  5. ETC2 区别于ETC的重要点
  6. django为什么需要子进程,如何关闭子进程,linux状态
  7. [Deepin - Pycharm] PyQT5安装配置
  8. python中函数的参数
  9. 【免费下载】2021年11月热门报告盘点(附热门报告列表及下载链接)
  10. C# 设置Windows程序窗口为穿透状态
  11. 洛谷 P4147 玉蟾宫 (最大子矩形问题)
  12. turbo码书籍推荐
  13. C/C++ 内部收益率
  14. 一个好用的数学公式编辑器的下载安装(LaTeX)
  15. QQdengluqi, wangluorenzheng
  16. 认知升级是令我们变得优秀的重要基石,没有之一
  17. Fashion-mnist数据的读取与保存
  18. 社会化营销,微博如何老树发新芽?
  19. 国产智能AI对话:技术狂潮之下,要有梦元宇宙正在改变世界
  20. JDBC SQl注入

热门文章

  1. mysql 表的第2条到4条记录,2020-07-30-mysql第三章作业练习
  2. jQuery的介绍与使用方式
  3. Mysql 忘记root密码的完美解决方法
  4. 电脑图标变大了怎么办?
  5. ios与安卓的时间兼容问题
  6. [数据结构与算法综合实验]欢乐练练看
  7. 爬十格阶梯每次一步或两步_爬上信息阶梯
  8. 高三计算机基础应用教学计划,一级计算机基础及MS,Office应用教学计划
  9. 用Yolact模型训练自己的数据集
  10. SolidWorks toolbox齿轮再修改方法