Android Property Animation completely parses (lower) and androidproperty

Source: Internet
Author: User

Android Property Animation completely parses (lower) and androidproperty

Reprinted please indicate the source: http://blog.csdn.net/lmj623565791/article/details/38092093

The previous Android Property Animation completely resolved (I) and basically showed the core usage of the Property Animation:

ObjectAnimator implements animation, ValueAnimator implements animation, and the use of AnimatorSet ~

Of course, attribute animation also has some knowledge points, which can also make very good results. This blog will show you ~

1. How to use an xml file to create an attribute Animation

Everyone knows that View Animator and Drawable Animator can all create animations in the anim folder and use them in the program, or even set them as attribute values in Theme. Of course, attribute animation can also be declared in the file:

First, create the animator folder under res, and then create res/animator/scalex. xml

<?xml version="1.0" encoding="utf-8"?><objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:propertyName="scaleX"    android:valueFrom="1.0"    android:valueTo="2.0"    android:valueType="floatType" ></objectAnimator>
Code:

Public void scaleX (View view) {// load the animation Animator anim = AnimatorInflater. loadAnimator (this, R. animator. scalex); anim. setTarget (mMv); anim. start ();}
Use AnimatorInflater to load the animation resource file and set the target. OK ~~ Isn't it easy? It's just a single horizontal magnification ~

What if I want to scale both vertically and horizontally? Then how can we define the property file:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:ordering="together" >    <objectAnimator        android:duration="1000"        android:propertyName="scaleX"        android:valueFrom="1"        android:valueTo="0.5" >    </objectAnimator>    <objectAnimator        android:duration="1000"        android:propertyName="scaleY"        android:valueFrom="1"        android:valueTo="0.5" >    </objectAnimator></set>

When the set tag is used, there is an orderring attribute set to together, and [there is another value: sequentially (indicating one execution after another )].

In the previous blog, I ignored an effect, that is, scaling and reversal all have center points or axes. By default, the center scaling and the center symmetric line are inverted lines, so I decided this horizontal line, vertically narrow down to the center in the upper left corner:

Code:

// Load the animation Animator anim = AnimatorInflater. loadAnimator (this, R. animator. scale); mMv. set0000tx (0); mMv. setPivotY (0); // The displayed call invalidatemMv. invalidate (); anim. setTarget (mMv); anim. start ();

It is very simple. You can directly set Tx and Ty for the View, and then call invalidate.

See the following:

Well, by writing xml declaration animations, using set nested set and combining orderring attributes, you can basically implement any animation ~~ The above also demonstrates the lifecycle settings.

2. Layout Animations)

LayoutTransition is mainly used to set an animation for the layout container. When the view hierarchy in the container changes, there is a transitional animation effect.

The basic code is:

LayoutTransition transition = new LayoutTransition();transition.setAnimator(LayoutTransition.CHANGE_APPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));transition.setAnimator(LayoutTransition.APPEARING,null);transition.setAnimator(LayoutTransition.DISAPPEARING,null);transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,null);mGridLayout.setLayoutTransition(transition);

There are four types of transition:

LayoutTransition. APPEARING when a View appears in the ViewGroupThis ViewSet Animation

LayoutTransition. CHANGE_APPEARING when a View appears in the ViewGroup, this View affects other View locations.Other viewsSet Animation

LayoutTransition. DISAPPEARINGThis ViewSet Animation

LayoutTransition. CHANGE_DISAPPEARING when a View is lost in the ViewGroup, this View has an impact on other View locations.Other viewsSet Animation

LayoutTransition. CHANGE does not affect the positions of other views due to the appearance or disappearance of views, and then the animations set for other views.

Pay attention to who the animation is set on. This View is another View.

Now let's take a comprehensive example:

Layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/id_container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="addBtn"        android:text="addBtns" />    <CheckBox        android:id="@+id/id_appear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="APPEARING" />    <CheckBox        android:id="@+id/id_change_appear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="CHANGE_APPEARING" />    <CheckBox        android:id="@+id/id_disappear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="DISAPPEARING" />    <CheckBox          android:id="@+id/id_change_disappear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="CHANGE_DISAPPEARING " /></LinearLayout>

Code:

Package com. example. zhy_property_animation; import android. animation. layoutTransition; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. checkBox; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import Droid. widget. gridLayout; public class LayoutAnimaActivity extends Activity extends {private ViewGroup viewGroup; private GridLayout mGridLayout; private int mVal; private LayoutTransition mTransition; private CheckBox mAppear, callback, mDisAppear, callback; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. Layout. layout_animator); viewGroup = (ViewGroup) findViewById (R. id. id_container); mAppear = (CheckBox) findViewById (R. id. id_appear); mChangeAppear = (CheckBox) findViewById (R. id. id_change_appear); mDisAppear = (CheckBox) findViewById (R. id. id_disappear); mChangeDisAppear = (CheckBox) findViewById (R. id. id_change_disappear); mAppear. setOnCheckedChangeListener (this); mChangeAppear. setOnCheckedChangeListener (This); mDisAppear. setOnCheckedChangeListener (this); mChangeDisAppear. setOnCheckedChangeListener (this); // create a GridLayoutmGridLayout = new GridLayout (this); // set five buttons for each column. setColumnCount (5); // Add to viewGroup in layout. addView (mGridLayout); // all default animations enable mTransition = new LayoutTransition (); mGridLayout. setLayoutTransition (mTransition);}/*** Add Button ** @ param view */public void addBtn (View view) {final Button bu Tton = new Button (this); button. setText (++ mVal) + ""); mGridLayout. addView (button, Math. min (1, mGridLayout. getChildCount (); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {mGridLayout. removeView (button) ;}}) ;}@ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {mTransition = new LayoutTransition (); mTransition. setAnimator (LayoutTran Sition. APPEARING, (mAppear. isChecked ()? MTransition. getAnimator (LayoutTransition. APPEARING): null); mTransition. setAnimator (LayoutTransition. CHANGE_APPEARING, (mChangeAppear. isChecked ()? MTransition. getAnimator (LayoutTransition. CHANGE_APPEARING): null); mTransition. setAnimator (LayoutTransition. DISAPPEARING, (mDisAppear. isChecked ()? MTransition. getAnimator (LayoutTransition. DISAPPEARING): null); mTransition. setAnimator (LayoutTransition. CHANGE_DISAPPEARING, (mChangeDisAppear. isChecked ()? MTransition. getAnimator (LayoutTransition. CHANGE_DISAPPEARING): null); mGridLayout. setLayoutTransition (mTransition );}}

:


The animation is a little long. Be patient. Be sure to check whether the animation is set for the current View or other Views.

Of course, animations support custom settings and time settings. For example, if we modify the settings, the added animation is:

mTransition.setAnimator(LayoutTransition.APPEARING, (mAppear.isChecked() ? ObjectAnimator.ofFloat(this, "scaleX", 0, 1): null));

The result is:


The original fade-in changes to the effect of enlarging the width from the center ~~ Isn't it good ~~

3. View anim Method

In SDK11, the animation method is added to the View to facilitate animation.

Layout file:

<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"     >    <ImageView        android:id="@+id/id_ball"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/bol_blue" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="viewAnim"            android:text="View Anim" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="propertyValuesHolder"            android:text="PropertyValuesHolder " />            </LinearLayout></RelativeLayout>
Code:

package com.example.zhy_property_animation;import android.animation.ObjectAnimator;import android.animation.PropertyValuesHolder;import android.app.Activity;import android.os.Bundle;import android.util.DisplayMetrics;import android.util.Log;import android.view.View;import android.widget.ImageView;public class ViewAnimateActivity extends Activity{protected static final String TAG = "ViewAnimateActivity";private ImageView mBlueBall;private float mScreenHeight;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.view_animator);DisplayMetrics outMetrics = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(outMetrics);mScreenHeight = outMetrics.heightPixels;mBlueBall = (ImageView) findViewById(R.id.id_ball);}public void viewAnim(View view){// need API12mBlueBall.animate()//.alpha(0)//.y(mScreenHeight / 2).setDuration(1000)// need API 12.withStartAction(new Runnable(){@Overridepublic void run(){Log.e(TAG, "START");}// need API 16}).withEndAction(new Runnable(){@Overridepublic void run(){Log.e(TAG, "END");runOnUiThread(new Runnable(){@Overridepublic void run(){mBlueBall.setY(0);mBlueBall.setAlpha(1.0f);}});}}).start();}                                                                                                                                                  }

Simply use mBlueBall. animate (). alpha (0). y (mScreenHeight/2). setDuration (1000). start () to implement animation ~~ However, SDK11 is required. In SDK12 and SDK16, withStartAction and withenderson action are added respectively to perform operations before and after the animation. Of course, you can also perform operations such as. setListener.

Using ObjectAnimator to implement the above changes, we can use: PropertyValueHolder

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,0f, 1f);PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0,mScreenHeight / 2, 0);ObjectAnimator.ofPropertyValuesHolder(mBlueBall, pvhX, pvhY).setDuration(1000).start();

The effect is the same as above.

Running result:




All right, the usage of attribute animation ends here ~~~~


Download source code





What should I do if I want to see an animation in an android program?

In the Android FrameWork, three Animation implementation methods are provided: Frame-by-Frame Animation, View Animation, and Property Animation ).

Based on the descriptions in the SDK, these three functions are powerful: frame-by-frame <view animation <attribute animation.
I. frame-by-frame animation:
This animation collects all the static images in the animation process, displays these images in sequence, and uses the principle of "visual stay" of the human eye, animation effects for users.

Ii. view animation:
It is also called a Tween animation. According to these two definitions, we can see some features of the animation method:
1) The animation method is only for View objects, such as ImageView and Button;
2) To implement this animation, you only need to provide the relevant attributes of two key frames. Android will give you an animation gradient process for two key frames within a specified period of time.

3. Property Animation:
Android introduces property animation in 3.0. Different from view animation's focus and view effect, view animation focuses more on Object attribute changes and implements animation by changing object attributes, regardless of whether the object is visible or not. For example, if you use a view animation to double a Button, the effect on the interface can be achieved, but the touch response area of the Button is the same as the original one, that is, view animation does not actually double the Button.
Www.linuxidc.com/Linux/2013-01/78069.htm

Android: animation Problems

You can use property animation to adjust translationX or translationY to move the file.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.