简体   繁体   English

Android规模图像视图与动画

[英]Android scale image view with animation

i have an ImageView and would like to scale it smaller with animation. 我有一个ImageView并希望通过动画缩小它。 i use 我用

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.7"
        android:fromYScale="1.0"
        android:toYScale="0.7"
        android:pivotX="50%"
        android:pivotY="10%"
        android:duration="1500" />
</set>

This works good. 这很好用。 But after the animation is finished the image gets back to its original size. 但是在动画完成后,图像会恢复到原始大小。 Any ideas why? 有什么想法吗?

Try with this code, it will not reset the image size after animation, here view is the imageview you want to animate. 尝试使用此代码,它不会在动画后重置图像大小,此处视图是您要设置动画的imageview。

ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f);
ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f);
scaleDownX.setDuration(1000);
scaleDownY.setDuration(1000);

AnimatorSet scaleDown = new AnimatorSet();
scaleDown.play(scaleDownX).with(scaleDownY);

scaleDown.start();

Ok, i found a solution. 好的,我找到了解决方案。 is this correct like this? 这是正确的吗? or it is quick and dirty? 还是很快又脏? ^^ ^^

    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(image, "scaleX", 0.7f);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(image, "scaleY", 0.7f);
    scaleDownX.setDuration(1500);
    scaleDownY.setDuration(1500);

    ObjectAnimator moveUpY = ObjectAnimator.ofFloat(image, "translationY", -100);
    moveUpY.setDuration(1500);

    AnimatorSet scaleDown = new AnimatorSet();
    AnimatorSet moveUp = new AnimatorSet();

    scaleDown.play(scaleDownX).with(scaleDownY);
    moveUp.play(moveUpY);

    scaleDown.start();
    moveUp.start();

A simplier answer is below; 下面是一个简单的答案;

imageview.animate().translationY(-200F).duration = 500
imageview.animate().scaleY(0.4F).duration = 500
imageview.animate().scaleX(0.4F).duration = 500

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM