Android中动画的使用

来源:互联网 发布:淘宝宠爱之名是真的吗 编辑:程序博客网 时间:2024/04/28 02:54

1,须知:

在安卓中动画分为帧动画和补间动画:

帧动画(Frame):即指定每一帧的内容和停留时间,然后播放动画

补间动画:是指通过指定View的初末状态和变化时间、方式,对View的内容完成一系列的图形变换来实现动画效果。主要包括四种效果:Alpha、Translate、Scale和Rotate。

一,帧动画(Frame):

XML布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_cartoon"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="readgroup.zhuoxin.com.myitems.Cartoon.CartoonActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_margin="10dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="帧动画:"            />        <ImageView            android:id="@+id/iv1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="10dp"            android:src="@drawable/animation"            />    </LinearLayout></LinearLayout>

animation如下(文件存放在res/drawable目录下

顺序显示动画文件):

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true"    >    <!--oneshot代表着是否只展示一遍-->    <!--设置图片和,显示时间-->    <item android:drawable="@drawable/ic1" android:duration="150"></item>    <item android:drawable="@drawable/ic2" android:duration="150"></item>    <item android:drawable="@drawable/ic3" android:duration="150"></item>    <item android:drawable="@drawable/ic4" android:duration="150"></item>    <item android:drawable="@drawable/ic5" android:duration="150"></item>    <item android:drawable="@drawable/ic6" android:duration="150"></item></animation-list>

效果图:
这里写图片描述

二,补间动画

Activity如下:

public class CartoonActivity extends AppCompatActivity {    @BindView(R.id.iv2)//findViewById    ImageView imageview;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_cartoon);        ButterKnife.bind(this);//绑定黄油刀    }    @OnClick(R.id.ap1)//点击事件,(黄油刀)    public void alpha(){//渐变        Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);//获取Animation        imageview.startAnimation(animation);//启动动画    }    @OnClick(R.id.ap2)    public void translate(){//移动        Animation animation= AnimationUtils.loadAnimation(this,R.anim.translate);//获取Animation        imageview.startAnimation(animation);//启动动画    }    @OnClick(R.id.ap3)    public void Scale(){//缩放        Animation animation= AnimationUtils.loadAnimation(this,R.anim.scale);//获取Animation        imageview.startAnimation(animation);//启动动画    }    @OnClick(R.id.ap4)    public void Rotate(){//旋转        Animation animation= AnimationUtils.loadAnimation(this,R.anim.rotate);//获取Animation        imageview.startAnimation(animation);//启动动画    }}

XML布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_cartoon"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="readgroup.zhuoxin.com.myitems.Cartoon.CartoonActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <Button            android:id="@+id/ap1"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="渐变"            />        <Button            android:id="@+id/ap2"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="位移"            />        <Button            android:id="@+id/ap3"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="缩放"            />        <Button            android:id="@+id/ap4"            android:layout_weight="1"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="旋转"            />    </LinearLayout>    <ImageView        android:id="@+id/iv2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_star_border_black_24dp"        /></LinearLayout>

1,渐变动画(Alpha):

alpha如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true">    <alpha        android:duration="2000"        android:fromAlpha="1"        android:repeatCount="1"        android:repeatMode="reverse"        android:toAlpha="0" /></set>    <!--repeatCount动画重复的次数-->    <!--duration动画的时间-->    <!--fromAlpha初始透明度-->    <!--toAlpha结束透明度-->    <!--fillAfter是指动画结束是画面停留在此动画的最后一帧。默认值为false-->    <!--fillBefore是指动画结束时画面停留在此动画的第一帧; 默认值为true-->

效果图如下:
这里写图片描述

2,位移动画(Translate):

translate如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true"    >    <translate        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromXDelta="0"        android:toXDelta="320"        android:fromYDelta="0"        android:toYDelta="0"        android:duration="2000"/></set>    <!--      android:interpolator 动画的渲染器            1、accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速            2、decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速            3、accelerate_decelerate_interpolator(动画加速减速器)               中间位置分层:  使动画在开始的时候 最慢,然后逐渐加速              使动画在开始的时候 最快,然后逐渐减速  结束的位置最慢     fillAfter      是指动画结束是画面停留在此动画的最后一帧。默认值为false     duration       动画的持续时间(以下参数可以用百分比表示,-50%p:这里是向上整个屏幕一半的意思,"-"为向上/左,"正"为向下/右)     fromXDelta     动画起始位置的横坐标     toXDelta       动画起结束位置的横坐标     fromYDelta     动画起始位置的纵坐标     toYDelta       动画结束位置的纵坐标     -->

效果图如下:
这里写图片描述

3,缩放动画(Scale):

scale如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"    android:fillAfter="true">    <scale        android:interpolator= "@android:anim/decelerate_interpolator"        android:fromXScale="0.0"        android:fromYScale="0.0"        android:toXScale="1.5"        android:toYScale="1.5"        android:duration="1000"        android:startOffset="0"        android:repeatCount="1"        android:repeatMode="reverse"  /></set>    <!--    interpolator                     指定动画插入器        常见的有加速减速插入器        accelerate_decelerate_interpolator        加速插入器                   accelerate_interpolator,        减速插入器                   decelerate_interpolator。    fromXDelta,fromYDelta            起始时X,Y座标,屏幕右下角的座标是X:320,Y:480    toXDelta, toYDelta              动画结束时X,Y的座标    fromXScale,fromYScale,          动画开始前X,Y的缩放,0.0为不显示,1.0为正常大小    toXScale,toYScale,             动画最终缩放的倍数, 1.0为正常大小,大于1.0放大    fillAfter                        是指动画结束是画面停留在此动画的最后一帧。默认值为false    startOffset,                    动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,    duration,                       一次动画效果消耗的时间,单位毫秒,    repeatCount,                    动画重复的计数,动画将会执行该值+1次    repeatMode,                     动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变    -->

效果图如下:
这里写图片描述

4,旋转动画(Rotate):

rotate如下:

<set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:fromDegrees="300"        android:toDegrees="-360"        android:pivotX="50%"        android:pivotY="50%"        android:duration="1000" /></set>    <!--      fromDegrees   动画开始时的角度      toDegrees     动画结束时物件的旋转角度,正代表顺时针      pivotX        属性为动画相对于物件的X坐标的开始位置      pivotY        属性为动画相对于物件的Y坐标的开始位置    -->

效果图如下:
这里写图片描述
如若发现有错的地方,希望博友们多多指出!

0 0
原创粉丝点击