Feb 3, 2015

Implement flipping animation horizontally with ObjectAnimator


package com.example.androidflipview;

import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.animation.ObjectAnimator;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

 TextView textTitle;
 Button buttonFlip;
 ImageView image;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textTitle = (TextView)findViewById(R.id.title);
  textTitle.setOnClickListener(MyOnClickListener);
  buttonFlip = (Button)findViewById(R.id.buttonflip);
  buttonFlip.setOnClickListener(MyOnClickListener);
  image = (ImageView)findViewById(R.id.image);
  image.setOnClickListener(MyOnClickListener);

 }
 
 OnClickListener MyOnClickListener = new OnClickListener(){

  @Override
  public void onClick(View v) {
   flipit(v);
  }
  
 };

    private void flipit(final View viewToFlip) {
     ObjectAnimator flip = ObjectAnimator.ofFloat(viewToFlip, "rotationY", 0f, 360f);
     flip.setDuration(3000);
        flip.start();
     
    }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidflipview.MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com"
        android:textSize="28dp"
        android:textStyle="bold" />
    
    <Button
        android:id="@+id/buttonflip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Flip" />
    
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" />
    
</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads