在代码中设置逐帧动画

上文介绍了在XML中定义逐帧动画,接下来本文中将介绍在代码中控制逐帧动画的加载和播放,代码如下:

Activity:

package com.lovo.frameanim;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
	private Button startBtn;
	private Button stopBtn;
	private ImageView imageView;
	// 定义一个存放所有静态图片的图片数组
	private Bitmap[] bitmaps = new Bitmap[25];;
	// 声明AnimationDrawable对象
	private AnimationDrawable animationDrawable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		startBtn = (Button) findViewById(R.id.main_btn_start);
		stopBtn = (Button) findViewById(R.id.main_btn_stop);
		startBtn.setOnClickListener(this);
		stopBtn.setOnClickListener(this);
		imageView = (ImageView) findViewById(R.id.main_image);
		animationDrawable = new AnimationDrawable();
		loadImages();
		// 设置动画对象的位置和大小
		animationDrawable.setBounds(0, 0, 128, 96);
		// 设置动画是否循环播放:true表示不循环,false表示循环
		animationDrawable.setOneShot(false);
		imageView.setBackgroundDrawable(animationDrawable);
		// 若要让启动Activity就开始动画,可以使用下面线程来处理
		// 利用Handler的postDelayed方法实现线程处理,这里可以直接操作组件对象
		// new Handler().postDelayed(new Runnable() {
		// @Override
		// public void run() {
		// animationDrawable.start();
		// }
		// }, 500);
	}

	@Override
	public void onClick(View v) {
		if (v == startBtn) {
			// 启动动画
			animationDrawable.start();
		}
		if (v == stopBtn) {
			// 停止动画
			animationDrawable.stop();
		}
	}

	/**
	 * 加载图片
	 */
	public void loadImages() {
		// 加载boy图片
		Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.boy);
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				// 在源图片上根据指定位置和大小截取并创建新的位图
				bitmaps[i * 5 + j] = Bitmap.createBitmap(srcBitmap, j * 128,
						i * 96, 128, 96);
				// 给AnimationDrawable对象添加BitmapDrawable对象
				animationDrawable.addFrame(new BitmapDrawable(
						bitmaps[i * 5 + j]), 50);
			}
		}
	}
}


布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >

    <ImageView
        android:id="@+id/main_image"
        android:layout_width="128dp"
        android:layout_height="96dp"
        android:background="@anim/freedom_frame_anim" />
    <Button
        android:id="@+id/main_btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始动画" />

    <Button
        android:id="@+id/main_btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止动画" />
</LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值