Table of Contents
There are three types of animation in android:
This blog mainly analyzes the basic use of Tween Animation.
Tween AnimationTween Animation has four forms:
These four animation implementation methods are all implemented through the combination of Animation class and AnimationUtils. Animation effects can be pre-configured in the xml file in the res/anim directory.
The parameters of scale are as follows:
In addition, the Animation class is the base class for all animations (scale, alpha, translate, rotate). Properties inherited from the Animation class:
Talking without practice is not enough. Here are an anim xml file, a layout file and an Activity class to practice the use of scale animation.
res/anim/anim_scale.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="700" android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="150" android:pivotY="300" android:toXScale="1.4" android:toYScale="1.4" android:fillAfter="true" android:repeatCount="10" android:repeatMode="reverse"></scale>
res/layout/tween_animation_layout.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/alpha_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/alpha" /> <Button android:id="@+id/scale_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/scale" /> <Button android:id="@+id/rotate_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/rotate" /> <Button android:id="@+id/trans_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/trans" /> </LinearLayout> <ImageView android:id="@+id/anim_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:contentDescription="@null" android:src="@drawable/splash_logo" /></LinearLayout>
Animation demonstration implementation class is implemented as follows :
import com.example.photocrop.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class TweenAnimationTest extends Activity implements OnClickListener { private Button alphaButton; private Button scaleButton; private Button rotateButton; private Button transButton; private ImageView animImageView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.tween_animation_layout); initView(); } private void initView() { animImageView = (ImageView) findViewById(R.id.anim_image); alphaButton = (Button) findViewById(R.id.alpha_button); scaleButton = (Button) findViewById(R.id.scale_button); rotateButton = (Button) findViewById(R.id.rotate_button); transButton = (Button) findViewById(R.id.trans_button); alphaButton.setOnClickListener(this); scaleButton.setOnClickListener(this); rotateButton.setOnClickListener(this); transButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.alpha_button: break; case R.id.scale_button: Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale); animImageView.startAnimation(animation); break; case R.id.rotate_button: break; case R.id.trans_button: break; } }}
The parameters of alpha are as follows:
Properties inherited from the Animation class:
We use the previous TweenAnimationTest class as a template. When the alpha button is clicked, we trigger the transparency animation effect.
res/anim/anim_alpha.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fillBefore="true" android:fromAlpha="0.0" android:toAlpha="1.0" ></alpha>
The listener event in response to clicking the alpha button is:
case R.id.alpha_button: Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha); animImageView.startAnimation(alphaAnimation); break;
rotate parameters are as follows:
Properties inherited from the Animation class:
res/anim/anim_rotate.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fillAfter="true" android:fromDegrees="0" android:pivotX="0" android:pivotY="0" android:repeatCount="3" android:toDegrees="810" ></rotate>
响应点击alpha button的listener事件为:
case R.id.rotate_button: Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate); animImageView.startAnimation(rotateAnimation); break;
translate参数:
从Animation类继承的属性:
注意:京东splash页面的进度条用的就是translate动画。
res/anim/anim_rotate.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:fromXDelta="0.0" android:toXDelta="60%p" ></translate>
响应点击alpha button的listener事件为:
case R.id.trans_button: Animation transAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_translate); animImageView.startAnimation(transAnimation); break;