先上效果图:

Android 自定义view的简单应用(3) 时钟_第1张图片

布局文件:

<?xml version="1.0" encoding="utf-8"?>    

自定义View 文件:

public class Clock extends View {    int width = 800;    int height = 800;    public Clock(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        setMeasuredDimension(width,height);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawColor(Color.TRANSPARENT);        /*        绘制原盘         */        Paint CircleBgPaint = new Paint();        CircleBgPaint.setColor(Color.WHITE);        CircleBgPaint.setStyle(Paint.Style.FILL);        CircleBgPaint.setAntiAlias(true);        int Radius = width / 2 - 60;        canvas.drawCircle(width /2 ,height / 2,Radius,CircleBgPaint);        /*        绘制原盘边框         */        Paint CircleBgStrkoePaint = new Paint();        CircleBgStrkoePaint.setColor(Color.BLACK);        CircleBgStrkoePaint.setStrokeWidth(20);        CircleBgStrkoePaint.setStyle(Paint.Style.STROKE);        CircleBgStrkoePaint.setAntiAlias(true);        canvas.drawCircle(width /2 ,height / 2,Radius,CircleBgStrkoePaint);        /*        绘制刻度        分2个数组 因为drawLines 的大小限制         */        Paint MarkPaint = new Paint();        MarkPaint.setColor(Color.BLACK);        MarkPaint.setStyle(Paint.Style.FILL);        MarkPaint.setAntiAlias(true);        MarkPaint.setStrokeWidth(20);        int R1 = Radius - 35;        int CenterX = width / 2;        int CenterY = height / 2;        float[] Marks = new float[7 * 4];        float[] Marks2 = new float[5 * 4];        for(int i = 0; i < 12;i++)        {            if(i <= 3) {                int degree = i * 30;                int x_s = (int) (Math.sin(Math.toRadians(degree)) * R1);                int y_s = (int) (Math.cos(Math.toRadians(degree)) * R1);                int x_e = (int) (Math.sin(Math.toRadians(degree)) * Radius);                int y_e = (int) (Math.cos(Math.toRadians(degree)) * Radius);                Marks[i * 4] = CenterX + x_s;                Marks[i * 4 + 1] = CenterY - y_s;                Marks[i * 4 + 2] = CenterX + x_e;                Marks[i * 4 + 3] = CenterY - y_e;            }            else if(i <= 6)            {                int degree = (i - 3) * 30;                int y_s = (int) (Math.sin(Math.toRadians(degree)) * R1);                int x_s = (int) (Math.cos(Math.toRadians(degree)) * R1);                int y_e = (int) (Math.sin(Math.toRadians(degree)) * Radius);                int x_e = (int) (Math.cos(Math.toRadians(degree)) * Radius);                Marks[i * 4] = CenterX + x_s;                Marks[i * 4 + 1] = CenterY + y_s;                Marks[i * 4 + 2] = CenterX + x_e;                Marks[i * 4 + 3] = CenterY + y_e;            }            else if(i <= 9)            {                int degree = (i - 6) * 30;                int x_s = (int) (Math.sin(Math.toRadians(degree)) * R1);                int y_s = (int) (Math.cos(Math.toRadians(degree)) * R1);                int x_e = (int) (Math.sin(Math.toRadians(degree)) * Radius);                int y_e = (int) (Math.cos(Math.toRadians(degree)) * Radius);                Marks2[( i - 7 ) * 4] = CenterX - x_s;                Marks2[( i - 7 ) * 4 + 1] = CenterY + y_s;                Marks2[( i - 7 ) * 4 + 2] = CenterX - x_e;                Marks2[( i - 7 ) * 4 + 3] = CenterY + y_e;            }            else if(i <= 11)            {                int degree = (i - 9) * 30;                int y_s = (int) (Math.sin(Math.toRadians(degree)) * R1);                int x_s = (int) (Math.cos(Math.toRadians(degree)) * R1);                int y_e = (int) (Math.sin(Math.toRadians(degree)) * Radius);                int x_e = (int) (Math.cos(Math.toRadians(degree)) * Radius);                Marks2[( i - 7 ) * 4] = CenterX - x_s;                Marks2[( i - 7 ) * 4 + 1] = CenterY - y_s;                Marks2[( i - 7 ) * 4 + 2] = CenterX - x_e;                Marks2[( i - 7 ) * 4 + 3] = CenterY - y_e;            }        }        canvas.drawLines(Marks,MarkPaint);        canvas.drawLines(Marks2,MarkPaint);        Paint TextPaint = new Paint();        TextPaint.setAntiAlias(true);        TextPaint.setTextAlign(Paint.Align.CENTER);        TextPaint.setTextSize(60);        TextPaint.setColor(Color.BLACK);        for(int i = 0 ;i < 12;i++)        {            int degree = i * 30;            int x_s = (int) (Math.sin(Math.toRadians(degree)) * ( R1 - 50 ));            int y_s = (int) (Math.cos(Math.toRadians(degree)) * ( R1 - 50 ));            String str = "";            if(i == 0)            {                str = "12";            }            else{                str = String.valueOf(i);            }            Paint.FontMetrics fm = TextPaint.getFontMetrics();            canvas.drawText(str,CenterX + x_s,CenterY - y_s + (fm.descent - fm.ascent) / 2 - fm.descent,TextPaint);        }        /*         获取当前时间         */        long time = System.currentTimeMillis();        SimpleDateFormat format = new SimpleDateFormat("HH-mm-ss");        String str = format.format(time);        String[] Hour_Min_Second = str.split("-");        Paint drawPaint = new Paint();        drawPaint.setColor(Color.BLACK);        drawPaint.setStyle(Paint.Style.FILL);        /*        计算针的角度         */        int hour = Integer.valueOf(Hour_Min_Second[0]) % 12;        int min = Integer.valueOf(Hour_Min_Second[1]) % 60;        int second = Integer.valueOf(Hour_Min_Second[2]) % 60;        float degree_Hour = hour * 30 + min * 0.5f;        float degree_Min = min * 360 / 60;        float degree_Second = second * 360 / 60;        /*        时针         */        canvas.rotate(degree_Hour,width/2,height/2);        canvas.drawRect(width / 2 - 10,height / 2 - R1 / 2,width / 2 + 10,height / 2 + 40,drawPaint);        /*        分针         */        canvas.rotate(360 - degree_Hour,width/2,height/2);        canvas.rotate(degree_Min,width/2,height/2);        canvas.drawRect(width / 2 - 10,height / 2 - R1 + 80,width / 2 + 10,height / 2 + 40,drawPaint);        /*        秒针         */        drawPaint.setColor(Color.RED);        canvas.rotate(360 - degree_Min,width/2,height/2);        canvas.rotate(degree_Second,width/2,height/2);        canvas.drawRect(width / 2 - 5,height / 2 - R1 + 40,width / 2 + 5,height / 2 + 50,drawPaint);        invalidate();    }}

 

更多相关文章

  1. framework之 ContextImpl文件解析
  2. Android外部存储(/storage/emulated/0)无法创建文件夹
  3. AndroidManifest.xml文件详解(uses-feature)
  4. Android Studio 导入so文件
  5. Android的string.xml文件中的特殊字符显示
  6. Hello Android - Android SDCard操作(文件读写,容量计算)
  7. Android 文件操作总结
  8. Android调用系统自带的文件管理器,打开指定路径

随机推荐

  1. H5调android 的方法修改UI无效
  2. cocos2d-x 编译成android apk常见错误
  3. android的ndk修改app_platform的方法,亲测
  4. Android字体设置
  5. Android一个Adapter的写法
  6. 音频的播放
  7. Android开发环境搭建
  8. Android中LayoutParams的用法
  9. Android(安卓)Http请求框架一:Get 和 Post
  10. Android(安卓)resource compilation fail