你好,游客 登录 注册 搜索
背景:
阅读新闻

Android中的动画实现详解

[日期:2013-01-18] 来源:Linux社区  作者:piaoyang [字体:大 中 小]

二、逐帧动画的实现步骤:

通过上面的分析,我们在这里总结一下实现逐帧动画的步骤。

1)在XML文件中定义动画所需要的静态图片;

2)将该动画设置成为一个View的background;

3)在代码中通过getBackground函数获取该View的background;

4)调用AnimationDrawable中的start启动动画。

三、逐帧动画的实例

下面通过一个实例来体验一下实现逐帧动画的过程。

1)在res中anim文件夹中新建一个定义动画的文件xml文件music_loading.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/music_load1" android:duration="500" />
 <item android:drawable="@drawable/music_load2" android:duration="500" />
 <item android:drawable="@drawable/music_load3" android:duration="500" />
 <item android:drawable="@drawable/music_load4" android:duration="500" />
 <item android:drawable="@drawable/music_load5" android:duration="500" />
 <item android:drawable="@drawable/music_load6" android:duration="500" />
 <item android:drawable="@drawable/music_load7" android:duration="500" />
 <item android:drawable="@drawable/music_load8" android:duration="500" />
 <item android:drawable="@drawable/music_load9" android:duration="500" />
 <item android:drawable="@drawable/music_load10" android:duration="500" />
 <item android:drawable="@drawable/music_load11" android:duration="500" />
 <item android:drawable="@drawable/music_load12" android:duration="500" />
 <item android:drawable="@drawable/music_load13" android:duration="500" />
 <item android:drawable="@drawable/music_load14" android:duration="500" />
</animation-list>

2)在定义的layout文件中将该动画设置为一个view的background,该实例中使用ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
   
    <ImageView
        android:id="@+id/musicLoader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/music_loading"/>

    <Button
        android:id="@+id/startFrame"
        android:text="Start..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
   
    <Button
        android:id="@+id/stopFrame"
        android:text="Stop..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

3)在Java文件中获取animation,同时启动

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class FrameAnimation1Activity extends Activity {
    /** Called when the activity is first created. */
 private ImageView musicLoader;
 private Button startBtn;
 private Button stopBtn;
 private AnimationDrawable loadingAnimation;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        musicLoader = (ImageView)findViewById(R.id.musicLoader);
        //第三步:通过getBackground获取AnimationDrawable对象
        loadingAnimation = (AnimationDrawable)musicLoader.getBackground();
       
        startBtn = (Button)findViewById(R.id.startFrame);
        //第四步:通过start方法启动动画
        startBtn.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
          loadingAnimation.start();
         }
        });
       
        stopBtn = (Button)findViewById(R.id.stopFrame);
        //第四步:通过stop方法停止动画
        stopBtn.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
          loadingAnimation.stop();
         }
        });
    }
}

linux
相关资讯       Android中动画  Android详解 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款