自制钟表(用canvas)

钟表的程序介绍

主程序

package com.test.mywidget;
import android.support.v7.app.ActionBarActivity;
import com.test.widget.MyView;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
    }   
}

View程序

package com.test.widget;

import java.util.Calendar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.os.Handler;
import android.os.Message;
import android.provider.CalendarContract.Colors;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {
    private int mWidth;
    private int mHeight;
    private Paint mPaint;
    private Paint mPaintMinute;
    private Paint mPaintHour;
    private Paint mPaintCircle;
    private Paint mPaintText;
    private Calendar mCalendar;
    private static final int NEED_UPDATE=34;
    private Handler mHandler=new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case NEED_UPDATE:
                mCalendar=Calendar.getInstance();
                invalidate();
                mHandler.sendEmptyMessageDelayed(NEED_UPDATE, 1000);
                break;

            default:
                break;
            }

        };
    };

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCalendar=Calendar.getInstance();
        //查看api以了解它的具体使用方法(canvas View的api)
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);

        mPaintHour = new Paint();
        mPaintHour.setColor(Color.BLACK);
        mPaintHour.setStrokeWidth(5);
        mPaintHour.setAntiAlias(true);


        mPaintMinute = new Paint();
        mPaintMinute.setColor(Color.GREEN);
        mPaintMinute.setStrokeWidth(5);
        mPaintMinute.setAntiAlias(true);


        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.BLUE);
        mPaintCircle.setStrokeWidth(5);
        mPaintCircle.setAntiAlias(true);
        mPaintCircle.setStyle(Paint.Style.STROKE);

        mPaintText = new Paint();
        mPaintText.setColor(Color.DKGRAY);
        mPaintText.setTextSize(30);
        mPaintText.setTextAlign(Align.CENTER);
        mPaintText.setStrokeWidth(5);
        mPaintText.setAntiAlias(true);
        mHandler.sendEmptyMessageDelayed(NEED_UPDATE, 1000);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        mHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        {
        super.onDraw(canvas);
        // 传入像素值,查看api
        //画出表盘
        canvas.drawCircle(mWidth / 2, mHeight / 2, 300, mPaintCircle);
        canvas.drawCircle(mWidth / 2, mHeight / 2, 10, mPaintCircle);
        for (int i = 1; i <= 12; i++) {
            //保存画布此时的状态
            canvas.save();
            canvas.rotate(360 / 12 * i, mWidth / 2, mHeight / 2);
            canvas.drawLine(mWidth / 2, mHeight / 2 - 300, mWidth / 2 - 1, mHeight / 2 - 280, mPaint);
            canvas.drawText("" + i, mWidth / 2, mHeight / 2 - 240, mPaintText);
            //配合save()使用
            canvas.restore();
        }

        //画出时钟的分针
        int minute=mCalendar.get(Calendar.MINUTE);
        int hour=mCalendar.get(Calendar.HOUR);
        float degreeMinute=minute/60f*360;
        canvas.save();
        canvas.rotate(degreeMinute,mWidth / 2, mHeight / 2);
        canvas.drawLine(mWidth / 2, mHeight / 2-200, mWidth / 2, mHeight / 2+18, mPaintMinute);
        canvas.restore();


        //画出时钟的时针,计算时针的位置,因为时针一天走两圈,所以除以720,在乘以角度360
        float degreeHour=(hour*60+minute)/12f/60*360;
        canvas.save();
        canvas.rotate(degreeHour,mWidth / 2, mHeight / 2);
        canvas.drawLine(mWidth / 2, mHeight / 2-150, mWidth / 2, mHeight / 2+14, mPaintHour);
        canvas.restore();
    }

        //画出秒针
        int seconde=mCalendar.get(Calendar.SECOND);
        float degreeSeconde=seconde*6;
        canvas.save();
        canvas.rotate(degreeSeconde,mWidth / 2, mHeight / 2);
        canvas.drawLine(mWidth / 2, mHeight / 2-250, mWidth / 2, mHeight / 2+24, mPaint);
        canvas.restore();

    }
}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.test.mywidget.MainActivity" >

    <com.test.widget.MyView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></com.test.widget.MyView>

</LinearLayout>

效果图

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值