Android image scale animation relative to center point

后端 未结 4 1798
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 17:37

I have an ImageView and I do a simple scale animation to it. Very standard code.

My scale_up.xml:



        
相关标签:
4条回答
  • 2020-12-22 18:04

    50% is center of animated view.

    50%p is center of parent

    <scale
        android:fromXScale="1.0"
        android:toXScale="1.2"
        android:fromYScale="1.0"
        android:toYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="175"/>
    
    0 讨论(0)
  • 2020-12-22 18:05

    The answer provided by @stevanveltema and @JiangQi are perfect but if you want scaling using code, then you can use my answer.

    // first 0f, 1f mean scaling from X-axis to X-axis, meaning scaling from 0-100%
    // first 0f, 1f mean scaling from Y-axis to Y-axis, meaning scaling from 0-100%
    // The two 0.5f mean animation will start from 50% of X-axis & 50% of Y-axis, i.e. from center
    
    ScaleAnimation fade_in =  new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    fade_in.setDuration(1000);     // animation duration in milliseconds
    fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
    view.startAnimation(fade_in);
    
    0 讨论(0)
  • 2020-12-22 18:08

    You can use the translate animation in your set to offset that. You'll probably need to tweak the toXDelta and toYDelta values to get it right so it keeps the image centered.

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale android:fromXScale="1"
            android:fromYScale="1"
            android:toXScale="1.2"
            android:toYScale="1.2"
            android:duration="175"/>
        <translate
            android:fromXDelta="0"
            android:fromYDelta="0"
            android:toXDelta="-20%"
            android:toYDelta="-20%"
            android:duration="175"/>
    </set>
    
    0 讨论(0)
  • 2020-12-22 18:20

    Forget the additional translation, set android:pivotX, android:pivotY to half the width and height and it will scale from the center of the image.

    0 讨论(0)
提交回复
热议问题