先看效果图,米老鼠的两个手分别指向时钟和分钟,然后米老鼠的脚在一秒执行一次动画操作。
分析完之后就先实现gif的播放,
如果实现gif的播放,就想着使用SurfaceView实现每格一秒循环一次贞动画。于是就需要8张贞图片,然后在surfaceView的125ms时间内播放一个贞,最后1000ms也就是1s播放了一次米老鼠跺脚的行为。代码如下:
private int count=0;/******/
while (mIsDrawing) { long start = System.currentTimeMillis(); draw(count); //0-7时候count++;到8时候count=0; if (count<7){ count++; }else { count=0; } long end = System.currentTimeMillis(); try { if (end - start < 125) { Thread.sleep(125 - (end - start)); } } catch (InterruptedException e) { e.printStackTrace(); }
分析完了播放1s跺脚动画之后就需要实现两个手的绘制。
首先需要的就是测量中心点和半径长度:
int widthSpecMode=MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode==MeasureSpec.AT_MOST||heightSpecMode==MeasureSpec.AT_MOST){ width = Math.min(getMeasuredHeight() , getMeasuredWidth() ); setMeasuredDimension(width, width); }else { width=Math.min(widthSpecSize,heightSpecSize); setMeasuredDimension(width,width); } //中心点 mCenter=width/2; //半径 mRadius=mCenter-getPaddingLeft()-getPaddingRight(); 中心点是width/2和getMeasuredHeight() /2的中心坐标半径就是mRadius=mCenter-getPaddingLeft()-getPaddingRight();测量完之后确定时钟和分钟的图片长度
//时分秒的长度 hDimension = mRadius * 2 / 3 ; mDimension = mRadius *4/5;最后要绘制这两个过程了:Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); Log.e("hour",hour+""); int minute = c.get(Calendar.MINUTE); Log.e(