How to make Rotate3dAnimation more smoother?

前端 未结 4 874
北荒
北荒 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条回答
  •  天命终不由人
    2020-12-30 07:01

    I have made the animation like this. I had the same problems. So, suggestions:

    • make xml layout as simple as possible, you can test it using Hierarchy View tool in android. It tool shows the time of building and drawing the laoyuts;

    • images on layout should have so low weight as possible;

    • use hardware acceleration if your device supports it (in manifest):

    • I have noticed one more interesing behavior. If I call some code in onAnimationEnd(Animation animation) method, the animation freezes for a short time. This problem I was solved using next construction:

      private static final int DELAY_AFTER_ANIMATION = 10;

      public void onAnimationEnd(Animation animation) { new Handler().postDelayed(new Runnable() { @Override public void run() { setData(); // do the heavy code here } }, DELAY_AFTER_ANIMATION); }

    To construct animation I used the same code (Rotate3dAnimation). For calling animation(main difference is using the isReverse parameter):

    public void apply3dRotation(float start, float end, AnimationListener listener, boolean isReverse) {
        View view = getRotateView();
        if(view == null){
            return;
        }
    
        if (isHardwareAcceleartionNotSupported()){
            AndroidHelper.disableHardwareAccelerationOnView(view, this.getClass());
        }
    
        final float centerX = view.getWidth() / 2.0f;
        final float centerY = view.getHeight() / 2.0f;
    
        Flip3dAnimation rotation;
    
        rotation = new Flip3dAnimation(start, end, centerX, centerY, 310.0f, isReverse);
        rotation.setDuration(ANIMATION_DURATION);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
    
        if(listener != null){
            rotation.setAnimationListener(listener);
        }
    
        view.startAnimation(rotation);
    }
    

    isHardwareAcceleartionNotSupported() method checks the OS version. In my project I disabled acceleration for smartphones. In AndroidHelper class:

    public static void disableHardwareAccelerationOnView(View view, Class c){
        try {
            view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        } catch (Error e) {
            Log.i(c.getSimpleName(), e.getMessage());
        }
    }
    

    And one problem yet. If the animation hides when screen rotates to 90 degrees, it's a problem of a camera. In this case we should to place image farther from the spectator.

    • And one more trick - sometimes I set the pause before animation start:

        animation.setStartOffset(100);
      

提交回复
热议问题