如何应用动画逐一添加gridview中的项目

时间:2015-06-23 08:28:28

标签: android animation

我在一个应用程序中看到有一个GridView,其中的项目逐个添加(并非所有项目都在同一时间)。

我正在寻找那个动画。

有人可以帮帮我吗?

谢谢!

5 个答案:

答案 0 :(得分:1)

使用可在AppCompat Support Library

中找到的RecyclerView

然后将RecyclerView.ItemAnimator子类化并将其作为参数传递给RecyclerView.setItemAnimator

可以找到一些示例herehere

注意:要将RecyclerView用作网格,您需要使用RecyclerView.setLayoutManager并传递GridLayoutManager作为参数。

希望这有帮助。

答案 1 :(得分:1)

尝试使用getview方法向适配器添加动画,如下所示:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null)
    {
         LayoutInflater inflator =  LayoutInflater.from(context);
         convertView = inflator.inflate(R.layout.grid_view, null);  
    }
    convertView.setAnimation(animation)
    return convertView;
}

答案 2 :(得分:1)

视图在适配器的getView方法中返回。所以你必须在这里向你的视图添加动画。请看你的代码:

   @Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item, null);

        vh = new ViewHolder();
        vh.iw = (ImageView) convertView.findViewById(R.id.iw);
        vh.container = (RelativeLayout) convertView.findViewById(R.id.category_label);
        vh.cateoryIcon = (ImageView) convertView.findViewById(R.id.category_icon);
        vh.username = (TextView) convertView.findViewById(R.id.username);
        vh.userScore = (TextView) convertView.findViewById(R.id.user_picture_score);
        vh.progressbar = new ProgressBar(context);
        convertView.setTag(vh);
    } else {
        vh = (ViewHolder) convertView.getTag();
    }

    UserHelper user = userList.get(position);
    vh.iw.setAdjustViewBounds(true);

    vh.container.setBackgroundColor(Color.parseColor(CategoryManager.INSTANCE.getCategoryById(user.getCategoryId()).getColorTransparent()));
    vh.cateoryIcon.setImageBitmap(CategoryManager.INSTANCE.getCategoryById(user.getCategoryId()).getSmallIcon());
    vh.username.setText(user.getNickName());
    vh.userScore.setText(user.getScore());

    Animation animation;
    if (position % 4 == 0) {
        animation = AnimationUtils.loadAnimation(context, R.anim.your_sexy_animation);
        animation.setDuration(340);
    } else {
        animation = AnimationUtils.loadAnimation(context, R.anim.fade_in);
        animation.setDuration(280);
    }

    convertView.startAnimation(animation);
    imageLoader.displayImage(user.getImageUrl(), vh.iw);

    return convertView;


}

答案 3 :(得分:1)

从我关于LayoutAnimationController的问题下面的答案中得到提示,我能够在答案中实现我想要的输出。

下面我发布了我的代码:

我的动画类

AnimationSet set = new AnimationSet(true);

    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(500);
    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(500);
    set.addAnimation(animation);

    LayoutAnimationController controller =
            new LayoutAnimationController(set, 0.5f);

我在GridView中将其设置为

gridView.setLayoutAnimation(controller);

答案 4 :(得分:0)

您可以尝试使用LayoutAnimationController类