动画在列表视图中添加项目和项目移动,例如Google Plus应用

时间:2014-02-12 12:57:09

标签: android listview android-listview

当用户在列表视图中滚动时,我想重现列表视图项的效果。

我想重现与Google Plus应用程序相同的效果。

我该怎么做?

更新:

以下是我的getView()代码的摘录:

    final float centerX = rowView.getWidth() / 2.0f;
    final float centerY = rowView.getHeight() / 2.0f;

    rowView.startAnimation(new Rotate3dAnimation(0.0f, -90.0f, centerX, centerY, 310.0f, true));

更新2:

02-12 16:01:17.198: E/AndroidRuntime(21625): java.lang.NullPointerException
02-12 16:01:17.198: E/AndroidRuntime(21625):    at com.rss.home.ArticleListAdapterHome.getView(ArticleListAdapterHome.java:129)
02-12 16:01:17.198: E/AndroidRuntime(21625):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
02-12 16:01:17.198: E/AndroidRuntime(21625):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-12 16:01:17.198: E/AndroidRuntime(21625):    at android.widget.ListView.fillDown(ListView.java:691)
02-12 16:01:17.198: E/AndroidRuntime(21625):    at android.widget.ListView.fillFromTop(ListView.java:752)

1 个答案:

答案 0 :(得分:1)

感谢视频。
对我来说,这看起来像3D旋转。动画可能是在适配器的getView方法中启动的。这意味着有三个步骤:

  1. 检查自定义ListAdapter,例如here。你也会发现很多问题/答案
  2. Click here了解如何在getView
  3. 中开始制作动画
  4. 检查this answer here。它描述了在哪里可以找到3D动画
  5. 的示例

    我自己没有检查3D动画,但ListAdapterAnimation不应该是一个大问题。


    修改
    您不必从xml加载动画。你可以这样做:

    public View getView(int position, View view, ViewGroup viewGroup) {
        // normal handling
        // ...
        // now apply animation
        view.startAnimation(new Rotate3dAnimation(/*params*/));
    }
    

    <强> EDIT2
    我现在已经对它进行了测试,以下是我改变它以使其正常工作的内容:

    1. 我忘了设置动画的持续时间,我确实设置了这个
    2. 视图的高度和宽度未在getView中设置,因此删除了参数centerXcenterY并添加了View view并将其赋予rowView }
    3. 动画正在使用camera.rotateY,但需要将其更改为camera.rotateX
    4. 这是我更新的Rotate3dAnimation:

      package de.malaka.player.animation;
      
      import android.view.View;
      import android.view.animation.Animation;
      import android.view.animation.Transformation;
      import android.graphics.Camera;
      import android.graphics.Matrix;
      
      /**
       * An animation that rotates the view on the Y axis between two specified angles.
       * This animation also adds a translation on the Z axis (depth) to improve the effect.
       */
      public class Rotate3dAnimation extends Animation {
      private final float mFromDegrees;
      private final float mToDegrees;
      private final float mDepthZ;
      private final View mView;
      private final boolean mReverse;
      private Camera mCamera;
      
      /**
       * Creates a new 3D rotation on the Y axis. The rotation is defined by its
       * start angle and its end angle. Both angles are in degrees. The rotation
       * is performed around a center point on the 2D space, definied by a pair
       * of X and Y coordinates, called centerX and centerY. When the animation
       * starts, a translation on the Z axis (depth) is performed. The length
       * of the translation can be specified, as well as whether the translation
       * should be reversed in time.
       *
       * @param fromDegrees the start angle of the 3D rotation
       * @param toDegrees the end angle of the 3D rotation
       * @param centerX the X center of the 3D rotation
       * @param centerY the Y center of the 3D rotation
       * @param reverse true if the translation should be reversed, false otherwise
       */
      public Rotate3dAnimation(float fromDegrees, float toDegrees, float depthZ, boolean reverse, View view) {
          mFromDegrees = fromDegrees;
          mToDegrees = toDegrees;
          mDepthZ = depthZ;
          mReverse = reverse;
          mView = view;
      }
      
      @Override
      public void initialize(int width, int height, int parentWidth, int parentHeight) {
          super.initialize(width, height, parentWidth, parentHeight);
          mCamera = new Camera();
      }
      
      @Override
      protected void applyTransformation(float interpolatedTime, Transformation t) {
          final float fromDegrees = mFromDegrees;
          float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
      
          final float centerX = mView.getWidth()/2;
          final float centerY = mView.getHeight()/2;
          final Camera camera = mCamera;
      
          final Matrix matrix = t.getMatrix();
      
          camera.save();
          if (mReverse) {
              camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
          } else {
              camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
          }
          camera.rotateX(degrees);
          camera.getMatrix(matrix);
          camera.restore();
      
          matrix.preTranslate(-centerX, -centerY);
          matrix.postTranslate(centerX, centerY);
      }
      }
      

      这就是我在适配器中使用它的方式:

      Animation anim = new Rotate3dAnimation(90.0f, 0.0f, 100.0f, false, convertView);
      anim.setDuration(1000l);
      convertView.startAnimation(anim);
      

      现在持续时间很长,但这样你可以调整值。