Android控制之垂直滚动广告条ViewFLipper解析,阿里牛逼

然后是anim_out.xml,同样的,出动画。从0位置移动到-100%的位置,动画持续1s

接下来看一下我们的ViewFlipper的子项布局

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
xmlns:app=“http://schemas.android.com/apk/res-auto”>


</android.support.constraint.ConstraintLayout>

很简单,就是一个图片+一个标题而已。 接着就可以设置ViewFlipper的子项了。

public class MainActivity extends AppCompatActivity {

private ViewFlipper mViewFlipper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewFlipper = (ViewFlipper)findViewById(R.id.view_flipper);
for(int i=0;i<3;i++){
View view = getLayoutInflater().inflate(R.layout.view_flipper_content,null);
mViewFlipper.addView(view);
}
mViewFlipper.setFlipInterval(2000);
mViewFlipper.startFlipping();
}
}

这里获取控件ViewFlipper之后,对控件addView,然后设置每个控件滑动到下一个控件的时间,之后调用startFlipping()就可以开始滚动啦。

源码解析

ViewFlipper继承自ViewAnimator,实质上只是封装了一些ViewAnimator的方法来调用,真正执行操作的是ViewAnimator。 我们来看ViewFlipper的核心方法startFlipping()。

public void startFlipping() {
mStarted = true;
updateRunning();
}

这里调用的是updateRuning(),我们接着跳进去看看

private void updateRunning() {
updateRunning(true);
}

一个私有方法,调用了重载的updateRunning,我们继续跳进去看看。

private void updateRunning(boolean flipNow) {
boolean running = mVisible && mStarted && mUserPresent;
if (running != mRunning) {
if (running) {
showOnly(mWhichChild, flipNow);
postDelayed(mFlipRunnable, mFlipInterval

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

);
} else {
removeCallbacks(mFlipRunnable);
}
mRunning = running;
}
if (LOGD) {
Log.d(TAG, “updateRunning() mVisible=” + mVisible + “, mStarted=” + mStarted

  • “, mUserPresent=” + mUserPresent + “, mRunning=” + mRunning);
    }
    }

在一系列的判断后调用基类的方法showOnly,我们跳转过去看看。

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);
}
child.setVisibility(View.VISIBLE);
mFirstTime = false;
} else {
if (animate && mOutAnimation != null && child.getVisibility() == View.VISIBLE) {
//对可见子项调用退出动画
child.startAnimation(mOutAnimation);
} else if (child.getAnimation() == mInAnimation)
//不可见子项取消动画
child.clearAnimation();
//同时设置不可见
child.setVisibility(View.GONE);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值