Android自定义View之popupwindow进阶封装:高仿ios “item动画弹出”效果的popupwindow。

一、前言。
二、何其重要的 LayoutAnimationController :控制子控件的动画。
既然上面提到 LayoutAnimationController可以为一个Layout里面的控件或者是Viewgroup的控件设置动画效果,那么我想到的思路是在 popuwindow上的view的控件做出这样的效果,所以,我想到的是listview,毕竟必须是Viewgroup或layout,如果是layout,在其里面动态的增加子控件,是不简单的,但是利用一些Viewgroup的控件,比如 listview、recyclerView… 等等,这里我就用listView啦,毕竟大家都很烂熟这个控件使用了。 下面是我的popupwindow加载的布局:

其代码就是如下这么少:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btCancle"
android:layout_margin="5dp" />
<Button
android:id="@+id/btCancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="5dp"
android:background="@drawable/buttonbg"
android:gravity="center"
android:text="取消"
android:textColor="#797979"
android:textStyle="bold" />
</RelativeLayout>
预览就如下:

三、代码分析。
// 从自已3倍的位置下面移到自己原来的位置,设置平移时间为0.4秒
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0f, Animation.RELATIVE_TO_SELF,3f, Animation.RELATIVE_TO_SELF, 0)
animation.setInterpolator(new DecelerateInterpolator())
animation.setDuration(400)
animation.setStartOffset(150)
- 然后在LayoutAnimationController的构造方法设置,其动画和每个子view的相继出现的时间间隔,我反复测试最佳时间是0.12,(注意是浮点类型)。
mLac = new LayoutAnimationController(animation, 0.12f);
mLac.setInterpolator(new DecelerateInterpolator());
- 下面为使用方法,只需要传一个list集合,泛型string,之后设置点击事件即可。如下,是不是感觉,这使用起来也太简单了吧?
final List<String> list = new ArrayList<>();
list.add("修改资料");
list.add("切换账号");
list.add("登录账号");
list.add("退出账号");
AnimotionPopupWindow popupWindow = new AnimotionPopupWindow(this, list);
popupWindow.show();
popupWindow.setAnimotionPopupWindowOnClickListener(new AnimotionPopupWindow.AnimotionPopupWindowOnClickListener() {
@Override
public void onPopWindowClickListener(int position) {
Toast.makeText(MainActivity.this, "点击了:" + list.get(position), Toast.LENGTH_SHORT).show();
}
});
- 好了,核心的代码就这么多啦,其他的popupwindow就没啥啦,想看整个代码去博文下面链接观看啦。相信大家都是可以做到的。
三、开发遇到的小问题总结。


那就先放GitHub吧,欢迎stat:https://github.com/xuhongv/AnimotionPopupWindow-masterr