挤出点时间迅速把这篇博文完成。

---------------------------------------------------------------------------------------------------------


1. 在 Android3.0+ 平台下可以直接使用 LayoutTransition 类实现上图效果(笔者随手制作的gif,见谅见谅),实现代码:

package com.soxfmr.anim;import android.animation.LayoutTransition;import android.content.Context;import android.util.AttributeSet;import android.widget.RelativeLayout;public class Container extends RelativeLayout {public Container(Context context, AttributeSet attrs) {super(context, attrs);LayoutTransition transition = new LayoutTransition();this.setLayoutTransition(transition);}}

接下来直接调用 addView 添加子元素即可看到效果。

---------------------------------------------------------------------------------------------------------


2. 如果app支持到 Android2.1 平台,那么 LayoutTransition 就无法使用,通过 Animation 实现上图效果:

package com.soxfmr.anim;import android.content.Context;import android.util.AttributeSet;import android.view.animation.AlphaAnimation;import android.widget.RelativeLayout;import android.widget.TextView;public class Container extends RelativeLayout {private Context mContext;public Container(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;}public void addview() {TextView item = new TextView(mContext);//set the animation effects to the itemAlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);alphaAnimation.setDuration(800);//add the item to parent view while starting animationitem.startAnimation(alphaAnimation);this.addView(item);}}

---------------------------------------------------------------------------------------------------------

3. 结合代码,以适应不同平台:

package com.soxfmr.anim;import android.animation.LayoutTransition;import android.content.Context;import android.os.Build;import android.util.AttributeSet;import android.view.View;import android.view.animation.AlphaAnimation;import android.widget.RelativeLayout;import android.widget.TextView;public class Container extends RelativeLayout {private Context mContext;private boolean bAnimMode = false;public boolean isbAnimMode() {return bAnimMode;}/** true is open the animation effects. */public void setbAnimMode(boolean bAnimMode) {this.bAnimMode = bAnimMode;}public Container(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;//If the platform is Android3.0 upperif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {final LayoutTransition transition = new LayoutTransition();this.setLayoutTransition(transition);    }else {    setbAnimMode(true);    }}public void addview() {TextView item = new TextView(mContext);refreshView(item);this.addView(item);}public void refreshView(View item) {if( ismAnimMode() ) {//set the animation effects to the itemAlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);alphaAnimation.setDuration(800);//add the item to parent view while starting animationitem.startAnimation(alphaAnimation);}}}

更多相关文章

  1. 【Android】使用LiveData KTX Builder让代码更简洁
  2. android实践项目一实现简单的验证码和spinner下拉选项效果
  3. 一个简单的iPhone项目及代码
  4. Android模仿微信、云播雷达扫描动画效果
  5. 在android平台上编译libpcap-0.9.8 和 jnetpcap-1.2.rc1
  6. android studio调试c++代码
  7. Android-两种方式实现走马灯效果
  8. Android之实现textvew跑马灯效果

随机推荐

  1. Android(安卓)API Guides---Making Appli
  2. Android(安卓)分享功能,过滤掉不需要的组
  3. Android消息机制之HandlerThread
  4. 按下电源键到启动Home应用过程详解(一)
  5. Android的4种基本布局
  6. 关于Unity与Android通信与交互
  7. IOS和Android开发的一些个人感受
  8. android开发笔记之animation(一)
  9. 如何分析和研究Log文件(转)
  10. Android实现凸起的导航栏