Drunken Daddy
Drunken Daddy

Reputation: 7991

Imageview: image change animation

I've these two images. Note that in one of the images, the bird's eye is closed. I want to create an eye blinking animation using these two images. Is it possible to do this using imageswitcher or some other way without having a separate image for eye and animating the eye's change of height? Is so, How?

enter image description here enter image description here

Upvotes: 1

Views: 2916

Answers (2)

AnasAbubacker
AnasAbubacker

Reputation: 3597

You can use frame animation , it is simple to use example like this.

spin_animation.xml file in res/drawable/ folder:

<animation-list android:id="@+id/selected" android:oneshot="false">
        <item android:drawable="@drawable/wheel0" android:duration="50" />
        <item android:drawable="@drawable/wheel1" android:duration="50" />
        <item android:drawable="@drawable/wheel2" android:duration="50" />
        <item android:drawable="@drawable/wheel3" android:duration="50" />
        <item android:drawable="@drawable/wheel4" android:duration="50" />
        <item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>

Here is the code to load and play this animation.

     // Load the ImageView that will host the animation and
     // set its background to our AnimationDrawable XML resource.
     ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
     img.setBackgroundResource(R.drawable.spin_animation); 
    // Get the background, which has been compiled to an    AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation (looped playback by default).
 frameAnimation.start();

Upvotes: 1

Ali Asadi
Ali Asadi

Reputation: 987

you can use ViewFlipper

XML

<ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/img1" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/img2" />
    </ViewFlipper>

in the activity

ViewFlipper mViewFlipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewFlipper = findViewById(R.id.view_flipper);
        mViewFlipper.startFlipping();
        mViewFlipper.setFlipInterval(400);

    }

with setFlipInterval() you set the time to slide between images in milliseconds

Upvotes: 0

Related Questions