Android动画效果之:LayoutAnimation
2016-06-03
开发中,我们通常要为控件做一些特殊动画效果。其实Android也为Layout提供了动画效果,其动画效果就展现在Layout加载子控件的过程中。
比如我们常用的LayoutAnimation及GridLayoutAnimation(用于GridLayout)。还是以代码例子说明。
1:GridLayoutAnimation在GridLayout的使用。
/**
* GridView中控件加载动画效果
*
* @description:
* @author ldm
* @date 2016-6-3 上午11:22:15
*/
public class GridLayoutAnimation extends Activity {
private GridView grid;
// 手机中应用列表数据
private List<ResolveInfo> mApps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载手机中已经安装的应用
loadApps();
setContentView(R.layout.layout_grid_animation);
// 初始化控件及添加适配器数据
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(new GridLayoutAnimation.AppsAdapter());
}
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(GridLayoutAnimation.this);
ResolveInfo info = mApps.get(position % mApps.size());
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f);
i.setLayoutParams(new GridView.LayoutParams(w, w));
return i;
}
public final int getCount() {
// 设置显示的总数量
return Math.min(32, mApps.size());
}
public final Object getItem(int position) {
return mApps.get(position % mApps.size());
}
public final long getItemId(int position) {
return position;
}
}
}
—–布局界面—–
<?xml version="1.0" encoding="utf-8"?>
<!-- android:verticalSpacing指item间的垂直间距 -->
<!-- android:horizontalSpacing指item间的水平间距 -->
<!-- android:numColumns指第一行itm的个数 -->
<!-- android:layoutAnimation通过这个设备item的加载动画 -->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="60dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:layoutAnimation="@anim/layout_grid_fade"
android:numColumns="auto_fit"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
—-动画xml——
<?xml version="1.0" encoding="utf-8"?>
<!-- gridLayoutAnimation是针对grideView添加Item的进入动画 -->
<!-- android:animation:动画xml -->
<!-- android:directionPriority:方向优先级 -->
<!-- android:rowDelay:每一行动画开始的延迟 -->
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/fade"
android:directionPriority="column"
android:rowDelay="50%" />
2,ListView通过LayoutAnimationController实现加载动画
/**
* ListView通过LayoutAnimationController实现加载动画
*
* @description:
* @author ldm
* @date 2016-6-3 上午11:50:22
*/
public class LayoutAnimation extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设备数据适配器
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings));
// AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等。
AnimationSet set = new AnimationSet(true);
// 透明度动画
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
// 位移动画
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
-1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
animation.setDuration(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.5f);// 注意第二个参数以秒为单位,是浮点型数据,所以要加f
ListView listView = getListView();
listView.setLayoutAnimation(controller);// 设置动画
}
private String[] mStrings = { "Bordeaux", "Lyon", "Marseille", "Nancy",
"Paris", "Toulouse", "Strasbourg" };
}
更多关于LayoutAnimation请参考:https://github.com/ldm520/ANDROID_API_DEMOS