Android开发 之 viewGroup视图动画
程序员文章站
2022-03-16 21:41:35
...
视图组添加,移除,隐藏,显示动画
ViewGroup ,LinearLayout,FrameLayout ,RelativeLayout等都是视图组,在他们添加,删除,显示隐藏的子view的时候的动画效果
如果想使用系统默认的动画效果只需添加一个属性
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/verticalContainer"
android:animateLayoutChanges="true" />
当然也可以自定义动画效果:
APPEARING - 指示在容器中出现的项目上运行的动画的标志。(添加view自身的动画)
CHANGE_APPEARING - 指示由于容器中出现新项目而正在更改的项目上运行的动画的标志。(添加view时,其他view的移动动画)
DISAPPEARING - 指示在从容器中消失的项目上运行的动画的标志。(移除view自身的动画)
CHANGE_DISAPPEARING - 指示由于项目从容器中消失而正在更改的项目上运行的动画的标志。(移除view时,其他view的动画)
LayoutTransition transition=new LayoutTransition();
transition.setAnimator(LayoutTransition.APPEARING,自定义的animator);
viewgroup.setLayoutTransition(transition);
自定义动画:可以参考apidemo中的LayoutAnimations示例
private void createCustomAnimations(LayoutTransition transition) {
// Changing while Adding
PropertyValuesHolder pvhLeft =
PropertyValuesHolder.ofInt("left", 0, 1);
PropertyValuesHolder pvhTop =
PropertyValuesHolder.ofInt("top", 0, 1);
PropertyValuesHolder pvhRight =
PropertyValuesHolder.ofInt("right", 0, 1);
PropertyValuesHolder pvhBottom =
PropertyValuesHolder.ofInt("bottom", 0, 1);
PropertyValuesHolder pvhScaleX =
PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
PropertyValuesHolder pvhScaleY =
PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
customChangingAppearingAnim = ObjectAnimator.ofPropertyValuesHolder(
this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhScaleX, pvhScaleY).
setDuration(transition.getDuration(LayoutTransition.CHANGE_APPEARING));
customChangingAppearingAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setScaleX(1f);
view.setScaleY(1f);
}
});
// Changing while Removing
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
PropertyValuesHolder pvhRotation =
PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);
customChangingDisappearingAnim = ObjectAnimator.ofPropertyValuesHolder(
this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhRotation).
setDuration(transition.getDuration(LayoutTransition.CHANGE_DISAPPEARING));
customChangingDisappearingAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setRotation(0f);
}
});
// Adding 以y走旋转出现
customAppearingAnim = ObjectAnimator.ofFloat(null, "rotationY", 90f, 0f).
setDuration(transition.getDuration(LayoutTransition.APPEARING));
customAppearingAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setRotationY(0f);
}
});
// Removing
customDisappearingAnim = ObjectAnimator.ofFloat(null, "rotationX", 0f, 90f).
setDuration(transition.getDuration(LayoutTransition.DISAPPEARING));
customDisappearingAnim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator anim) {
View view = (View) ((ObjectAnimator) anim).getTarget();
view.setRotationX(0f);
}
});
}
上一篇: orb特征描述符 物体匹配
下一篇: 帧同步之从零开始
推荐阅读
-
Android开发之超强图片工具类BitmapUtil完整实例
-
Android开发技巧之我的菜单我做主(自定义菜单)
-
Android开发之图形图像与动画(一)Paint和Canvas类学习
-
Android开发之图形图像与动画(五)LayoutAnimationController详解
-
Android应用开发之简易、大气音乐播放器实现专辑倒影效果
-
Android开发之背景动画简单实现方法
-
Android开发之OkHttpUtils的具体使用方法
-
Android 开发之Dialog,Toast,Snackbar提醒
-
Android开发之XML文件解析的使用
-
解析Android开发优化之:对界面UI的优化详解(一)