Android基础动画
1.Tween Animation 变换动画
2.Frame Animation 帧动画
3.Layout Animation 布局动画
4.Property Animation 属性动画
1.Tween Animation
1)Alpha:渐变透明度动画
基本属性:fromAlpha:动画起始时的透明度
toAlpha:动画终止时的透明度 0.0表示完全透明 1.0 表示完全不透明
2)Scale:渐变尺寸缩放动画
基本属性:
fromX,toX 分别是起始和结束时的X坐标上的伸缩尺寸
fromY,toY 分别是起始和结束时的Y坐标上的伸缩尺寸
pivotX,pivotY 分别为伸缩动画相对于X,Y坐标开始的位置
3)Translate:位置移动动画
基本属性:
fromXDelta,fromYDelta 分别是起始时X,Y的坐标
toXDelta,toYDelta 分别是结束时X,Y的坐标
4)Rotate:旋转动画
基本属性:
fromDegrees 启示的角度
toDegrees 终止的角度
pivotX,pivotY分别为旋转动画相对于X,Y坐标开始的位置
Tween Animation的共同属性:
1)Duration:动画持续时间(单位:毫秒)
2)fillAfter:设置为true,动画转化在动画结束后被应用
3)fillBefore:设置为true,动画转化在动画开始时被应用
4)interpolator:动画插入器(加速、减速插入器)
5)repeatCount:动画重复次数
6)repeatMode:顺序重复/倒序重复
7)startOffset:动画之间的时间间隔
Tween Animation的实现方式:
(1)配置文件(res/anim)------alpha、scale、translate、rotate
(2)Java代码实现------AlphaAnimation、Scal额Animation、TranslateAnimation、RotateAnimation
一、配置文件(res/anim)
1)Alpha:渐变透明度动画
<set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="0.1" android:toAlpha="1.0" > </alpha> </set>2)Scale:渐变尺寸缩放动画
<set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="2000" android:fillAfter="false" android:fromXScale="0.0" android:fromYScale="0.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0" /> </set>3)Translate:位置移动动画
<set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="1000" android:fromXDelta="10" android:fromYDelta="10" android:toXDelta="100" android:toYDelta="100" /> </set>4)Rotate:旋转动画
<set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="1000" android:fromDegrees="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toDegrees="+360" /> </set>
二、Java代码实现
AnimationActivity
package com.example.helloword.fragment; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.example.helloword.ListActivity; import com.example.helloword.R; import java.util.Timer; import java.util.TimerTask; public class AnimationActivity extends Activity implements View.OnClickListener { private Timer timer = null; private TimerTask timeTask = null; private boolean isExit = false; // 标记是否要退出 private ImageView image; private Button scale; private Button rotate; private Button translate; private Button mix; private Button alpha; private Button continue_btn; private Button continue_btn2; private Button flash; private Button move; private Button change; private Button layout; private Button frame; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); scale = (Button) findViewById(R.id.scale); rotate = (Button) findViewById(R.id.rotate); translate = (Button) findViewById(R.id.translate); alpha = (Button) findViewById(R.id.alpha); continue_btn = (Button) findViewById(R.id.continue_btn); continue_btn2 = (Button) findViewById(R.id.continue_btn2); flash = (Button) findViewById(R.id.flash); move = (Button) findViewById(R.id.move); change=(Button) findViewById(R.id.change); layout=(Button) findViewById(R.id.layout); frame=(Button) findViewById(R.id.frame); scale.setOnClickListener(this); rotate.setOnClickListener(this); translate.setOnClickListener(this); alpha.setOnClickListener(this); continue_btn.setOnClickListener(this); continue_btn2.setOnClickListener(this); flash.setOnClickListener(this); move.setOnClickListener(this); change.setOnClickListener(this); layout.setOnClickListener(this); frame.setOnClickListener(this); } @Override public void onClick(View view) { // TODO Auto-generated method stub Animation loadAnimation; switch (view.getId()) { case R.id.scale: { loadAnimation = AnimationUtils.loadAnimation(this, R.anim.scale); image.startAnimation(loadAnimation); break; } case R.id.rotate: { loadAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate); image.startAnimation(loadAnimation); break; } case R.id.translate: { loadAnimation = AnimationUtils .loadAnimation(this, R.anim.translate); image.startAnimation(loadAnimation); break; } case R.id.continue_btn: { loadAnimation = AnimationUtils .loadAnimation(this, R.anim.translate); image.startAnimation(loadAnimation); final Animation loadAnimation2 = AnimationUtils.loadAnimation(this, R.anim.rotate); loadAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation arg0) { // TODO Auto-generated method stub image.startAnimation(loadAnimation2); } }); break; } case R.id.continue_btn2: { loadAnimation = AnimationUtils.loadAnimation(this, R.anim.continue_anim); image.startAnimation(loadAnimation); break; } case R.id.alpha: { loadAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha); image.startAnimation(loadAnimation); break; } case R.id.move: { TranslateAnimation translate = new TranslateAnimation(-50, 50, 0, 0); translate.setDuration(1000); translate.setRepeatCount(Animation.INFINITE); translate.setRepeatMode(Animation.REVERSE); image.startAnimation(translate); break; } case R.id.flash: { AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f); alphaAnimation.setDuration(100); alphaAnimation.setRepeatCount(10); //倒序重复REVERSE 正序重复RESTART alphaAnimation.setRepeatMode(Animation.REVERSE); image.startAnimation(alphaAnimation); break; } case R.id.change: { Intent intent=new Intent(AnimationActivity.this,MainActivity2.class); startActivity(intent); overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out); break; } case R.id.layout: { Intent intent=new Intent(AnimationActivity.this,ListActivity.class); startActivity(intent); break; } case R.id.frame: { image.setImageResource(R.drawable.anim_list); AnimationDrawable ad = (AnimationDrawable) image.getDrawable(); ad.start(); break; } } } @Override public void onBackPressed() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("退出对话框"); builder.setIcon(R.mipmap.ic_launcher); builder.setMessage("确认退出?"); setPositiveButton(builder); setNegativeButton(builder); builder.create(); builder.show(); } private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) { return builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); } private AlertDialog.Builder setNegativeButton(final AlertDialog.Builder builder) { return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); } } 布局文件如下:<?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:layout_marginTop="10dp" android:orientation="vertical" > <Button android:id="@+id/scale" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="ScaleAnimation(缩放动画)" /> <Button android:id="@+id/alpha" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="AlphaAnimation(透明度动画)" /> <Button android:id="@+id/rotate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="RotateAnimation(旋转动画)" /> <Button android:id="@+id/translate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="TranslateAnimation(位移动画)" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/continue_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="续播1" /> <Button android:id="@+id/continue_btn2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="续播2" /> <Button android:id="@+id/flash" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="闪烁" /> <Button android:id="@+id/move" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="抖动" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/change" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="切换动画" /> <Button android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="布局动画" /> <Button android:id="@+id/frame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:paddingBottom="5dp" android:paddingTop="5sp" android:text="逐帧动画" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:src="@mipmap/ic_launcher" > </ImageView> </LinearLayout> </LinearLayout>