Feb 5, 2015

Flip view vertically


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.LinearLayout;
import android.widget.TextView;
import android.animation.ObjectAnimator;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

 LinearLayout mainLayout;
 TextView textTitle;
 Button buttonFlip;
 ImageView image;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mainLayout = (LinearLayout)findViewById(R.id.mainlayout);
  mainLayout.setOnClickListener(MyOnClickListener);
  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, "rotationX", 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:id="@+id/mainlayout"
    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="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    
</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads