从底部中心到屏幕中心缩放和过渡图像

时间:2014-12-31 21:48:55

标签: android animation scale

我想将图像从屏幕的底部中心缩放到屏幕的中心。这是我的动画xml,图像从中心左上方到屏幕中心缩放。请帮我理解这里有什么问题。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator" >



    <translate
        android:duration="750"
       android:fromYDelta="100%"
        android:toYDelta="0%" 
        android:fromXDelta="0%"
        android:toXDelta="0%" />

        <scale
            android:duration="500"
            android:fromXScale="0.1"
            android:fromYScale="0.1"
            android:pivotX="0.5"
            android:pivotY="0.5"
            android:toXScale="1"
            android:toYScale="1" 
            android:fillBefore="false" />

</set>

1 个答案:

答案 0 :(得分:2)

private void moveViewToScreenCenter( final View view ){
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics( dm );

        int originalPos[] = new int[2];
        view.getLocationOnScreen( originalPos );

        int xDelta = (dm.widthPixels - view.getMeasuredWidth() - originalPos[0])/2;
        int yDelta = (dm.heightPixels - view.getMeasuredHeight() - originalPos[1])/2;

        AnimationSet animSet = new AnimationSet(true);
        animSet.setFillAfter(true);
        animSet.setDuration(1000);
        animSet.setInterpolator(new BounceInterpolator());
        TranslateAnimation translate = new TranslateAnimation( 0, xDelta , 0, yDelta);
        animSet.addAnimation(translate);
        ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f, ScaleAnimation.RELATIVE_TO_PARENT, .5f, ScaleAnimation.RELATIVE_TO_PARENT, .5f);
        animSet.addAnimation(scale);
        view.startAnimation(animSet);
    }