Android图像相对于中心点缩放动画

Android图像相对于中心点缩放动画,android,android-layout,android-animation,scale,Android,Android Layout,Android Animation,Scale,我有一个ImageView,我对它做了一个简单的缩放动画。非常标准的代码 My scale_up.xml: <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1" android:fromYScale="1" android:toXScale="1.2" android:t

我有一个ImageView,我对它做了一个简单的缩放动画。非常标准的代码

My scale_up.xml:

<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"/>
</set>
问题是:

当图像缩放时,它不是从中心缩放,而是从左上角缩放。换句话说,图像的缩放区域与中心点不相同,但具有相同的左上角点。第一个图像是动画的缩放方式,第二个图像是我希望它的缩放方式。它应该保持中心点不变。我试着在图像上设置重力,在容器上,左对齐或右对齐,它的比例总是一样的。
我在主屏幕上使用RelativeLayout,而ImageView位于另一个RelativeLayout中,但我尝试了其他布局,没有任何更改。

您可以使用集合中的translate动画来抵消该设置。您可能需要调整toXDelta和toYDelta值以使其正确,从而保持图像居中

<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>


忘记额外的翻译,将
android:pivotX
android:pivotY
设置为宽度和高度的一半,它将从图像中心缩放。

50%
是动画视图的中心

50%p
是父项的中心

<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"/>

由@stevanveltema和@JiangQi提供的答案非常完美,但如果您想使用代码进行缩放,那么您可以使用我的答案

// 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);

对于我来说,50%的人做了这项工作(没有p),如果它与应用动画的组件宽度或高度相关,那么应该没有p。p指应用动画的组件的父级。如何在Java文件中使用
50%p
,而不是XML?新的缩放动画(1.0f、1.2f、1.0f、1.2f、动画。相对于父级,0.5f,动画。相对于父级,0.5f);在px中还有一个“a”-Animation.ABSOLUTE要设置。@T.Todua有什么错误?请提出一个新问题并链接回此答案。如果我想要无限次重复动画,例如“`android:repeatCount=“infinite”android:repeatMode=“reverse”``
pivotX
pivotY
应该精确地设置为
viewportWidth
viewportHeight
的一半,该怎么办。
// 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);