Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Property Animation Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 246
    Comment on it

    1) Here I will rotate a View around its vertical axis using the rotationY property. we will create an XML resource file in the /res/animator directory named as flip_on_vertical.xml.

    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
      android:propertyName="rotationY"
      android:duration="500"
      android:valueFrom="0"
      android:valueTo="360"
      android:valueType="floatType" />
    

    This defines a rotation around the y-axis from 0 to 360 degrees that will take 500 milliseconds. The propertyName attribute defines the property that should be animated. While rotationY is also a valid view attribute that can be specified in a layout, the ObjectAnimator does not make use of this. The argument type of the method is specified using the valueType attribute. The animation has to be applied to an object. We have applied it to an ImageView that is centred on the screen. We can use a Relative Layout for this purpose.


    2)

    <RelativeLayout 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"
      tools:context=".MainActivity" >
    
      <ImageView
        android:id="@+id/some_image"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:cropToPadding="true"
        android:layout_centerInParent="true"
        android:src="@drawable/ferriswheel"
        android:text="@string/example1"
        android:onClick="flipOnVertical" />
    
    </RelativeLayout>
    

    Now all we need to do is to make the animation run. Now we will write call back method named as `flipOnVertical`.


    3)

        public void flipOnVertical(View view) {
          View image = findViewById(R.id.some_image);
          Animator anim = AnimatorInflater
            .loadAnimator(this, R.animator.flip_on_vertical);
          anim.setTarget(image);
          anim.start();
    
    }
    

    The first line in the method flipOnVertical simply retrieves the ImageView from the layout. The second line creates an Animator object from the XML resource. The method takes a context and a resource id that references the animation . Next we define the target object for the animation using Animator.setTarget. Finally Animator.start will start the animation.

    That's all.Hope it helps:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: