初学Android,图形图像之使用逐帧动画(三十四)

逐帧动画其实跟动画片的原理是一样的,一段时间内连续播放一组图片,使之看起来像在动一样

下面是一个例子,不用专门说明就可以理解了

定义文件的资源名为girl,放在res->anmi目录下,下面有一个重要的参数 "

android:oneshot="false"
如果为true,动画将不会循环播放

<?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/one" android:duration = "60" />
    <item android:drawable="@drawable/two" android:duration = "60" />
    <item android:drawable="@drawable/three" android:duration = "60" />
    <item android:drawable="@drawable/four" android:duration = "60" />
    <item android:drawable="@drawable/five" android:duration = "60" />
    <item android:drawable="@drawable/six" android:duration = "60" />
</animation-list>

主界面main中,imageveiw组件对动画资源进行引用

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff">
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center">
  <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" /> 
  <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" /> 
  </LinearLayout>
  <ImageView android:id="@+id/anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/girl" android:scaleType="center" /> 
</LinearLayout>

主界面中两个按钮,一个用来播放动画,一个用来停止动画

package WangLi.Graphics.AnimationFrame;

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 AnimationFrame extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //获取两个按钮
        Button play = (Button)findViewById(R.id.play);
        Button stop = (Button)findViewById(R.id.stop);
        ImageView imageView = (ImageView)findViewById(R.id.anim);
        //获取AnimationDrawable动画对象
        final AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();
        play.setOnClickListener(new OnClickListener(){
        	public void onClick(View v)
        	{
        		anim.start();
        	}
        });
        stop.setOnClickListener(new OnClickListener(){
        	public void onClick(View v)
        	{
        		anim.stop();
        	}
        });
    }
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值