自定义view中绘制动画

package zy.qiufo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

public class QiuQianAnimationView extends View {

	private Bitmap[] m_animationPics;
	private int m_CurPic=0;
	private int m_iDevWidth = 0;
	private int m_iDevHeight = 0;
	private boolean m_bStartAnimation = false;
	private QiufoActivity m_mainActivity;
	
	public QiuQianAnimationView(Context context) {
		super(context);
		//initPic();
		// TODO Auto-generated constructor stub
	}
	
	public QiuQianAnimationView(Context context, AttributeSet attrs) {  
		super(context,attrs);
		initPic();  
		m_mainActivity = (QiufoActivity)context;
		WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);	 
		m_iDevWidth = wm.getDefaultDisplay().getWidth();
		m_iDevHeight = wm.getDefaultDisplay().getHeight();
    } 
	
	protected void initPic()
	{
		m_animationPics = new Bitmap[10];
		m_animationPics[0] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian);
		m_animationPics[1] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_left_01);
		m_animationPics[2] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_left_02);
		m_animationPics[3] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_right_01);
		m_animationPics[4] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_right_02);
		m_animationPics[5] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_left_01);
		m_animationPics[6] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_left_02);
		m_animationPics[7] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_right_01);
		m_animationPics[8] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_right_02);
		m_animationPics[9] = BitmapFactory.decodeResource(getResources(), R.drawable.qiuqian_result);
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		if (m_bStartAnimation)
		{
			if (m_CurPic < 10)
			{
				int iBgX = m_iDevWidth/2 - m_animationPics[m_CurPic].getWidth()/2;
				int iBgY = m_iDevHeight/2 - m_animationPics[m_CurPic].getHeight()/2;
				canvas.drawBitmap(m_animationPics[m_CurPic],iBgX,iBgY, null);
				m_CurPic++;
				this.postInvalidateDelayed(500);
			}
			else
			{
				int iBgX = m_iDevWidth/2 - m_animationPics[9].getWidth()/2;
				int iBgY = m_iDevHeight/2 - m_animationPics[9].getHeight()/2;
				canvas.drawBitmap(m_animationPics[9],iBgX,iBgY, null);
				m_bStartAnimation = false;
			}
		}		
		else 
		{
			if(m_CurPic == 0)
			{
				int iBgX = m_iDevWidth/2 - m_animationPics[0].getWidth()/2;
				int iBgY = m_iDevHeight/2 - m_animationPics[0].getHeight()/2;
				canvas.drawBitmap(m_animationPics[0],iBgX,iBgY, null);
			}
			else
			{
				int iBgX = m_iDevWidth/2 - m_animationPics[9].getWidth()/2;
				int iBgY = m_iDevHeight/2 - m_animationPics[9].getHeight()/2;
				canvas.drawBitmap(m_animationPics[9],iBgX,iBgY, null);

			}
		}
			
	}	
	
	public void startDraw()
	{
		m_CurPic = 0;
		m_bStartAnimation = true;
		this.postInvalidate();
	}
	
	public void stopDraw()
	{
		
	}
}

上面的view类利用postInvalidate来不断刷新自身,刷新时调用ondraw重绘,重绘时根据情况绘制不同的图片到屏幕中央,形成动画(类似逐帧动画)

在activity中放入上述view:

<FrameLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<ImageView 
android:src="@drawable/gmfou" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:scaleType="fitXY"
android:id="@+id/iv_fo"/>
       
<zy.qiufo.QiuQianAnimationView
   android:id="@+id/vw_animationPic"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content" 
   />  

</FrameLayout>




你可能感兴趣的:(view)