广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义View实现时钟效果
  • 884
分享到

Android自定义View实现时钟效果

2024-04-02 19:04:59 884人浏览 安东尼
摘要

本文实例为大家分享了Android自定义View实现时钟效果的具体代码,供大家参考,具体内容如下 自定义时钟 初学自定义View,先画一个不太成熟的时钟(甚至只有秒针) 时钟效果

本文实例为大家分享了Android自定义View实现时钟效果的具体代码,供大家参考,具体内容如下

自定义时钟

初学自定义View,先画一个不太成熟的时钟(甚至只有秒针)

时钟效果

@SuppressLint("DrawAllocation")
public class ClockView extends View {

    private final Context mContext;
    private canvas mCanvas;// 画布
    private Paint clockPaint;// 表盘画笔
    private Paint textPaint;// 文字画笔
    private Paint secondPaint;// 秒针画笔
    private int x,y;// 表中心坐标
    private Thread refreshThread;
    private boolean isStop = false;
    // 用于获取当前秒数
    private  Date currentDate;
    private SimpleDateFORMat sp = new SimpleDateFormat("ss");

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 0){
                invalidate();//每隔一秒刷新一次
            }
        }
    };

    public ClockView(Context context) {
        this(context,null);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context,null,0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context,null,0,0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.mContext = context;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMeasure;
        // 设置宽高一致
        if(widthMeasureSpec > heightMeasureSpec){
            super.onMeasure(heightMeasureSpec, heightMeasureSpec);
            widthMeasure  = getMeasuredHeight();
        }else{
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            widthMeasure = getMeasuredWidth();
        }
        initPaint(widthMeasure);
    }

    private void initPaint(int width){
        // 表盘画笔
        clockPaint = new Paint();
        clockPaint.setColor(mContext.getColor(R.color.black));// 颜色
        clockPaint.setAntiAlias(true);// 抗锯齿
        clockPaint.setStyle(Paint.Style.STROKE);// 样式
        clockPaint.setStrokeWidth(width/80f);// 宽度

        //字体画笔
        textPaint = new Paint();
        textPaint.setColor(mContext.getColor(R.color.black));
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(width/16f);
        textPaint.setTextAlign(Paint.Align.CENTER);

        // 秒针画笔
        secondPaint = new Paint();
        secondPaint.setColor(mContext.getColor(R.color.red));
        secondPaint.setAntiAlias(true);
        secondPaint.setStyle(Paint.Style.FILL);
        secondPaint.setStrokeWidth(5f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mCanvas = canvas;

        // 获取画布中心坐标
        x = getWidth() / 2;
        y = getHeight() / 2;

        // 绘制表盘
        mCanvas.drawCircle(x, y,getWidth()/40f, clockPaint);// 表盘中心
        mCanvas.drawCircle(x, y, x -getWidth()/40f, clockPaint);// 表盘边框

        // 绘制刻度
        for(int i = 1;i <= 60;i++){
            mCanvas.rotate(6, x, y);
            if(i%5 == 0){
                // 绘制大刻度
                mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()*5/80f, clockPaint);
            }else{
                // 绘制小刻度
                mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()/20f, clockPaint);
            }
        }

        // 绘制 1-12小时 字体
        for(int i = 1;i <= 60;i++){
            if(i%5 == 0){
                float x1 = (float) Math.sin(Math.toRadians(6 * i)) * (y * 3 / 4f) + x;
                float y1 = y - (float) Math.cos(Math.toRadians(6 * i)) * (y * 3 / 4f) + getWidth()/40f;
                mCanvas.drawText(String.valueOf(i/5), x1, y1, textPaint);
            }
        }

        // 绘制秒针
        currentDate = new Date(System.currentTimeMillis());
        int ss = Integer.parseInt(sp.format(currentDate));// 获取当前秒数
        // 根据当前秒数 计算出秒针的 start 及 end 坐标
        float sin = (float) Math.sin(Math.toRadians(6 * ss));
        float cos = (float) Math.cos(Math.toRadians(6 * ss));
        float x0 = x - sin * (y / 10f);
        float y0 = y + cos * (y / 10f);
        float x1 = x + sin * (y * 13 / 20f);
        float y1 = y - cos * (y * 13 / 20f);
        mCanvas.drawLine(x0, y0, x1, y1, secondPaint);
        mCanvas.drawCircle(x, y,getWidth()/80f, secondPaint);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        refreshThread = new Thread(){
            @Override
            public void run() {
                super.run();
                while (!isStop){
                    SystemClock.sleep(1000);
                    mHandler.sendEmptyMessage(0);
                }
            }
        };
        refreshThread.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeCallbacksAndMessages(null);
        isStop = true;
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android自定义View实现时钟效果

本文链接: https://www.lsjlt.com/news/162234.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android自定义View实现时钟效果
    本文实例为大家分享了Android自定义View实现时钟效果的具体代码,供大家参考,具体内容如下 自定义时钟 初学自定义View,先画一个不太成熟的时钟(甚至只有秒针) 时钟效果 ...
    99+
    2022-11-12
  • android自定义view实现钟表效果
    本文实例为大家分享了android view实现钟表的具体代码,供大家参考,具体内容如下 先看效果图: 自定义view大家肯定已经不陌生了,所以直接今天直接步入正题:如何利用...
    99+
    2022-06-06
    view Android
  • Android Canvas自定义实现时钟效果
    Android之Canvas自定义画一个时钟,供大家参考,具体内容如下 自定义控件,在安卓是也是一种无所不能的技术了,所有自带控件,以及组合自带控件不能实现的一些效果,我们都可...
    99+
    2022-06-06
    canvas Android
  • Android自定义View实现时钟功能
    最近在练习自定义view, 想起之前面试的时候笔试有道题是写出自定义一个时钟的关键代码. 今天就来实现一下. 步骤依然是先分析, 再上代码. 实现效果 View分析 时钟主要分为五...
    99+
    2022-11-13
  • Android自定义控件实现时钟效果
    在学习安卓群英传自定义控件章节的时候,有一个例子是绘制时钟,在实现了书上的例子后就想看这个时钟能不能动起来。 这里选择延迟一秒发送消息重绘view来实现的动画,对外提供了开启时...
    99+
    2022-06-06
    Android
  • Android自定义View实现钟摆效果进度条PendulumView
    在网上看到了一个IOS组件PendulumView,实现了钟摆的动画效果。由于原生的进度条确实是不好看,所以想可以自定义View实现这样的效果,以后也可以用于加载页面的进度条。...
    99+
    2022-06-06
    view Android
  • Android自定义View实现扫描效果
    本文实例为大家分享了Android自定义View实现扫描效果的具体代码,供大家参考,具体内容如下 演示效果如下: 实现内容: 1、控制动画是竖向或者横向 2、控制动画初始是从底部/...
    99+
    2022-11-12
  • Android中自定义view实现侧滑效果
    效果图: 看网上的都是两个view拼接,默认右侧的不显示,水平移动的时候把右侧的view显示出来。但是看最新版QQ上的效果不是这样的,但给人的感觉却很好,所以献丑来一发比较高...
    99+
    2022-06-06
    view 自定义view Android
  • Android自定义Animation实现View摇摆效果
    使用自定义Animation,实现View的左右摇摆效果,如图所示: 代码很简单,直接上源码 activity_maini.xml布局文件: <?xml v...
    99+
    2022-06-06
    view animation Android
  • Android自定义View实现折线图效果
    下面就是结果图(每种状态用一个表情图片表示): 一、主页面的布局文件如下: <RelativeLayout xmlns:android="http://schema...
    99+
    2022-06-06
    折线图 view Android
  • Android自定义View实现打字机效果
    一、先来看看效果演示 二、实现原理: 这个其实不难实现,通过一个定时器不断调用TextView的setText就行了,在setText的时候播放打字的音效。 具体代码如下:...
    99+
    2022-06-06
    view 打字机 Android
  • Android自定义view实现输入框效果
    本文实例为大家分享了Android自定义view实现输入框的具体代码,供大家参考,具体内容如下 自定义输入框的View package com.fenghongzhang.day...
    99+
    2022-11-11
  • Android自定义view实现半圆环效果
    本文实例为大家分享了Android自定义view实现半圆环的具体代码,供大家参考,具体内容如下 1.自定义属性 <declare-styleable name="Semicir...
    99+
    2022-11-13
  • Android自定义View实现标签流效果
    本文实例为大家分享了Android自定义View实现标签流效果的具体代码,供大家参考,具体内容如下 一、概述 Android自定义View实现标签流效果,一行放不下时会自动换行,用户...
    99+
    2022-11-13
  • Android自定义View实现水波纹效果
    介绍:水波纹散开效果的控件在 App 里面还是比较常见的,例如 网易云音乐歌曲识别,附近搜索场景。看下实现的效果:实现思路: 先将最大圆半径与最小圆半径间距分成几等份,从内到外,Paint 透明度依次递减,绘制出同心圆,然后不断的改变这些同...
    99+
    2023-05-30
    android view 水波纹
  • Android使用自定义View实现横行时间轴效果
    前言 本篇文章会说下如何使用并且要用麻烦的自定义 view 去实现时间轴效果,以及如何分析、实现自定义 view。 需要具备的知识:Paint、Canvas、自定义 view ...
    99+
    2022-06-06
    view 自定义view Android
  • Android自定义View实现APP启动页倒计时效果
    Android自定义View实现APP启动页倒计时效果,供大家参考,具体内容如下 之前也是做过APP启动页的倒计时效果,但是只有文字变化,没有动画效果,这次通过使用自定义View控件...
    99+
    2022-11-13
  • Android自定义View控件实现刷新效果
    三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b. LayoutInflate...
    99+
    2022-06-06
    view Android
  • Android自定义View实现闪耀字体效果
    本文实例为大家分享了闪耀字体效果的具体代码,供大家参考,具体内容如下 import android.content.Context; import android.graph...
    99+
    2022-06-06
    view 字体 Android
  • Android自定义View实现水面上涨效果
    实现效果如下: 实现思路: 1、如何实现圆中水面上涨效果:利用Paint的setXfermode属性为PorterDuff.Mode.SRC_IN画出进度所在的矩形与圆的交...
    99+
    2022-06-06
    view Android
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作