Android动画——Frame动画

来源:互联网 发布:网络寻衅滋事罪案例 编辑:程序博客网 时间:2024/05/06 12:36

Frame动画即帧动画,与电影放映的原理相同,都是通过帧的快速切换,展现动态的视觉效果。
本文包含帧动画两个方面的应用实例,分别是经典帧动画和旋转帧动画(即转菊花)。

一、经典帧动画实现

第一步:在工程res目录下的drawable目录(没有请自行创建)中新建一个资源文件名为normal.xml:
这里写图片描述

第二步:编辑资源文件

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false"    android:visible="false"    android:variablePadding="false">    <item android:drawable="@mipmap/a" android:duration="500" />    <item android:drawable="@mipmap/b" android:duration="500" />    <item android:drawable="@mipmap/c" android:duration="500" />    <item android:drawable="@mipmap/d" android:duration="500" />    <item android:drawable="@mipmap/e" android:duration="500" />    <item android:drawable="@mipmap/f" android:duration="500" /></animation-list>

属性介绍:

android:oneshot  如果为true,动画只执行一次android:visible  drawable的初始可见行,默认为falseandroid:variablePadding  选择true时,drawable的内边距会根据状态的变化而变化,设置为true时,你必须为不同的状态配置layout,但是通常不建议这么做。选择false时,内边距保持一致,所有状态中最大的内边距。默认为false android:drawable  当前帧引用的drawable资源android:duration  当前帧的显示时间

第三步:使用drawable

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/imageview"        android:layout_width="200dp"        android:layout_height="200dp"        android:layout_centerInParent="true"        android:background="@drawable/normal"/></RelativeLayout>

第四步:启动drawable
1.初始化

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    View rootView = inflater.inflate(R.layout.fragment_normal, container, false);    imageView = (ImageView) rootView.findViewById(R.id.imageview);    animationDrawable = (AnimationDrawable)imageView.getBackground();    return rootView;}

2.启动

public void onResume() {    super.onResume();    animationDrawable.start();}

3.停止

public void onResume() {    super.onResume();    animationDrawable.start();}

旋转帧动画

第一步:在工程res目录下的drawable目录(没有请自行创建)中新建一个资源文件名为rotate.xml:
这里写图片描述

第二步:编辑资源文件

<?xml version="1.0" encoding="utf-8"?><animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@mipmap/ic_launcher"    android:pivotX="50%"    android:pivotY="50%"    android:visible="true"/>

属性介绍:

android:drawable  资源文件android:poivotX  旋转中心点的X坐标在drawableX轴方向的比例android:poivotY  旋转中心店的Y坐标在drawableY轴方向的比例android:visible  drawable初始可见性

第三步:使用drawable

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/rotate"/></RelativeLayout>
0 0
原创粉丝点击