Tom*_*ski 80 android scale android-animation android-layout
我有一个ImageView,我做了一个简单的比例动画.非常标准的代码.
我的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>
Run Code Online (Sandbox Code Playgroud)
我的动画代码:
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale_up);
((ImageView) findViewById(R.id.circle_image)).startAnimation(a);
Run Code Online (Sandbox Code Playgroud)
问题:
当图像缩放时,它不会从中心缩放,而是从左上角缩放.换句话说,图像的缩放版本与中心不具有相同的点,但它具有相同的左上角点.这是一个解释我的意思的链接.第一个图像是动画如何缩放,第二个图像是我希望它如何缩放.它应该保持中心点相同.我已尝试在图像上设置重力,在容器上,左对齐或右对齐,它始终按比例缩放.我正在使用RelativeLayout作为主屏幕,ImageView位于另一个RelativeLayout,但我尝试了其他布局,没有改变.
Jia*_* Qi 149
50% 是动画视图的中心.
50%p 是父母的中心
<scale
android:fromXScale="1.0"
android:toXScale="1.2"
android:fromYScale="1.0"
android:toYScale="1.2"
android:pivotX="50%p"
android:pivotY="50%p"
android:duration="175"/>
Run Code Online (Sandbox Code Playgroud)
Roh*_*wal 108
@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);
Run Code Online (Sandbox Code Playgroud)
小智 71
忘了额外的转换,设置android:pivotX,android:pivotY宽度和高度的一半,它会从图像的中心规模.
您可以使用集合中的平移动画来抵消它.您可能需要调整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>
Run Code Online (Sandbox Code Playgroud)