转载请注明出处,谢谢:http://blog.csdn.net/harryweasley/article/details/51546426
本篇博客最终效果,如下所示:
当点击click按钮,弹出对话框,gridView里面的item一个一个从下往上出来;当点击红叉按钮,gridView里面的item一个一个从上往下的消失。
其实刚开始做的时候,item一个一个出来的动画,官方api是有现成的接口的,调用setLayoutAnimation(LayoutAnimationController controller)这个方法就可以。
看官方文档是这样描述的:
Sets the layout animation controller used to animate the group's children after the first layout.
翻译为:设置布局动画控制器用来给第一次出现的ChildView做动画。
但是,令我无语的是,官方并没有现成的item一个个一消失的动画,最终我通过下面的方法解决了这个问题,在这里进行记录,方法是,当我点击红色叉的时候,让每一个item都执行一个动画,通过迭代的方法来实现动画效果。
Java类就这三个,
我现在只放MoreFragmentDialog的代码,其他的代码,你可以下载demo,直接去看,链接地址在文章的末尾。
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.GridView;
import android.widget.LinearLayout;
public class MoreFragmentDialog extends DialogFragment implements OnClickListener{
private GridView gv;
/**
* 关闭按钮
*/
private LinearLayout close;
/**
* 当前下降的是哪个item
*/
private int downNum;
/**
* 是否多次点击关闭按钮
*/
private boolean isMultiple = false;
private List<String> stringlList;
public static MoreFragmentDialog newInstance(){
MoreFragmentDialog fragmentDialog=new MoreFragmentDialog();
return fragmentDialog;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//全屏幕
setStyle(DialogFragment.STYLE_NORMAL,
android.R.style.Theme_Translucent_NoTitleBar);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_more,container , false);
gv=(GridView) view.findViewById(R.id.gv);
gv.setLayoutAnimation(getAnimationController());
initList();
close=(LinearLayout) view.findViewById(R.id.close);
close.setOnClickListener(this);
return view;
}
private void initList() {
stringlList=new ArrayList<>();
for (int i = 0; i < 6; i++) {
stringlList.add(i+"");
}
MyBaseAdapter adapter=new MyBaseAdapter(getActivity(),stringlList);
gv.setAdapter(adapter);
}
@Override
public void onClick(View v) {
if (!isMultiple) {
isMultiple = true;
if (v == close) {
downNum = stringlList.size()-1;
removeGridViewItem(gv.getChildAt(downNum), downNum);
}
}
}
/**
* Layout动画
*
* @return
*/
protected LayoutAnimationController getAnimationController() {
int duration = 400;
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(duration);
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(duration);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(
set, 0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
return controller;
}
/**
* 移除gridView固定位置的一个item
*
* @param rootView
* gridView固定位置的View
* @param position
* gridView固定位置的position
*/
private void removeGridViewItem(final View rootView, final int position) {
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setDuration(400);
set.addAnimation(animation);
animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(400);
set.addAnimation(animation);
set.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
//隐藏已经退出的item
rootView.setVisibility(View.INVISIBLE);
downNum--;
if (downNum == -1) {
isMultiple = false;
MoreFragmentDialog.this.dismiss();
return;
}
removeGridViewItem(gv.getChildAt(downNum), downNum);
}
});
rootView.startAnimation(set);
}
}
关于TranslateAnimation里面的参数问题,你可以查看这篇文章:
http://blog.csdn.net/knlnzhao/article/details/8026778
本篇博客demo下载地址为:http://download.csdn.net/detail/harryweasley/9536614