Android Animation介绍

Android Animation是Android提供的实现UI界面动画效果的API,Animation提供了一系列的动画效果。Android SDK提供了两种Animation:

  1. Tween Animation(渐变动画): 通过对特定的对象做图像变换如平移、缩放、旋转、淡出/淡入等产生动画效果
  2. Frame Animation(帧动画): 创建一个Drawable序列,这些Drawable可以按照指定的时间间隔一个一个的显示,也就是顺序播放事先做好的图像
Tween Animation(渐变动画):
  • AlphaAnimation:渐变透明度动画效果
  • RotateAnimation:旋转动画效果
  • TranslateAnimation:移动动画效果
  • ScaleAnimation:渐变尺寸伸缩动画效果
Frame Animation(帧动画):
  • 在XML资源中定义Animation,使用AnimationUtils中的loadAnimation()函数加载动画
  • 使用Animation子类的构造函数来初始化Animation对象

Android Animation使用

1.界面配置

activity_main.xml文件中配置:

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    android:orientation="vertical"    tools:context="com.example.icarus.learnanimation.MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/anamationme"        android:id="@+id/btnAlphaAnimation" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/rotateanimation"        android:id="@+id/btnRotateAnimation"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/translateanimation"        android:id="@+id/btnTranslateAnimation"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/scaleanimation"        android:id="@+id/btnScaleAnimation"/></LinearLayout>


2.界面效果:

3.给按钮添加Animation动画效果

有两种方式给按钮添加动画效果,一种是直接在代码中编写相应的控制语句,另外一种是在xml资源文件中配置相应的动画属性,然后使用AnimationUtils中的loadAnimation()函数加载动画。

  1. 渐变透明度动画效果按钮功能实现(代码实现)
btnAlphaAnimation = (Button) findViewById(R.id.btnAlphaAnimation);btnAlphaAnimation.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        //使用代码方式配置按钮动画效果        AlphaAnimation aa=new AlphaAnimation(0,1);//透明度0到1之间的动画效果        aa.setDuration(1000);//设置动画时间长度        v.startAnimation(aa);//给按钮指明动画效果    }});   2.渐变透明度动画效果按钮功能实现(资源文件实现)在res中新建Resource File,主要配置信息如下:File name:aaResource type:AnimationRoot element:alphaSource set:mainDirectory name:animaa.xml中编写:
<?xml version="1.0" encoding="utf-8"?><!--用xml资源文件实现对按钮透明动画的配置--><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="0"    android:toAlpha="1"    android:duration="1000"></alpha>此时按钮点击事件中便可以使用资源文件来配置按钮点击动画:
btnAlphaAnimation = (Button) findViewById(R.id.btnAlphaAnimation);btnAlphaAnimation.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View v) {        v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.aa));    }});这样透明效果按钮的Animation动画效果就添加完成。

4.其他按钮资源配置文件:

  • 旋转效果按钮:
<?xml version="1.0" encoding="utf-8"?><!--使用xml配置文件实现按钮旋转效果--><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromDegrees="0"    android:toDegrees="360"    android:duration="1000"    android:pivotX="50%"    android:pivotY="50%"></rotate>
  • 移动效果按钮:
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXDelta="0"    android:toXDelta="300%"    android:fromYDelta="0"    android:toYDelta="0"    android:duration="1000"></translate>
  • 缩放效果按钮:
<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:fromXScale="0"    android:toXScale="1"    android:fromYScale="0"    android:toYScale="1"    android:duration="1000"    android:pivotX="50%"    android:pivotY="50%"></scale>

完整java代码

package com.example.icarus.learnanimation;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;/** * 给按钮实现动画效果 */public class MainActivity extends AppCompatActivity {    private Button btnAlphaAnimation;    private Button btnRotateAnimation;    private Button btnTranslateAnimation;    private Button btnScaleAnimation;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnAlphaAnimation = (Button) findViewById(R.id.btnAlphaAnimation);        btnAlphaAnimation.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {//                使用代码方式配置按钮动画效果//                AlphaAnimation aa=new AlphaAnimation(0,1);//透明度0到1之间的动画效果//                aa.setDuration(1000);//设置动画时间长度//                v.startAnimation(aa);//给按钮指明动画效果//                使用xml文件配置动画效果                v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.aa));            }        });        btnRotateAnimation= (Button) findViewById(R.id.btnRotateAnimation);        btnRotateAnimation.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //相对于自身中间点旋转//                使用代码方式配置按钮动画效果//                RotateAnimation ra=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//                ra.setDuration(1000);//                v.startAnimation(ra);//                使用xml配置文件实现按钮旋转效果                v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.ra));            }        });        btnTranslateAnimation= (Button) findViewById(R.id.btnTranslateAnimation);        btnTranslateAnimation.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {//                使用代码方式配置按钮动画效果//                TranslateAnimation ts=new TranslateAnimation(0,600,0,0);//                TranslateAnimation ts=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,3f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);//                ts.setDuration(1000);//                v.startAnimation(ts);//                使用xml配置文件实现按钮旋转效果                v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.ta));            }        });        btnScaleAnimation= (Button) findViewById(R.id.btnScaleAnimation);        btnScaleAnimation.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {//                使用代码方式配置按钮动画效果//                ScaleAnimation sa=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);//                sa.setDuration(1000);//                v.startAnimation(sa);//                使用xml配置文件实现按钮旋转效果                v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.sa));            }        });    }}

更多相关文章

  1. Android切换Activity时的淡入动画和缩小动画
  2. Android 关闭虚拟按钮、底部导航条
  3. Android 中关于PathEffect子类的效果(中级)
  4. Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]
  5. android中实现tab标签切换时的一些效果
  6. Android dialog回弹效果
  7. android tab选项卡效果
  8. Android中播放GIF图片动画

随机推荐

  1. 2011.09.07——— android zxing 条形码
  2. android 9.0 增加实体按键的按键声音,以及
  3. android 创建快捷方式图标
  4. android源码分享之蓝虫火车票余票查询源
  5. android与webservice通信之中文乱码问题!
  6. android通过shape.xml制作渐变背景
  7. Android listview中使用button解决方法
  8. android apilevel和android系统版本对应
  9. Android动态壁纸开发
  10. android PhoneGap JQuery Mobile Demo