Android 循环读取名字类似的图片

  1. 云栖社区>
  2. 博客>
  3. 正文

Android 循环读取名字类似的图片

一叶飘舟 发布时间:2012-06-04 14:37:00 浏览450 评论0

摘要: 这个用处还是很大的 package com.lzx.game; import android.content.Context; import android.graphics.Canvas; import android.

这个用处还是很大的
package com.lzx.game;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.View;

public class Game17View extends View {

	private Context context;
	private AnimationDrawable frameAnimation;
	//定义一个Drawable对象
	private Drawable mDrawable;
	public Game17View(Context context) {
		super(context);
		this.context = context;
		frameAnimation = new AnimationDrawable();
		for (int i = 1; i <= 15; i++) {
			int id = getResources().getIdentifier("a" + i, "drawable", context.getPackageName());
			mDrawable = getResources().getDrawable(id);
			frameAnimation.addFrame(mDrawable, 500);
		}
		//是否循环播放
		frameAnimation.setOneShot(false);
		this.setBackgroundDrawable(frameAnimation);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			frameAnimation.start();
			break;

		default:
			break;
		}
		return super.onKeyUp(keyCode, event);
	}
}

【云栖快讯】云栖专辑 | 阿里开发者们的第20个感悟:好的工程师为人写代码,而不仅是为编译器  详情请点击

网友评论