Use ObjectAnimator

Description

The following code shows how to Use ObjectAnimator.

Example

Main layout xml file


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <ImageView
    android:id="@+id/flip_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
  />
</RelativeLayout>

Main Activity Java code


package com.java2s.myapplication3.app;
/*ww w .  ja  va 2  s  .  co m*/
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends Activity {

    boolean isHeads;
    ObjectAnimator flipper;
    Bitmap headsImage, tailsImage;
    ImageView flipImage;

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

        headsImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        tailsImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        flipImage = (ImageView)findViewById(R.id.flip_image);
        flipImage.setImageBitmap(headsImage);
        isHeads = true;

        flipper = ObjectAnimator.ofFloat(flipImage, "rotationY", 0f, 360f);
        flipper.setDuration(1500);
        flipper.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (animation.getAnimatedFraction() >= 0.25f && isHeads) {
                    flipImage.setImageBitmap(tailsImage);
                    isHeads = false;
                }
                if (animation.getAnimatedFraction() >= 0.75f && !isHeads) {
                    flipImage.setImageBitmap(headsImage);
                    isHeads = true;
                }
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            if (flipper.isPaused()) {
                flipper.resume();
            } else {
                flipper.start();
            }
            return true;
        }
        if(event.getAction() == MotionEvent.ACTION_UP) {
            flipper.pause();
            return true;
        }
        return super.onTouchEvent(event);
    }
}
Use ObjectAnimator




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView