Android--Animation动画介绍和实现

简介: 版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/72472031 1.
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/72472031

1.Animation 动画类型

Android的animation由四种类型组成:

XML中

alph 渐变透明度动画效果
scale 渐变尺寸伸缩动画效果
translate 画面转换位置移动动画效果
rotate 画面转移旋转动画效果

 

 

 

 

 

JavaCode中

AlphaAnimation 渐变透明度动画效果
ScaleAnimation 渐变尺寸伸缩动画效果
TranslateAnimation 画面转换位置移动动画效果
RotateAnimation 画面转移旋转动画效果

 

 

 

 

 2.Android动画模式

Animation主要有两种动画模式:

一种是tweened animation(渐变动画)

XML中 JavaCode
alpha AlphaAnimation
scale ScaleAnimation

 

 

 

一种是frame by frame(画面转换动画)

XML中 JavaCode
translate TranslateAnimation
rotate RotateAnimation

 

 

 

3.如何在XML文件中定义动画

步骤如下:

①新建 Android 项目

在res目录中新建anim文件夹

在anim目录中新建一个my_anim.xml(注意文件名小写)

④在 my_anim.xml 加入动画代码

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

importandroid.view.animation.AnimationSet;

importandroid.view.animation.RotateAnimation;

importandroid.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class Animation1Activity extends Activity {

    private Button rotateButton = null;

    private Button scaleButton = null;

    private Button alphaButton = null;

    private Button translateButton = null;

    private ImageView image = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        rotateButton = (Button)findViewById(R.id.rotateButton);

        scaleButton = (Button)findViewById(R.id.scaleButton);

        alphaButton = (Button)findViewById(R.id.alphaButton);

        translateButton = (Button)findViewById(R.id.translateButton);

        image = (ImageView)findViewById(R.id.image);

     

        rotateButton.setOnClickListener(newRotateButtonListener());

        scaleButton.setOnClickListener(newScaleButtonListener());

        alphaButton.setOnClickListener(newAlphaButtonListener());

        translateButton.setOnClickListener(

           new TranslateButtonListener());

    }

    class AlphaButtonListener implementsOnClickListener{

       public void onClick(View v) {

           //创建一个AnimationSet对象,参数为Boolean型,

           //true表示使用Animation的interpolator,false则是使用自己的

           AnimationSet animationSet = new AnimationSet(true);

           //创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明

           AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

           //设置动画执行的时间

           alphaAnimation.setDuration(500);

           //将alphaAnimation对象添加到AnimationSet当中

           animationSet.addAnimation(alphaAnimation);

           //使用ImageView的startAnimation方法执行动画

           image.startAnimation(animationSet);

       }

    }

    class RotateButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           //参数1:从哪个旋转角度开始

           //参数2:转到什么角度

           //后4个参数用于设置围绕着旋转的圆的圆心在哪里

           //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标

           //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴

           //参数5:确定y轴坐标的类型

           //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴

           RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           rotateAnimation.setDuration(1000);

           animationSet.addAnimation(rotateAnimation);

           image.startAnimation(animationSet);

       }

    }

    class ScaleButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           //参数1:x轴的初始值

           //参数2:x轴收缩后的值

           //参数3:y轴的初始值

           //参数4:y轴收缩后的值

           //参数5:确定x轴坐标的类型

           //参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴

           //参数7:确定y轴坐标的类型

           //参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴

           ScaleAnimation scaleAnimation = new ScaleAnimation(

                  0, 0.1f,0,0.1f,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           scaleAnimation.setDuration(1000);

           animationSet.addAnimation(scaleAnimation);

           image.startAnimation(animationSet);

       }

    }

    class TranslateButtonListener implementsOnClickListener{

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           //参数1~2:x轴的开始位置

           //参数3~4:y轴的开始位置

           //参数5~6:x轴的结束位置

           //参数7~8:x轴的结束位置

           TranslateAnimation translateAnimation =

              new TranslateAnimation(

                  Animation.RELATIVE_TO_SELF,0f,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           translateAnimation.setDuration(1000);

           animationSet.addAnimation(translateAnimation);

           image.startAnimation(animationSet);

       }

    }

}
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

         <Button

            android:id="@+id/rotateButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="旋转" />

         <Button

            android:id="@+id/scaleButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="缩放" />

         <Button

            android:id="@+id/alphaButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="淡入淡出" />

         <Button

            android:id="@+id/translateButton"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="移动" />

    </LinearLayout>

     <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="vertical" >

         <ImageView

            android:id="@+id/image"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"

            android:src="@drawable/an" />

    </LinearLayout>

 </LinearLayout>



目录
相关文章
|
2月前
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
49 2
基于Android P,自定义Android开机动画的方法
|
5月前
|
Java Android开发 开发者
Android10 修改开发者选项中动画缩放默认值
Android10 修改开发者选项中动画缩放默认值
135 0
|
5月前
|
XML Java Android开发
android的三种动画
android的三种动画
34 0
|
3月前
|
XML Android开发 数据格式
Android 中如何设置activity的启动动画,让它像dialog一样从底部往上出来
在 Android 中实现 Activity 的对话框式过渡动画:从底部滑入与从顶部滑出。需定义两个 XML 动画文件 `activity_slide_in.xml` 和 `activity_slide_out.xml`,分别控制 Activity 的进入与退出动画。使用 `overridePendingTransition` 方法在启动 (`startActivity`) 或结束 (`finish`) Activity 时应用这些动画。为了使前 Activity 保持静止,可定义 `no_animation.xml` 并在启动新 Activity 时仅设置新 Activity 的进入动画。
63 12
|
3月前
|
XML Android开发 UED
Android动画之共享元素动画简单实践
本文介绍Android共享元素动画, 实现两Activity间平滑过渡特定UI元素。通过设置`transitionName`属性和使用`ActivityOptions.makeSceneTransitionAnimation`启动目标Activity实现动画效果。可自定义过渡动画提升体验。
48 0
|
4月前
|
Android开发 UED
Android Item平移动画
【6月更文挑战第18天】
|
3月前
|
Android开发
android 动画 插值器和估值器
android 动画 插值器和估值器
|
3月前
|
Android开发 容器
android animation clipToPadding clipChildren
android animation clipToPadding clipChildren
|
5月前
|
数据库 Android开发
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
|
5月前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
114 1
Android开发之使用OpenGL实现翻书动画