android viewswitcher 切换动画,分享一个可以轻松自定义效果的视图自由切换容器(Android),欢迎点赞和fork...

English | 中文文档

AndroidAutoSwitcher

AutoSwitchView是一个能自由切换两个子控件的控件容器,相比于ViewFlipper,它支持子控件的复用,数据量大的情况下能有更好的性能。同时,相比于AdapterViewFlipper,它有更好的扩展性,支持自定义各种切换动画。

ffbf624c108040bcd7433d829569b27a.gif

用法

将以下依赖添加到 build.gradle 文件中

compile 'com.github.markshawn:auto-switcher:1.0.1'

XML中的代码

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:switcher_repeatCount="1"

app:switcher_autoStart="true"/>

如果想让切换动画无限循环,只需将switcher_repeatCount设置为-1.

Activity中的代码

AutoSwitchView as = (AutoSwitchView) findViewById(R.id.yourid);

as.setAdapter(new YourAdapter());

as.setSwitchStrategy(new YourStrategy()); // See Switching Strategy

as.startSwitcher(); // If you have set autoStart true, this is not needed.

Switching Strategy

你能利用SwitchStrategy轻松定制你喜欢的切换动画,它同时支持Animation和 ObjectAnimator。以下是一些已经提供的动画策略的建造者类:

AnimationStrategyBuilde:

支持利用Animation定制自己的动画;

AnimatorStrategyBuilder:

支持利用ObjectAnimator定制自己的动画;

CarouselStrategyBuilder:

支持在不同方向上的两个item的无缝切换;

ContinuousStrategyBuilder:

支持两个item之间的平滑无停顿的切换;

一个栗子:

as.setSwitchStrategy(

new AnimationStrategyBuilder(this, R.anim.anim_in, R.anim.anim_out).

build()

);

另一个栗子:

as.setSwitchStrategy(

new CarouselStrategyBuilder().

setAnimDuration(900).

setInterpolator(new AccelerateDecelerateInterpolator()).

setMode(DirectionMode.right2Left).

build()

);

在大多数情况下,以上的切换动画策略已经足够。如果想定制不那么复杂的动画, 也可以将自己的Animation或者ObjectAnimator添加到AnimationStrategyBuilde或者AnimatorStrategyBuilder中产生对应的切换策略。

复杂定制(非必需)

如果想定制更加复杂的切换动画, 你可以轮流添加SingleOperator到BaseBuilder中,创建你自己的SwitchStrategy,这样就可以完全控制切换流程了(如下所示)。

new SwitchStrategy.BaseBuilder().

init(new SingleOperator() {

@Override

public void operate(AutoSwitchView switcher, ChainOperator operator) {

...//init your animation

}

}).next(new SingleOperator() {

@Override

public void operate(AutoSwitchView switcher, ChainOperator operator) {

...//run your own animation

}

}).withEnd(new SingleOperator() {

@Override

public void operate(AutoSwitchView switcher, ChainOperator operator) {

...//cancle or end your animation

}

}).build();

许可证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值