ViewFlipper 淘宝头条 轮播 自动切换
自动 切换 淘宝 头条 轮播
2023-09-14 09:00:05 时间
ViewFlipper介绍
ViewFilpper类继承于ViewAnimator,而ViewAnimator类继承于FrameLayout。
![](https://images2015.cnblogs.com/blog/795730/201703/795730-20170313115627448-340287242.png)
ViewAnimator:
Base class for a FrameLayout container that will perform执行 animations when switching切换 between its views.
ViewFilpper:
Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip自动翻转 between each child at a regular interval间隔.
常用API
查看ViewAnimator类的源码可以看出此类的作用主要是为其中的View切换提供动画效果。该类有如下几个和动画相关的方法:
- setInAnimation(Animation)与setInAnimation(Context, resourceID):设置View进入屏幕时候使用的动画
- setOutAnimation、getInAnimation、getOutAnimation:动画
- showNext、showPrevious:显示FrameLayout里面的下/上一个View
- getCurrentView:当前的View。Returns the View corresponding to the currently displayed child.
- setDisplayedChild、getDisplayedChild:正在显示的View的序号。Returns the index of the currently displayed child view.
- setAnimateFirstView(boolean animate)、getAnimateFirstView:True to animate the current View the first time it is displayed, false otherwise.
- 此外还有各种removeView方法。
- setFilpInterval:设置View切换的时间间隔,参数为毫秒
- startFlipping:开始进行View的切换,切换会循环进行
- stopFlipping:停止View切换
- isFlipping: 用来判断View切换是否正在进行 。Returns true if the child views are flipping.
- setAutoStart:设置是否自动开始,如果设置为true,当ViewFlipper显示的时候View的切换会自动开始
- isAutoStart:是否为自动开始,Returns true if this view automatically calls startFlipping() when it becomes attached to a window.
一般情况下,我们都会使用ViewFilpper类实现View的切换,而不使用它的父类ViewAnimator类。
案例
public class MainActivity extends Activity {
private ViewFlipper flipper1, flipper2;
private List<TBean> mList = new ArrayList<TBean>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
flipper1 = new ViewFlipper(this);
flipper1.setBackgroundColor(0x330000ff);
flipper2 = new ViewFlipper(this);
flipper2.setBackgroundColor(0x33ff0000);
layout.addView(flipper1);
layout.addView(flipper2);
initFlipper(flipper1, R.anim.anim_in, R.anim.anim_out);
initFlipper(flipper2, R.anim.anim_in2, R.anim.anim_out2);
setContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
private void initFlipper(ViewFlipper flipper, int animIn, int animOut) {
mList.add(new TBean("疯传 家人给2岁孩子喝这个,孩子智力倒退10岁", "https://www.baidu.com/"));
mList.add(new TBean("哈哈 日本玩家33万甩卖15万张游戏王卡", "http://www.sina.com.cn/"));
mList.add(new TBean("头条 内疚逃命时没带够,回废墟狂挖30小时", "https://www.google.com.hk/"));
int size = mList.size();
for (int i = 0; i < size; i++) {
final int num = i;
TextView tv = new TextView(this);
tv.setText(mList.get(i).Title);
tv.setTextColor(0xff424954);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mList.get(num).PageUrl)));
}
});
flipper.addView(tv);
}
flipper.setFlipInterval(3000);//间隔时间
flipper.setInAnimation(this, animIn);
flipper.setOutAnimation(this, animOut);
flipper.startFlipping();
}
public static class TBean {
public TBean(String title, String pageUrl) {
Title = title;
PageUrl = pageUrl;
}
public String Title;
public String PageUrl;
}
}附件列表
相关文章
- 机器学习笔记 – 自动编码器autoencoder
- Vue项目实战05:18n实现多语言自动切换-浏览器语言设置「建议收藏」
- 深入Spring Boot自动装配
- avn自动切换node版本
- JetBrains IntelliJ支持自动切换输入法,写代码如丝般顺滑
- Microsoft Edge引入自动HTTPS切换功能推动安全浏览
- 使用Java操作Redis实现自动过期(redisjava过期)
- 自动清理基于Java实现Redis过期自动清理(redisjava过期)
- 小鹏「泊车」西南偏南,讲述自动驾驶的「落地」与「生长」
- MySQL实现跨数据库自动复制(mysql 跨数据库复制)
- MySQL高可用,实现一主一从自动切换(mysql 一主一从切换)
- 动Win7下Redis自动启动的操作指南(win7 redis自启)
- 解决Redis数据存储如何设置实现自动启动(怎么自动启动redis库)
- Oracle主键自增实现自动递增的方法(oracle主键自增实现)
- 使用Redis集群可以实现自动选举吗(redis集群可以选举吗)
- Redis实现自动切换优雅解决缓存瓶颈(redis设置自动切换)
- javascript定时自动切换的选项卡Tab
- jQuery自动切换/点击切换选项卡效果的小例子
- phpMyAdmin自动登录和取消自动登录的配置方法
- 点击标签切换和自动切换DIV选项卡
- jquery实现炫酷的叠加层自动切换特效