Stoian
Stoian

Reputation: 21

How to use scale animation to hide image in Android

Is it possible to hide image using scale animation in xml. What I mean is that I have an image on the screen and I want to hide it using scale animation in the direction from 0.0 -> 1.0. So the image is on the screen and I want to start scaling it from left to right until it disappear. Is it possible?

This is not what I want. You see my app is suporting devices below 3.0. And I don't want to move an image. I want to scale it, but in opposite direction. The scale animation works with the coords of the screen, so the image will be shown if you start from 0.0 to 1.0. But I want to hide it from 0.0 to 1.0. Is it possible?

Upvotes: 1

Views: 651

Answers (2)

Stoian
Stoian

Reputation: 21

I managed to do what I wanted. I used scale animation with the tag pivotX. With pivotX or pivotY you can set from what point of the image to start the animation and in what direction. You have to play a little bit with it until you recive your desired animation. Here is my code:

    <scale
        android:duration="4000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="450"
        android:interpolator="@android:anim/linear_interpolator"
        android:startOffset="300"
        android:toXScale="0.0"
        android:toYScale="1.0" />

Upvotes: 0

Alireza Ahmadi
Alireza Ahmadi

Reputation: 5348

First scaling means changing the size of the image which is not what you want! you want to change the position of an image. With property animation (supported in android 3.0+) you can animate any property of an object (even non visual properties). use this code to animate any property

    ValueAnimator move = ValueAnimator.ofFloat(0,100); //start and ending value
    scaleUp.setDuration(300);
    scaleUp.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            Float newValue = (Float) valueAnimator.getAnimatedValue();
            myObject.setX(newValue); //in pixcels
        }
    });
    move.start();

This code changes the value of "x" property from 0 to 100 over the period of 300ms. x and y are position of your view in pixels. of course you need to first calculate start and ending point in pixels. findout more about "x" property here http://developer.android.com/reference/android/view/View.html#setX(float) you can also animate setTranslationX istead http://developer.android.com/reference/android/view/View.html#setTranslationX(float)

As i said earlier with property animation you can animate any property you want and it's really easy to work with, to learn more about it read this http://developer.android.com/guide/topics/graphics/prop-animation.html

Upvotes: 0

Related Questions