Android - Zoom animation using AnimatorSet

后端 未结 2 1817
我在风中等你
我在风中等你 2020-12-31 06:07

The official Zooming a View tutorial uses an AnimatorSet to zoom into a View. It creates the illusion of downward movement as the view expands. Later, the

2条回答
  •  温柔的废话
    2020-12-31 06:28

    This is the solution I have ultimately used:

    private void applyAnimation(final View startView, final View finishView, long duration) {
        float scalingFactor = ((float)finishView.getHeight())/((float)startView.getHeight());
    
        ScaleAnimation scaleAnimation =  new ScaleAnimation(1f, scalingFactor,
                                                            1f, scalingFactor,
                                                            Animation.RELATIVE_TO_SELF, 0.5f,
                                                            Animation.RELATIVE_TO_SELF, 0.5f);
    
        scaleAnimation.setDuration(duration);
        scaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
    
        Display display = getWindowManager().getDefaultDisplay();
    
        int H;
    
        if(Build.VERSION.SDK_INT >= 13){
            Point size = new Point();
            display.getSize(size);
            H = size.y;
        }
        else{
            H = display.getHeight();
        }
    
        float h = ((float)finishView.getHeight());
    
        float verticalDisplacement = (-(H/2)+(3*h/4));
    
        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0,
                                                                       Animation.ABSOLUTE, 0,
                                                                       Animation.ABSOLUTE, 0,
                                                                       Animation.ABSOLUTE, verticalDisplacement);
    
        translateAnimation.setDuration(duration);
        translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
    
        AnimationSet animationSet = new AnimationSet(false);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);
        animationSet.setFillAfter(false);
    
        startView.startAnimation(animationSet);
    }
    

    The key factor here is the value of toYDelta in the TranslateAnimation parameter:

    toYDelta = (-(H/2)+(3*h/4));
    

    Understanding why this works is the main thing. The rest is mostly simple.

提交回复
热议问题