安卓开发基础之动画(二)帧动画效果的实现

       在实际开发中还没用到帧动画,但不代表帧动画不重要。帧动画的原理很简单,就是如小时候用胶片放电影一样,一帧一帧的画面连起来就如同动画一样。这里只是简单介绍下它的实现方法,具体步骤在代码注释部分。(本文与上一篇写在同一个项目中)

下面是fram_animation代码,放在res/drawable文件夹下:

<?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/girl_1"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_2"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_3"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_4"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_5"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_6"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_7"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_8"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_9"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_10"
        android:duration="200"/>
    <item
        android:drawable="@drawable/girl_11"
        android:duration="200"/>
</animation-list>
这里要说明一下:如果直接设置android:src="@drawable/ fram_animation",那么图片直接变成动画。

下面是布局的XML文件:

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/bt_start"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="开始" />

        <Button
            android:id="@+id/bt_stop"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="停止" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/girl_1" />

    </RelativeLayout>

</LinearLayout>
这里要说明下:如果直接设置


接下来是JAVA代码:

package com.example.animationexercise;

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;
/**
 * 具体步骤:
 * 1、新建fram_animation文件,放在res/drawable文件夹下;
 * 2、将fram_animation文件作为资源赋给imageview;
 * 	 mImageView.setImageResource(R.drawable.fram_animation);
 * 3、获得AnimationDrawable类的对象实例,进而控制动画的启动和停止;
 *   animationDrawable = (AnimationDrawable) mImageView.getDrawable();
 *   animationDrawable.start();
 *   animationDrawable.stop();
 */
public class FramAnimationActivity extends Activity implements OnClickListener {
	
	private Button bt_start;
	private Button bt_stop;
	private ImageView mImageView;
	private AnimationDrawable animationDrawable;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fram_animation_layout);
		initView();
	}
	private void initView() {
		// TODO Auto-generated method stub
		bt_start = (Button) findViewById(R.id.bt_start);
		bt_stop = (Button) findViewById(R.id.bt_stop);
		mImageView = (ImageView) findViewById(R.id.imageView1);
		
		bt_start.setOnClickListener(this);
		bt_stop.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_start:
			mImageView.setImageResource(R.drawable.fram_animation);
			animationDrawable = (AnimationDrawable) mImageView.getDrawable();
			animationDrawable.start();
			break;
		case R.id.bt_stop:
			animationDrawable.stop();
			break;

		default:
			break;
		}
	}
}
好了,代码就这些,是不是很简单,自己写写看吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值