• 大小: 7KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: Java
  • 标签: 时钟  

资源简介

android模拟时钟控件 android studio

资源截图

代码片段和文件信息

package com.example.xiaoluo.myapplication;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import java.util.Calendar;

/**
 * Created by xiaoluo on 2016/1/11.
 */
public class TimerView extends View {

    private int mWidthmHeight;

    public TimerView(Context context){
        super(context);
    }
    public TimerView(Context contextAttributeSet attrs){
        super(contextattrs);
    }
    public TimerView(Context context AttributeSet attrs int defstyleAttr) {
        super(context attrs defstyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {

        //设置画布背景
        canvas.drawColor(Color.GRAY);

        //获取屏幕宽度
        mWidth=getMeasuredWidth();
        //获取屏幕高度
        mHeight=getMeasuredHeight();

        //获取系统时间
        //Calendar里面月份的值是从0~11,对应1~12
        //星期几是从星期天开始算的
        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH);
        int day=calendar.get(Calendar.DAY_OF_MONTH);
        int week=calendar.get(Calendar.DAY_OF_WEEK);
        String sYearsMonthsDaysWeek;
        sYear=String.valueOf(year);
        sMonth=String.valueOf(month+1);
        sDay=String.valueOf(day);
        sWeek=String.valueOf(week);
        if (week==1){
            sWeek=“日“;
        }else if(week==2){
            sWeek=“一“;
        }else if(week==3){
            sWeek=“二“;
        }else if(week==4){
            sWeek=“三“;
        }else if(week==5){
            sWeek=“四“;
        }else if(week==6){
            sWeek=“五“;
        }else if(week==7){
            sWeek=“六“;
        }
        int hour=calendar.get(Calendar.HOUR_OF_DAY);
        if (hour>12){
            hour=hour-12;
        }
        int minute=calendar.get(Calendar.MINUTE);
        int second=calendar.get(Calendar.SECOND);

        //显示时间
        Paint paintTime=new Paint();
        paintTime.setColor(Color.BLUE);
        //要先设置画笔颜色后,设置透明度才有效果
        paintTime.setAlpha(100);
        paintTime.setStrokeWidth(25);
        paintTime.setTextSize(50);
        String mHour=String.valueOf(hour);
        canvas.drawText(mHour mWidth / 2-80 mHeight / 2-130 paintTime);
        canvas.drawText(“:“ mWidth / 2-20 mHeight / 2-130 paintTime);
        String mMinute=String.valueOf(minute);
        canvas.drawText(mMinute mWidth / 2 mHeight / 2-130 paintTime);
        canvas.drawText(“:“ mWidth / 2+60 mHeight / 2-130 paintTime);
        String mSecond=String.valueOf(second);
        canvas.drawText(mSecondmWidth/2+80mHeight/2-130paintTime);

        canvas.drawText(sYear+“.“+sMonth+“.“+sDay+“-----星期“+sWeekmWidth/2-200mHeight/2+180paintT

评论

共有 条评论