android中三种动画的简单介绍

前言  

在学习了Android的动画之后,简单总结了一下,看完这篇文章就可以自己动手写一个简单的动画了

正文

Android中的动画可分为三种:

1、逐帧动画 FrameAnimation

2、补间动画 TweenAnimation

3、属性动画 PropertyAnimation

本文就来简单的介绍一下这三种动画

一、逐帧动画:FrameAnimation

          每一张静止的动画依次显示出来。利用人眼暂时停留的错觉,得出的动画。

首先在drawable文件中添加文件。资源文件一般存放在res/drawable/目录当中。


帧动画的使用步骤

1、在drawable文件夹下创建帧动画的资源文件frame_animlist.xml 代码如下:

<animation-listxmlns:android="http://schemas.android.com/apk/res/android">

<item

          android:drawable="@drawable/icon_open"

          android:duration="200"/>

<item

          android:drawable="@drawable/icon_hell"

          android:duration="200"/>

</animation-list>

其中:

          duration:表示持续的时间

          设置android:oneshot="false":设置为true:就表示指定的图片只切换一次

                                                     设置为false:就表示指定的item图片无限切换。默认为无限切换

2、在布局文件中用ImageView控件作为动画载体来显示动画,可设置两个button按钮来开启和停止动画

<ImageView

android:id="@+id/iv"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"/>

<Button

android:id="@+id/bt_start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:text="start"/>

<Button

android:id="@+id/bt_stop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="stop"

android:layout_alignParentBottom="true"

android:layout_toRightOf="@+id/bt_start"

android:layout_toEndOf="@+id/bt_start"

android:layout_marginLeft="26dp"

android:layout_marginStart="26dp"/>


3、在java代码中设置控件的背景为动画资源文件。

iv. setBackgroundResource();

4、声明动画管理器,AnimationDrawable,通过获得控件的背景,对其进行初始化。

AnimationDrawable  anima;//声明一个动画管理器对象

mImageView.setBackgroundResource(R.drawable.frame_animlist);

anima= (AnimationDrawable)mImageView.getBackground();

5、调用动画管理器的start,stop。开启和停止动画。


完整代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private ImageView mImageView;

private Button mBtStart,mBtStop;

private AnimationDrawable anima;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView= (ImageView) findViewById(R.id.iv);

mBtStart= (Button) findViewById(R.id.bt_start);

mBtStop= (Button) findViewById(R.id.bt_stop);

mImageView.setBackgroundResource(R.drawable.frame_animlist);

anima= (AnimationDrawable)mImageView.getBackground();

mBtStart.setOnClickListener(this);

mBtStop.setOnClickListener(this);

}

@Override

public voidonClick(View view) {

switch(view.getId()){

caseR.id.bt_start:

anima.start();

break;

caseR.id.bt_stop:

anima.stop();

break;

default:

break;

}

}

}

二、补间动画:TweenAnimation

补间动画是指开发者只需要提供动画开始和结束的关键帧,二动画中间变化的帧,由系统计算,自己补充完整

资源文件一般存放在res/anim/目录当中。

提供了4中变化的方式。

位移的改变,旋转,透明度,大小缩放动画。

<translate . . ./>

<rotate . . ./>

<alpha . . ./>

<scale . . ./>

可以指定动画变化的时间,和动画变化的速度。

补间动画的使用步骤:

1、在drawable文件夹下创建anim文件夹,在anim文件夹下创建对应的变化方式的xml文件

a、translate_anim.xml:(位移的改变)

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

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromXDelta="0"

android:toXDelta="300"

android:fromYDelta="0"

android:toYDelta="0"

android:duration="2000"/>

其中:

android:interpolator 动画的渲染器

fromXDelta  动画起始位置的横坐标

toXDelta    动画起结束位置的横坐标

fromYDelta  动画起始位置的纵坐标

toYDelta  动画结束位置的纵坐标

duration 动画的持续时间-->

b、rotate_anim.xml:(旋转)

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

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromDegrees="0"

android:toDegrees="360"

android:duration="1000"

android:repeatCount="1"

android:repeatMode="restart"/>

其中:

fromDegrees:表示旋转的起始角度

toDegrees:表示旋转的结束角度

repeatCount:旋转的次数  默认值是0 代表旋转1次  如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止

repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。

repeatCount=-1 或者infinite 循环了

还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。-->

c、alpha_anim.xml(透明度):

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

android:interpolator="@android:anim/accelerate_decelerate_interpolator"

android:fromAlpha="1.0"

android:toAlpha="0.1"

android:duration="2000"/>

其中:

fromAlpha :表示起始透明度

toAlpha:表示结束透明度

1.0表示完全不透明

0.0表示完全透明

d、scale_anim.xml:(大小缩放动画)

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

android:interpolator="@android:anim/accelerate_interpolator"

android:fromXScale="0.2"

android:toXScale="1.5"

android:fromYScale="0.2"

android:toYScale="1.5"

android:pivotX="50%"

android:pivotY="50%"

android:duration="2000"/>

其中

fromXScale:表示沿着x轴缩放的起始比例

toXScale:表示沿着x轴缩放的结束比例

fromYScale:表示沿着y轴缩放的起始比例

toYScale:表示沿着y轴缩放的结束比例

pivotX、pivotY 表示图片大小缩放轴点

2、在布局文件中用ImageView控件作为动画载体来显示动画,同样可设置button按钮来显示不同的动画

3、声明Animation

Animation animation= AnimationUtils.loadAnimation(this,R.anim.translate_anim);;

animation.setRepeatCount(Animation.INFINITE);//循环显示

mImageView.startAnimation(animation);

补充:当然可以将这些动画设置在一起在anim文件夹下创建all_anim.xml使这些动画同时实现

完整代码如下:

public class MainActivity extends AppCompatActivity implementsView.OnClickListener {

private ImageView mImageView;

private Button mBtTranslate,mBtRotate,mBtScale,mBtAlpha,mBtAll;

private Animationanimation;

@Override

protected void  onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView= (ImageView) findViewById(R.id.iv);

mBtTranslate= (Button) findViewById(R.id.bt_translate);

mBtRotate= (Button) findViewById(R.id.bt_rotate);

mBtScale= (Button) findViewById(R.id.bt_scale);

mBtAlpha= (Button) findViewById(R.id.bt_alpha);

mBtAll= (Button) findViewById(R.id.bt_all);

mBtTranslate.setOnClickListener(this);

mBtRotate.setOnClickListener(this);

mBtScale.setOnClickListener(this);

mBtAlpha.setOnClickListener(this);

mBtAll.setOnClickListener(this);

}

@Override

public void  onClick(View view) {

switch(view.getId()){

caseR.id.bt_translate:

animation= AnimationUtils.loadAnimation(this,R.anim.translate_anim);

break;

caseR.id.bt_rotate:

animation= AnimationUtils.loadAnimation(this,R.anim.rotate_anim);

break;

caseR.id.bt_scale:

animation= AnimationUtils.loadAnimation(this,R.anim.scale_anim);

break;

caseR.id.bt_alpha:

animation= AnimationUtils.loadAnimation(this,R.anim.alpha_anim);

break;

caseR.id.bt_all:

animation= AnimationUtils.loadAnimation(this,R.anim.all_anim);

}

animation.setRepeatCount(Animation.INFINITE);//循环显示

mImageView.startAnimation(animation);

}

}

三、属性动画 :PropertyAnimation

补间动画的增强版

属性动画的两大特点:

1、可以定义任何属性的变化

2、可以对任何对象执行动画

属性动画能够定义的属性:

1、动画的持续时间

2、动画的插入方式

3、动画的重复次数

4、动画的行为

5、动画集

6、每一帧刷新的频率

. . . . . . . .

属性动画的使用步骤:

1、在drawable文件夹下创建animator文件夹,在animator文件夹下创建对应的object_anim_x.xml文件 内容如下:

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

android:propertyName="scaleX"  //想要变化的属性名称

android:duration="5000"  //改变的时间

android:valueType="floatType"  //改变的值的类型

android:valueFrom="1.0"  //改变前的值

android:valueTo="3.0">    //改变后的值

</objectAnimator>

2、同样的需要一个ImageView控件作为动画载体来显示动画

3、声明ObjectAnimator

ObjectAnimator  objectAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(this,

R.animator.object_anim_x);

objectAnimation . setTarget(mImageView);

objectAnimation . start();

完整代码如下:

public class MainActivity extends AppCompatActivity {

privateImageViewmImageView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mImageView= (ImageView) findViewById(R.id.iv);

ObjectAnimator objectAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(this,

R.animator.object_anim_x);

objectAnimation . setTarget(mImageView);

objectAnimation . start();

}

}

属性动画很强大,这里只列举了其中一种,补间动画能够对View进行位移的改变,旋转,透明度和大小缩放,但是一旦超过这四种动画补间动画就无能为力了,此时我们可以使用属性动画来实现我们想要达到的效果。属性动画相当于补间动画的增强版,是3.0后推出的动画,使用简单、容易实现,当然也有一定的局限性,就是需要3.0以上的API支持,限制较大。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容