Android 实现View中添加子元素的动态效果
2014-01-21 14:16
691 查看
挤出点时间迅速把这篇博文完成。
---------------------------------------------------------------------------------------------------------
1. 在 Android3.0+ 平台下可以直接使用 LayoutTransition 类实现上图效果(笔者随手制作的gif,见谅见谅),实现代码:
接下来直接调用 addView 添加子元素即可看到效果。
---------------------------------------------------------------------------------------------------------
2. 如果app支持到 Android2.1 平台,那么 LayoutTransition 就无法使用,通过 Animation 实现上图效果:
---------------------------------------------------------------------------------------------------------
3. 结合代码,以适应不同平台:
---------------------------------------------------------------------------------------------------------
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 item AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(800); //add the item to parent view while starting animation item.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 upper if (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 item AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); alphaAnimation.setDuration(800); //add the item to parent view while starting animation item.startAnimation(alphaAnimation); } } }
相关文章推荐
- android开发之实现动态打勾效果(DrawHookView)
- Android 自定义ViewGroup 实现FlowLayout,动态添加布局
- android开发之实现动态打勾效果(DrawHookView)
- android中TextView的文字实现动态效果,走马灯效果,闪烁效果
- 【Android UI】自定义TextView实现动态的文字闪动效果
- Android自定义View之实现一个动态的文字闪动效果
- android 动态背景的实现以及SurfaceView中添加EditText控件
- android 动态背景的实现以及SurfaceView中添加EditText控件
- android布局中子View添加动态隐藏和显示的动画效果。(LayoutTransition)
- Android自定义TextView实现动态的文字效果BlingBling
- Android TextView富文本、超链接和添加文字背景等其他效果的实现
- android 动态背景的实现以及SurfaceView中添加EditText控件
- Android 使用PullToZoomScrollViewEx实现头像下拉缩放动态效果
- Android用RecyclerView实现动态添加本地图片
- Android自定义ImageView实现在图片上添加图层效果
- Android开发之实现动态打勾效果(DrawHookView)
- Android viewpager中动态添加view并实现伪无限循环的方法
- LinearLayout 动态添加 TextView 实现标签效果
- android新特性:使用RecyclerViewHeader一键添加RecyclerView头布局并实现轮播效果
- Android自定义动态的View,实现飘雪的效果