Android动画效果之自定义ViewGroup添加布局动画(五)

 更新时间:2016年08月25日 09:09:09   作者:总李写代码  
这篇文章主要介绍了Android动画效果之自定义ViewGroup添加布局动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言:

前面几篇文章介绍了补间动画、逐帧动画、属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画。本文将通过对自定义图片选择控件设置动画为例来学习布局动画。

自定义一个显示多行图片的ViewGroup:

这里不再对自定义控件做解说,想了解的可以看下以下几篇文章
 •Android自定义控件之基本原理(一)
 •Android自定义控件之自定义属性(二)
 •Android自定义控件之自定义组合控件(三)
 •Android自定义控件之自定义ViewGroup实现标签云(四) 

声明几个属性值:

  <declare-styleable name="GridImageViewGroup">
  <attr name="childVerticalSpace" format="dimension"/>
  <attr name="childHorizontalSpace" format="dimension"/>
  <attr name="columnNum" format="integer"/>
 </declare-styleable>

GridImageViewGroup.java 代码

public class GridImageViewGroup extends ViewGroup {
 private int childVerticalSpace = 0;
 private int childHorizontalSpace = 0;
 private int columnNum = 3;
 private int childWidth = 0;
 private int childHeight = 0;


 public GridImageViewGroup(Context context, AttributeSet attrs) {
  super(context, attrs);
  TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.GridImageViewGroup);
  if (attributes != null) {
   childVerticalSpace = attributes.getDimensionPixelSize(R.styleable.GridImageViewGroup_childVerticalSpace, 0);
   childHorizontalSpace = attributes.getDimensionPixelSize(R.styleable.GridImageViewGroup_childHorizontalSpace, 0);
   columnNum = attributes.getInt(R.styleable.GridImageViewGroup_columnNum, 3);
   attributes.recycle();
  }
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int rw = MeasureSpec.getSize(widthMeasureSpec);
  int rh = MeasureSpec.getSize(heightMeasureSpec);
  int childCount = getChildCount();
  if (childCount > 0) {
   childWidth = (rw - (columnNum - 1) * childHorizontalSpace) / columnNum;

   childHeight = childWidth;

   int vw = rw;
   if (childCount < columnNum) {
    vw = childCount * (childHeight + childVerticalSpace);
   }
   int rowCount = childCount / columnNum + (childCount % columnNum != 0 ? 1 : 0);

   int vh = rowCount * childHeight + (rowCount > 0 ? rowCount - 1 : 0) * childVerticalSpace;

   setMeasuredDimension(vw, vh);
  }
 }

 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int left = 0;
  int top = 0;
  int count = getChildCount();
  for (int i = 0; i < count; i++) {
   View child = getChildAt(i);
   left = (i % columnNum) * (childWidth + childHorizontalSpace);
   top = (i / columnNum) * (childHeight + childVerticalSpace);
   child.layout(left, top, left + childWidth, top + childHeight);
  }
 }

在xml中引用: 

<com.whoislcj.animation.GridImageViewGroup
   android:id="@+id/image_layout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:animateLayoutChanges="true"
   lee:childHorizontalSpace="10dp"
   lee:childVerticalSpace="10dp"
   lee:columnNum="3"/>



在Activity中调用:

private void initViews() {
  mImageViewGroup = (GridImageViewGroup) findViewById(R.id.image_layout);
  ImageView imageView = new ImageView(this);
  imageView.setImageResource(R.mipmap.add_image);
  imageView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    addImageView();
   }
  });
  mImageViewGroup.addView(imageView);
 }

 public void addImageView() {
  final ImageView imageView = new ImageView(MainActivity4.this);
  imageView.setImageResource(R.mipmap.lottery);
  imageView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    mImageViewGroup.removeView(imageView);
   }
  });
  mImageViewGroup.addView(imageView, 0);
 }

实现效果如下: 

布局动画产生的背景:

凡事总要问个明白,为何要引入布局动画呢?其实通过上面的实现效果可以看出,在添加和删除图片时都显得很突兀,不知道该用什么语言形容了,总之就是感觉不舒服。其实我平时在开发中调用View.setVisibility()方法时也会有这种感受,这也是布局动画产生的一个背景吧。 

布局动画:

布局动画是指ViewGroup在布局时产生的动画效果 。实现布局动画有如下几种方式 
第一种方式:在xml中,对ViewGrope设置android:animateLayoutChanges="true"属性: 

<com.whoislcj.animation.GridImageViewGroup
   android:id="@+id/image_layout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:animateLayoutChanges="true"
   lee:childHorizontalSpace="10dp"
   lee:childVerticalSpace="10dp"
   lee:columnNum="3"/>

就这么简单的一句话实现的效果就可以实现了,看看效果如何

 

这种方式虽然简单但是实现的布局动画比较单一,下面看第二种方式。 

第二种方式:LayoutTransition实现 

 LayoutTransition mLayoutTransition = new LayoutTransition();

  //设置每个动画持续的时间
  mLayoutTransition.setStagger(LayoutTransition.CHANGE_APPEARING, 50);
  mLayoutTransition.setStagger(LayoutTransition.CHANGE_DISAPPEARING, 50);
  mLayoutTransition.setStagger(LayoutTransition.APPEARING, 50);
  mLayoutTransition.setStagger(LayoutTransition.DISAPPEARING, 50);

  PropertyValuesHolder appearingScaleX = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 1.0f);
  PropertyValuesHolder appearingScaleY = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 1.0f);
  PropertyValuesHolder appearingAlpha = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
  ObjectAnimator mAnimatorAppearing = ObjectAnimator.ofPropertyValuesHolder(this, appearingAlpha, appearingScaleX, appearingScaleY);
  //为LayoutTransition设置动画及动画类型
  mLayoutTransition.setAnimator(LayoutTransition.APPEARING, mAnimatorAppearing);


  PropertyValuesHolder disappearingAlpha = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
  PropertyValuesHolder disappearingRotationY = PropertyValuesHolder.ofFloat("rotationY", 0.0f, 90.0f);
  ObjectAnimator mAnimatorDisappearing = ObjectAnimator.ofPropertyValuesHolder(this, disappearingAlpha, disappearingRotationY);
  //为LayoutTransition设置动画及动画类型
  mLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, mAnimatorDisappearing);


  ObjectAnimator mAnimatorChangeDisappearing = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f);
  //为LayoutTransition设置动画及动画类型
  mLayoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, mAnimatorChangeDisappearing);

  ObjectAnimator mAnimatorChangeAppearing = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f);
  //为LayoutTransition设置动画及动画类型
  mLayoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, mAnimatorChangeAppearing);

  //为mImageViewGroup设置mLayoutTransition对象
  mImageViewGroup.setLayoutTransition(mLayoutTransition);

上面通过自定义LayoutTransition 修改系统提高的默认动画效果,如果不需要自定义的动画效果的话,不调用mLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, mAnimatorDisappearing);就行了。 
LayoutTransition 提供了以下几种过渡类型:
 •APPEARING —— 元素在容器中显现时需要动画显示。
 •CHANGE_APPEARING —— 由于容器中要显现一个新的元素,其它元素的变化需要动画显示。
 •DISAPPEARING —— 元素在容器中消失时需要动画显示。
 •CHANGE_DISAPPEARING —— 由于容器中某个元素要消失,其它元素的变化需要动画显示。 

看下修改过的动画效果: 

第三种方式:通过设置LayoutAnimation来实现布局动画

 AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
  alphaAnimation.setDuration(200);
  LayoutAnimationController animationController = new LayoutAnimationController(alphaAnimation, 0.5f);
  animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
  mImageViewGroup.setLayoutAnimation(animationController); 

 显示顺序有以下几种:
 • ORDER_NORMAL;//顺序显示
 • ORDER_REVERSE;//反显示
 • ORDER_RANDOM//随机显示 

也可以通过xml实现 

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:delay="0.5"
 android:animationOrder="normal"
 android:animation="@anim/alpha"
 />

ViewGroup xml添加android:layoutAnimation属性 

 <com.whoislcj.animation.GridImageViewGroup
   android:id="@+id/image_layout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10dp"
   android:layoutAnimation="@anim/layoutanimation"
   lee:childHorizontalSpace="10dp"
   lee:childVerticalSpace="10dp"
   lee:columnNum="3"/>

由于这种方式采用的是补间动画,个人不再推荐使用这种方式,原因很简单实现的动画效果相对单一。

总结:

本篇学习了布局动画,自此Android的动画学习也将告一段落了,接下来准备总结一下学习动画的过程中遇见的编程知识,比如链式编程,TreadLocal等。

相关文章

  • android帮助文档打开慢的三种解决方法

    android帮助文档打开慢的三种解决方法

    本文介绍了“android帮助文档打开慢的三种解决方法”,需要的朋友可以参考一下
    2013-03-03
  • Android 网络图片查看器与网页源码查看器

    Android 网络图片查看器与网页源码查看器

    本篇文章主要介绍了Android 网络图片查看器与网页源码查看器的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04
  • 基于Android代码实现常用布局

    基于Android代码实现常用布局

    大家在日常中经常见到用xml文件实现android常用布局,但是大家知道如何用代码实现呢?使用代码实现可以帮助我们学习sdk api,所以小编把我日常整理些关于android常用布局代码实现分享给大家
    2015-11-11
  • AndroidStudio更新出现Refreshing ''xxx'' Gradle Project状态解决办法

    AndroidStudio更新出现Refreshing ''xxx'' Gradle Project状态解决办法

    这篇文章主要介绍了AndroidStudio更新出现Refreshing 'xxx' Gradle Project状态解决办法的相关资料,需要的朋友可以参考下
    2017-03-03
  • Android实现扫描二维码功能

    Android实现扫描二维码功能

    这篇文章主要为大家详细介绍了Android实现扫描二维码功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Android实现滑动加载数据的方法

    Android实现滑动加载数据的方法

    这篇文章主要介绍了Android实现滑动加载数据的方法,实例分析了Android通过滑动实现动态加载数据的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • Android源码中final关键字的用法及final,finally,finalize的区别

    Android源码中final关键字的用法及final,finally,finalize的区别

    Android的源码中很多地方对final关键字的用法很是“别出心裁”,之所以这么说是因为我从没看过是这么使用final关键字的,通过本文给大家分享Android源码中final关键字的用法及final,finally,finalize的区别,感兴趣的朋友一起学习吧
    2015-12-12
  • Flutter应用框架搭建实现屏幕适配方案详解

    Flutter应用框架搭建实现屏幕适配方案详解

    移动设备多样性,特别是Android的碎片化严重,存在各种各样的分辨率,flutter跨平台开发又需要同时支持Android和IOS,为尽可能的还原设计图效果提升用户的体验,根据设计稿设计屏幕ui的时候我们需要考虑到屏幕适配的问题
    2022-11-11
  • Android 进度条显示在标题栏的实现方法

    Android 进度条显示在标题栏的实现方法

    android进度条显示在标题栏的实现方法,大概分文xml文件和java文件,具体代码内容大家可以通过本文学习下
    2017-01-01
  • Android Jetpack库剖析之Lifecycle组件篇

    Android Jetpack库剖析之Lifecycle组件篇

    本章也是带来了Jetpack中我认为最重要的架构组件Lifecycle的原理探索,至于为什么觉得它是最重要是因为像ViewModel,LiveData这些组件也依赖于Lifecycle来感知宿主的生命周期,那么本章我们带着几个问题来探索一下这个组件
    2022-07-07

最新评论