自定义View实现米老鼠时钟

本文介绍如何利用Android的SurfaceView创建一个独特的米老鼠时钟,时钟的指针为米老鼠的手,每秒米老鼠的脚会进行一次动画效果。通过设置8帧图片,在125毫秒内播放一帧,实现每秒一次的动画循环。
摘要由CSDN通过智能技术生成

先看效果图,米老鼠的两个手分别指向时钟和分钟,然后米老鼠的脚在一秒执行一次动画操作。

分析完之后就先实现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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值