GithubHelp home page GithubHelp logo

support_v4_nineoldandroids's Introduction

#Android Support Library v4 with NineOldAndroids

Allows using android Property Animations with Google's Support Library v4 Fragment transitions. Rely's on Jake Wharton's NineOldAndroids.

##Project Configuration

Your project must have NineOldAndroids in the classpath. This can be done with Maven or putting the jar into the /libs folder. This project is packaged as an APK Library to support style resources. Import it into eclipse and reference it as an Android Library. Right-click on project and Properties->Android. Finally remove Google's support v4 library from your classpath.

The latest version is available on the Maven Central Repo

<dependency>
	<artifactId>com.github.kedzie.supportanimator</artifactId>
	<groupId>support-v4-animator</groupId>
	<version>19.0.0</version>
</dependency>

##Usage

Read Google's documentation about Property Animations. They are different from View animations. For example:

Create a file `res/animator/fade_in.xml'

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:zAdjustment="top">
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="1"
        android:valueType="floatType"
        android:propertyName="alpha"
        android:duration="220"/>
</set>

Look at the sample application for a complete example and read below for instructions.

###Animator Fragment Transitions

This fork allows using NineOldAndroids Object Animators for fragment transitions. View animations will also work but only for Custom Transitions.

####Standard Transitions

Specify standard transitions in the transaction.

tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

####Custom Transitions

Specify custom transition animations in the transaction

//Assign Object Animators for transitions
tx.setCustomTransitions(R.animator.flip_left_in, R.animator.flip_left_out, R.animator.flip_right_in, R.animator.flip_right_out)

//View Animations also work
tx.setCustomTransitions(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)

####Fragment Implementation Specified Transitions

Specify animations in Fragment implementation

public class AnimatedFragment extends Fragment {

	//class contents removed

	@Override
	public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
	   //If transaction specifies a custom animation, use it
	   if(nextAnim>0)
		  return AnimatorInflater.loadAnimator(getActivity(), nextAnim);
	   if(enter)
		  return AnimatorInflater.loadAnimator(getActivity(), R.animator.fade_in);
	   else
		  return AnimatorInflater.loadAnimator(getActivity(), R.animator.fade_out);
	}

}

####Transition style resources

Specify transition animations in a style resource.

Create a style resource `res/values/styles.xml'

<?xml version="1.0" encoding="utf-8"?>
<resources>
 	<!-- Override standard Transitions with a Style -->
   	<style name="MyTransitionStyle">
	    <item name="fragmentFadeEnterAnimation">@animator/fade_enter</item>
	    <item name="fragmentFadeExitAnimation">@animator/fade_exit</item>
	    <item name="fragmentOpenEnterAnimation">@animator/flip_left_in</item>
	    <item name="fragmentOpenExitAnimation">@animator/flip_left_out</item>
	    <item name="fragmentCloseEnterAnimation">@animator/flip_right_in</item>
	    <item name="fragmentCloseExitAnimation">@animator/flip_right_out</item>
   	</style>
</resources>

Specify the resource and transition in the transaction

tx.setTransitionStyle(R.style.MyTransitionStyle);
tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

###PageTransformer

The standard ViewPager ignores PageTransformers in pre-HoneyComb API levels. This library supports implementing PageTransformers with NineOldAndroids. Just use ViewHelper instead of the View properties. For example:

/**
 * Implemented using NineOldAndroids ViewHelper instead of newer API View properties
 */
public class ZoomOutPageTransformer implements PageTransformer {
    private static float MIN_SCALE = 0.85f;
    private static float MIN_ALPHA = 0.5f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();
        
        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            ViewHelper.setAlpha(view, 0);
        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                ViewHelper.setTranslationX(view, horzMargin - vertMargin / 2);
            } else {
                ViewHelper.setTranslationX(view, -horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)
            ViewHelper.setScaleX(view, scaleFactor);
            ViewHelper.setScaleY(view, scaleFactor);

            // Fade the page relative to its size.
            ViewHelper.setAlpha(view, MIN_ALPHA +
                    (scaleFactor - MIN_SCALE) /
                    (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            ViewHelper.setAlpha(view, 0);
        }
    }
}

Set the PageTransformer as usual:

viewPager.setPageTransformer(new ZoomOutPageTransformer());

###ActionBarDrawerToggle for Drawer Navigation

Google Support Library r13 added the Navigation Drawer. See the android docs for Drawer Navigation for details. The ActionBarDrawerToggle may be used with ActionBarSherlock to change the up icon on the ActionBar on Gingerbread.

support_v4_nineoldandroids's People

Contributors

adamp avatar sganov avatar stephenhines avatar kedzie avatar alanv avatar tjohns avatar jsharkey avatar scottamain avatar chethaase avatar cwren avatar vsiva avatar scottkennedy avatar dakegu2 avatar labtopia avatar jakewharton avatar dsandler avatar caseyburkhardt avatar xsjojo avatar kkuan2011 avatar andrewstadler avatar kirill-grouchnikov avatar hyperb1iss avatar alexander-mironov avatar onoratoj avatar jafu888 avatar pidelport avatar roman-mazur avatar veeti avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.