欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android播放帧动画

程序员文章站 2022-03-16 21:29:23
...


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import android.view.View;

public class AnimUtils_2 {

	private int[] resIdArr;
	private int index;
	private View view;
	private int period;//动画间隔
	private boolean isRepeatFlag = false;
	private boolean isStartFlag = false;
	
	private final static int PARSE_BITMAP_MSG = 0;
	private static HandlerThread sHandlerThread = new HandlerThread("anim-workThread");
	private static Handler sWorkHandler;
	static{
		sHandlerThread.start();
		sWorkHandler = new Handler(sHandlerThread.getLooper()){
			public void handleMessage(Message msg) {
				
				if(msg.what == PARSE_BITMAP_MSG){
					if(msg.obj instanceof Request){
					    Request request = (Request)msg.obj;
					    request.returnFun();
					}
				}
			};
		};
	}
	
	static class Request{
		final Resources resource;
		final int resId;
		final Handler handler;
		final int msgWhat;
		
		public Request(Resources resource, int resId, Handler handler, int msgWhat){
			this.resource = resource;
			this.resId = resId;
			this.handler = handler;
			this.msgWhat = msgWhat;
		}
		
		public void returnFun(){
			Bitmap bitmap = BitmapFactory.decodeResource(resource, resId);
			Message msg = Message.obtain();
			msg.what = msgWhat;
			msg.obj = bitmap;
			handler.sendMessage(msg);
		}
	}
	
	
	
	public interface OnAnimEndListener{
		public void onAnimEnd();
	}
	private OnAnimEndListener listener;
	
	public void setOnAnimEndListener(OnAnimEndListener listener){
		this.listener = listener;
	}
	
	private final static int PLAYING_ANIM_MSG = 0;
	private final static int GET_BITMAP_MSG = 1;
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			
			switch(msg.what){
				case PLAYING_ANIM_MSG:
				{
					sWorkHandler.post(new Runnable() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							final Bitmap bitmap = BitmapFactory.decodeResource(view.getResources(), 
									resIdArr[index]);
							Message msg = Message.obtain();
							msg.what = GET_BITMAP_MSG;
							msg.obj = bitmap;
							handler.sendMessage(msg);
						}
					});
					
//					Request request = new Request(view.getResources(), resIdArr[index], 
//							handler, GET_BITMAP_MSG);
//					Message requestMsg = Message.obtain();
//					requestMsg.what = PARSE_BITMAP_MSG;
//					requestMsg.obj = request;
//					sWorkHandler.sendMessage(requestMsg);
					break;
				}
				case GET_BITMAP_MSG:
				{
					if(!isStartFlag){
						
						return;
					}
					Bitmap bitmap = (Bitmap)msg.obj;
					BitmapDrawable bd = new BitmapDrawable(view.getResources(), bitmap);
					view.setBackground(bd);
					index ++;
					if(index >= resIdArr.length){
						if(isRepeatFlag){
							index = 0;
						}
						else{
							if(listener != null){
								listener.onAnimEnd();
							}
							return;
						}
					}
					handler.sendEmptyMessageDelayed(PLAYING_ANIM_MSG, getDelay(period));
					break;
				}
			}
		}
		
	};
	
	private long getDelay(long delayTime){
		if(delayTime <= 0){
			return 0;
		}
		else if(delayTime < 2){
			return delayTime;
		}
		else{
		    return delayTime - 1;
		}
	}
	
	public AnimUtils_2(){
		index = 0;
	}
	
	public void setAnimInfo(View view, int[] resIdArr, boolean isRepeatFlag){
		this.view = view;
		this.resIdArr = resIdArr;
		this.isRepeatFlag = isRepeatFlag;
	}
	
	public void startAnim(long delayMillis, int period){
		this.period = period;
		index = 0;
		handler.removeMessages(PLAYING_ANIM_MSG);
		handler.sendEmptyMessageDelayed(PLAYING_ANIM_MSG, getDelay(delayMillis));
		isStartFlag = true;
	}
	
	public boolean isAniming(){
		return isStartFlag;
	}
	
	public boolean isRepeat(){
		return isRepeatFlag;
	}
	
	public void stopAnim(){
		isStartFlag = false;
		handler.removeMessages(PLAYING_ANIM_MSG);
		//view.setBackgroundResource(resIdArr[0]);
	}
	
	public static void quit(){
		sHandlerThread.quit();
	}
}

 

相关标签: android 帧动画