How to make Rotate3dAnimation more smoother?

前端 未结 4 867
北荒
北荒 2020-12-30 06:47

In my app I am using Rotate3dAnimation to show a Google map. The code is working fine, but the animation is not smooth, some lines are also visible while rotating the view.

4条回答
  •  -上瘾入骨i
    2020-12-30 07:10

    One option for (non-OpenGL) 3d animation effects on Android is to implement the animations in a ViewGroup's getChildStaticTransform method using the graphics.Camera and Matrix classes.

    In broad terms it's done like this:

    • Extend ViewGroup or a subclass thereof.

    • In the constructors, set staticTransformationEnabled to true:

       setStaticTransformationsEnabled(true);
      
    • Override the protected method getChildStaticTransformation(View view, Transformation t).

    • In getChildStaticTransformation, use graphics.Camera to rotate the View as per your picture.

    • Get the camera's matrix and adjust it to center the camera position on the view.

    For example, this is how a 3d translation effect is done in the 3d carousel by Igor Kushnarev:

    protected boolean getChildStaticTransformation(View child, Transformation transformation) {
       //...
        // Center of the item
        float centerX = (float)child.getWidth()/2, centerY = (float)child.getHeight()/2;
    
        // Save camera
        mCamera.save();
    
        // Translate the item to it's coordinates
        final Matrix matrix = transformation.getMatrix();
        mCamera.translate(((CarouselImageView)child).getX(), 
                ((CarouselImageView)child).getY(), 
                ((CarouselImageView)child).getZ());
    
        // Get the camera's matric and position the item
        mCamera.getMatrix(matrix);
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    
        // Restore camera
        mCamera.restore();      
    
        return true;
     }    
    

    Here are some more code examples on how to use graphics.Camera and Matrixin getChildStaticTransformation:

    • ViewPager3d by Inovex. This project is interesting because if you run it as is, the 3d animation is not smooth (on a Galaxy S2). Hoverever, if you strip it of the non-camera/matrix overscroll animations but keep the getChildStaticTransformation 3d effects done with camera and matrix, the 3d effects are smooth.

    • CoverFlow by Neil Davies.

提交回复
热议问题