android frame by frame AnimationDrawable 实现动画效果

来源:互联网 发布:淘宝网店查询 编辑:程序博客网 时间:2024/05/21 04:41

虽然现在帧动画使用的并不是太多了,但是在实际的开发过程中,还是会使用到的

下面直接上代码:

drawable文件夹下创建一个动画xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@drawable/ok1"        android:duration="500">    </item>    <item        android:drawable="@drawable/ok2"        android:duration="500">    </item>    <item        android:drawable="@drawable/ok3"        android:duration="500">    </item>    <item        android:drawable="@drawable/ok4"        android:duration="500">    </item></animation-list>

主布局,很简单,就一个imageview

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/iv_go"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         /></LinearLayout>

不过这个imageview需要注意啊,不要给他设置src


activity代码:

package com.example.testanimationdrawable;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.widget.ImageView;public class Main extends Activity {private ImageView iv_go;private AnimationDrawable animationDrawable;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);initView();}@SuppressLint("NewApi")private void initView() {// TODO Auto-generated method stubiv_go = (ImageView) findViewById(R.id.iv_go);//先通过xml动画创建出我们的animationDrawable对象//然后把这个animationDrawable对象设置为imageview的background//然后animationDrawable调用start方法就可以实现动画效果了.祯动画//animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.myanimation);//iv_go.setBackground(animationDrawable);//animationDrawable.start();//先为我们的imageview设置背景资源//然后使用imageview的getbackground方法,得到一个drawable对象,然后把它强转给animationdrawable//animationdrawable对象调用start方法进行动画的开始iv_go.setBackgroundResource(R.drawable.myanimation);animationDrawable = (AnimationDrawable) iv_go.getBackground();animationDrawable.start();}}

代码都很简单,这里就不多做解释了

0 0