自定义时钟控件

1.自定义控件时钟的布局和Java类

values文件下的attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyClock">
        <attr name="circleColor01" format="color"/>
        <attr name="circleColor02" format="color"/>
        <attr name="circleWidth" format="dimension"/>
    </declare-styleable>
    <declare-styleable name="Textview">
        <attr name="lineColor" format="color"></attr>
        <attr name="lineWidth" format="dimension"></attr>
    </declare-styleable>
</resources>

Java

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import java.util.Calendar;

public class MyClock extends View {
    private int hour,minute,second;
    private boolean running;

    private Paint circlePaint,linePaint,timerPaint,numPaint;
    //circlePaint---画圆的画笔,linePaint---画刻度线画笔,timerPaint---画时分秒的画笔,以及时钟上的数字
    private int circleColor01,circleColor02;//颜色
    private int circleWidth;//时钟外圈的宽度

    private Handler handler;
    private float density = getResources().getDisplayMetrics().density;//取手机密度

    public MyClock(Context context){
        super(context);

        circleColor01 = 0xFFFF0000;
        circleColor02 = 0xFFFFFFFF;
        circleWidth = (int)(4*density);
        init();
    }

    public MyClock(Context context,AttributeSet attrs){
        super(context,attrs);
        //通过TypedArray的相应方法获得attrs.xml定义的参数值,格式TypedArray的name+下划线+属性名称
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyClock);

        circleColor01 = a.getColor(R.styleable.MyClock_circleColor01,0xFFFF0000);
        circleColor02 = a.getColor(R.styleable.MyClock_circleColor02,0xFFFFFFFF);
        circleWidth = a.getDimensionPixelOffset(R.styleable.MyClock_circleWidth, 4);

        init();
        a.recycle();
    }

    private final void init(){
        hour = 0;minute = 0;second = 0;
        running = false;
        handler = new Handler();

        //初始化各个画笔
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);

        linePaint = new Paint();
        linePaint.setColor(0xFF000000);
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(2.0f * density);

        timerPaint = new Paint();
        timerPaint.setColor(0xFF000000);
        timerPaint.setAntiAlias(true);
        timerPaint.setStrokeWidth(3.0f * density);

        numPaint = new Paint();
        numPaint.setColor(0xFF000000);
        numPaint.setAntiAlias(true);
        numPaint.setTextSize(40);
        numPaint.setStrokeWidth(3.0f*density);
    }

    //用onMeasure方法询问组件大小
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        int result = Math.min(width,height);
        setMeasuredDimension(result, result);
    }
    //调用此方法得到模式和大小
    private int measureWidth(int measureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if((specMode == MeasureSpec.EXACTLY)||(specMode == MeasureSpec.AT_MOST)){
            result = specSize;
        }else {
            result = 256;
        }
        return result;
    }

    private int measureHeight(int measureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if((specMode == MeasureSpec.EXACTLY)||(specMode == MeasureSpec.AT_MOST)){
            result = specSize;
        }else {
            result = 256;
        }

        return result;
    }

    //绘画
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        circlePaint.setColor(0xFF0000FF);
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2, circlePaint);
        circlePaint.setColor(circleColor02);
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2 - circleWidth, circlePaint);

        for (int i = 1;i<=12;i++){
            canvas.save();
            canvas.rotate(30*i,this.getWidth()/2, this.getHeight()/2);
            canvas.drawLine(this.getWidth()/2,25*density,this.getWidth()/2,circleWidth + 1*density, linePaint);
            canvas.drawText(""+i,this.getWidth()/2,20*density+65,numPaint);
            canvas.restore();
        }

        for (int i = 1;i<=60; i++){
            canvas.save();
            canvas.rotate(6*i, this.getWidth()/2, this.getHeight()/2);
            canvas.drawLine(this.getWidth()/2,15*density,this.getWidth()/2,circleWidth + 1*density, linePaint);
            //canvas.drawText(""+i,this.getWidth()/2,20*density+60,numPaint);
            canvas.restore();
        }

        canvas.save();
        canvas.rotate(hour * 30 + minute / 2, this.getWidth() / 2, this.getHeight() / 2);
        timerPaint.setStrokeWidth(5.0f * density);
        //canvas.drawLine(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2, circleWidth + (this.getHeight()/4) * density, timerPaint);
        canvas.drawLine(this.getWidth()/2, this.getHeight()/2, this.getWidth()/2,
                this.getHeight() / 2 - (this.getWidth() /10-circleWidth)*density, timerPaint);
        canvas.restore();

        canvas.save();
        canvas.rotate(minute * 6, this.getWidth() / 2, this.getHeight() / 2);
        timerPaint.setStrokeWidth(4.0f * density);
        //canvas.drawLine(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2, circleWidth + (this.getHeight()/12) * density, timerPaint);
        canvas.drawLine(this.getWidth()/2, this.getHeight()/2, this.getWidth()/2,
                this.getHeight() / 2 - (this.getWidth()/8-circleWidth)*density, timerPaint);
        canvas.restore();

        canvas.save();
        canvas.rotate(second*6, this.getWidth()/2, this.getHeight()/2);
        timerPaint.setStrokeWidth(3.0f*density);
        //canvas.drawLine(this.getWidth()/2, this.getHeight()/2, this.getWidth()/2, circleWidth + (this.getHeight()/18)*density, timerPaint);
        canvas.drawLine(this.getWidth()/2, this.getHeight()/2, this.getWidth()/2,
                this.getHeight()/2-(this.getWidth()/7-circleWidth)*density, timerPaint);
        canvas.restore();
    }

    private class TimerTask implements Runnable{
        public void run(){
            while (running){
                try{
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    return;
                }

                Calendar c = Calendar.getInstance();
                hour = c.get(Calendar.HOUR);
                minute = c.get(Calendar.MINUTE);
                second = c.get(Calendar.SECOND);

                handler.post(new Runnable(){
                    public void run(){
                        MyClock.this.invalidate();
                    }
                });
            }
        }
    }

    protected void onAttachedToWindow(){
        super.onAttachedToWindow();
        start();
    }

    public void onDetachedFromWindow(){
        super.onDetachedFromWindow();
        stop();
    }

    public void start(){
        if(running == false){
            running = true;
            Thread t = new Thread(new TimerTask());
            t.start();
        }
    }

    public void stop(){
        running = false;
    }

}

2.在现有组件上添加自定义

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

public class Textview extends TextView {

    private int lineColor;
    private int lineWidth;
    private Paint linePaint;

    public Textview(Context context){
        super(context);

        lineColor=0xFF000000;
        lineWidth=2;
        init();
    }
    public Textview(Context context,AttributeSet attrs){
        super(context,attrs);

        TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.Textview);
        lineColor=a.getColor(R.styleable.Textview_lineColor,0xFF000000);
        lineWidth=a.getDimensionPixelOffset(R.styleable.Textview_lineWidth,2);
        init();

        a.recycle();
    }
    private final void init(){
        linePaint=new Paint();
        linePaint.setColor(lineColor);
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(lineWidth);
    }

    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawLine(0,this.getHeight(),this.getWidth(),this.getHeight(),linePaint);
    }
}

3.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rj141.sb.myclockview.MainActivity"
    android:orientation="vertical">

    <com.rj141.sb.myclockview.Textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:lineColor="#FFFF0000"
        android:textSize="24dp"
        app:lineWidth="3dp"
        android:text="@string/clock"
        />
    <com.rj141.sb.myclockview.MyClock
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/id_clock"
        app:circleColor01 = "#FF0000FF"
        app:circleColor02 = "#ffffff"
        app:circleWidth = "4dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/start"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/start"/>
        <Button
            android:id="@+id/stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/stop"/>
    </LinearLayout>
</LinearLayout>

4.显示

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button start,stop;
    private MyClock myClock;

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

        myClock=(MyClock)this.findViewById(R.id.id_clock);
        start=(Button)this.findViewById(R.id.start);
        start.setOnClickListener(this);
        stop=(Button)this.findViewById(R.id.stop);
        stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.start:
                myClock.start();
                break;
            case R.id.stop:
                myClock.stop();
                break;
            default:
                break;
        }
    }
}

时间: 2024-08-08 01:04:42

自定义时钟控件的相关文章

C# 时钟控件

//控件名:myNewClock //作者:刘典武 //时间:2011-06-10 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace O

使用Canvas绘制简单的时钟控件

Canvas是HTML5新增的组件,它就像一块幕布,可以用JavaScript在上面绘制各种图表.动画等. 没有Canvas的年代,绘图只能借助Flash插件实现,页面不得不用JavaScript和Flash进行交互.有了Canvas,我们就再也不需要Flash了,直接使用JavaScript完成绘制. 基本使用 在html源码中只要像普通标签那样使用即可,比如下面的示例,但是在一些老旧的浏览器在中是不支持canvas的,因此我们常在canvas标签中提示用户,具体代码如下所示. <canvas

自定义组合控件和在自定义控件中使用自定义属性

今天,整理了一下我平时的笔记,写一个比较简单的自定义组合控件,仅供小白参考,大神请绕道,希望能够对大家有一些帮助 首先,得明白为什么我们需要自定义组合控件,它是因为原有控件并不能满足开发的需求,或者说并不能达到我们想要的一种效果,这个时候,就需要我们自己定义一些控件,以达到目的 ![先来看一下效果](http://img.blog.csdn.net/20160716224219109) 个人总结自定义控件的步骤: 1.先写一个布局,这里我用的是一个相对布局,我这里的相对布局就是根布局了 <?xm

自定义HtmlHelper控件

在asp.net mvc 中每一个Html控件都返回了MvcHtmlString ,他继承了HtmlString.下面自定义一个关于显示男女性别的自定义Html控件,使在创建页面时,可以直接调用该自定义的Html控件.可以查看其他的Html控件返回的是HtmlHelper,所以自定义的时候也要返回相同的类型直接在Controls文件夹下建立要自定义的html控件代码如下: using System.Web.Mvc; using System.Text; namespace System.Web.

【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法

原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们并不想在DataHolder类中加一个标识是否选中的checked的成员变量,因为在项目开发中,大部分的ListItemLayout布局都是大家共用的,有些人根本不需要checkbox控件,所以会在初始化的时候把这个控件给隐藏掉,但我们的DataHolder在构造的时候以及ListItemAdapt

WIN32窗口子类化----自定义Edit控件的右键菜单

前言 Win32应用程序中,子控件的消息都是分发到其父窗口的消息处理函数中去了,这对于我们需要自定义子控件的某些特性时时十分不方便的,还好,Windows为我们提供了控件子类化的相关接口API.核心的思想是:通过获取子控件的消息处理函数地址,设置子控件的消息处理函数到自己定义的函数里,也就是Get/SetWindowLong API的使用. 测试代码 这里是一个简单的测试程序,在控制台程序中创建一个对话框,然后对话框上有一个EDIT控件(资源编辑器里拖入的,不多说),子类化EDIT控件,右键弹出

Android实例-手机安全卫士(七)-自定义组合控件

一.目标. 将多个系统控件(TextView.Button.CheckBox等)组合成一个自定义的控件,并像系统控件一样使用.如图所示第1个自动更新控件是根据相对布局放置而成的,第2个自动更新控件即为自定义组合控件,它可以想一般的TextView等系统控件一样重复使用. 自定义控件如图: 二.代码实现. 1.在layout文件夹下新建一个xml文件(取名model_setting_item.xml),用于保存自定义控件的布局. 2.在新建的xml文件(model_setting_item.xml

ExtJs5_继承自定义一个控件

Extjs的开发都可以遵循OOP的原则,其对类的封装也很完善了.自定义一个控件最简单的办法就是继承一个已有的控件.根据上一节的需要,我做了一个Button的子类.首先根据目录结构,在app目录下建立一个ux目录,将自定义控件都放在这个目录下.在ux目录下建立一个文件ButtonTransparent.js. /** * 定义了一个背景透明的Button类,继承于Button */ Ext.define('app.ux.ButtonTransparent', { extend : 'Ext.but

自定义Imageview控件实现多种手势操作 (拖动、水平缩放、竖直缩放、等比例缩放、双击、长按)

项目中需要使用自定义控件的多种手势操作,之前在网上查阅资料的时候发现能找到的一般是只实现了其中的几种,这次就把我做的控件分享一下,人人为我,我为人人嘛,哈哈! 这个自定义控件实现的主要功能是控件的拖动和缩放(注意:不是对控件中的图片进行操作,话说很多帖子都把这两个混了),其中缩放可以按照三个方向进行,就是水平.竖直和等比例.双击操作只做了一个提示,长按加上了一个简单的弹出菜单. 抱歉的是没有足够的时间写详细注释了,如果跟你需要的功能相同就请直接调用,要是需要改代码就费点神自己读懂代码吧,看不懂的