Android帧动画实现

来源:互联网 发布:阿里巴巴商机助理 mac 编辑:程序博客网 时间:2024/05/29 09:01

文章主要介绍了安卓帧动画的实现过程。

下面是java代码,其实很简单,关键的地方就四行:

public class MainActivity extends Activity {    private ImageView refreshView;    private AnimationDrawable animationDrawable;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        refreshView = (ImageView) findViewById(R.id.refreshView);        refreshView.setImageResource(R.drawable.refresh_animation);        animationDrawable = (AnimationDrawable) refreshView.getDrawable();        animationDrawable.start();    }}
以下是布局文件的代码:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="80dp"        android:id="@+id/refresh">        <RelativeLayout            android:layout_marginLeft="10dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <ImageView                android:id="@+id/refreshView"                android:src="@drawable/f1"                android:layout_marginLeft="50dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />            <LinearLayout                android:orientation="vertical"                android:layout_toRightOf="@id/refreshView"                android:layout_marginTop="20dp"                android:layout_width="wrap_content"                android:layout_height="wrap_content">                <TextView                    android:id="@+id/refreshTextOne"                    android:text="正在刷新数据..."                    android:textColor="@color/material_blue_grey_950"                    android:textSize="15sp"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content" />                <TextView                    android:id="@+id/refreshTextTwo"                    android:text="松手更新"                    android:textColor="#50000000"                    android:layout_marginTop="4dp"                    android:textSize="12sp"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content" />            </LinearLayout>        </RelativeLayout>    </RelativeLayout></RelativeLayout>

实现这一过程最重要的一步就是在drawable下新建refresh_animation.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">    <item android:drawable="@drawable/f1" android:duration="150"></item>    <item android:drawable="@drawable/f2" android:duration="150"></item>    <item android:drawable="@drawable/f3" android:duration="150"></item>    <item android:drawable="@drawable/f4" android:duration="150"></item>    <item android:drawable="@drawable/f5" android:duration="150"></item>    <item android:drawable="@drawable/f6" android:duration="150"></item>    <item android:drawable="@drawable/f7" android:duration="150"></item>    <item android:drawable="@drawable/f8" android:duration="150"></item></animation-list>

duration可以设置图片更换的时间长短。

大家可以试试,只是实现以下效果,最终效果没有问题。

另注,这里有一篇很好的关于安卓动画的文章值得学习入门:http://www.2cto.com/kf/201410/342375.html

0 0
原创粉丝点击