美文网首页
时钟的布局实现

时钟的布局实现

作者: 果冻都烂了 | 来源:发表于2016-11-12 01:22 被阅读0次

这个案例是通过view来制作一个Android时钟

//创建一个类继承view

public class ClockView extends View {

   private static final String TAG = "ClockView";

   private final Paint mPaint2;

   private final Paint mPaint3;

   private final Paint mPaint4;

   private final Paint mPaint5;

   private int mCx;

   private int mCy;

   private int mRadius;

   private final Paint mPaint;

   private final Paint mPaint1;

   private int CIRCLESIZE=3; //环到外边框的距离

   private int DIGITSLONG=20;//长刻度距离

   private int DIGISTSHART=10;//断刻度距离

   private int mTextsize=20;//文本的大小

   private Bitmap mbitmap;

   private int mHourDegree;

   private int mMinuteDegree;

   private int mSecondDegree;

   private Paint mBackgroundPaint;

   public ClockView(Context context, AttributeSet attrs) {

       super(context, attrs);

       //圆环的笔

       mPaint = new Paint();

       mPaint.setStrokeWidth(5);

       mPaint.setStyle(Paint.Style.STROKE);

       mPaint.setAntiAlias(true);

       mPaint.setColor(Color.GRAY);

       //刻度的笔

       mPaint1 = new Paint();

       mPaint1.setColor(Color.GRAY);

       mPaint1.setAntiAlias(true);

       mPaint1.setStrokeWidth(3);

       //loge的笔

       mbitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.heima);

       mPaint2 = new Paint();

       //指针的笔

       mPaint3 = new Paint();

       mPaint3.setColor(Color.WHITE);

       mPaint3.setStrokeWidth(5);

       mPaint3.setAntiAlias(true);

       mPaint4 = new Paint();

       mPaint4.setColor(Color.WHITE);

       mPaint4.setStrokeWidth(4);

       mPaint4.setAntiAlias(true);

       mPaint5 = new Paint();

       mPaint5.setColor(Color.WHITE);

       mPaint5.setStrokeWidth(3);

       mPaint5.setAntiAlias(true);

   }

   @Override

   protected void onSizeChanged(int w, int h, int oldw, int oldh) {

       //原点

       mCx = w/2;

       mCy = h/2;

       //环半径

       mRadius = w/2-CIRCLESIZE;

       initBackgroundPaint();

   }

   private void initBackgroundPaint() {

       mBackgroundPaint = new Paint();

       mBackgroundPaint.setColor(Color.LTGRAY);

       int[] colors = {Color.BLACK, Color.LTGRAY, Color.WHITE, Color.LTGRAY};

//    float[] positions = {0.0f, 0.5f, 0.75f, 1};

       Shader shader = new RadialGradient(mCx, mCy, mRadius, colors, null, Shader.TileMode.CLAMP);

       mBackgroundPaint.setShader(shader);

   }

   @Override

//开始绘制

   protected void onDraw(Canvas canvas) {

       //    6. 画背景

       canvas.drawCircle(mCx, mCy, mRadius, mBackgroundPaint);

       //1.画环

       canvas.drawCircle(mCx,mCy,mRadius,mPaint);

       //2.画刻度

     /* int startX=mCx;

       int startY=5;

       int stopX=mCx;

       int stopY=20;

       canvas.rotate(30,mCx,mCy);

       canvas.drawLine(startX,startY,stopX,stopY,mPaint1);*/

       drawDigits(canvas);

       //3画数字

       drawText(canvas);

       //画loge

       drawLoge(canvas);

       //画指针

       drawArrows(canvas);

       //指针动起来 调用onAttachedToWindow  当view绑定到window的时候调用

   }

   private void drawArrows(Canvas canvas) {

     /* mpath=new Path();

       mpath.moveTo(mCx,mCy+DIGITSLONG);

       mpath.lineTo(mCx,mCy-DIGITSLONG);*/

       //canvas.drawPath(mpath,mPaint3);

       //进行指针动画

       canvas.rotate(mHourDegree,mCx,mCy);

       canvas.drawLine(mCx,mCy+DIGISTSHART,mCx,CIRCLESIZE*3+DIGITSLONG+mTextsize,mPaint3);

       canvas.rotate(-mHourDegree,mCx,mCy);

       canvas.rotate(mMinuteDegree,mCx,mCy);

       canvas.drawLine(mCx,mCy+DIGISTSHART,mCx,CIRCLESIZE+DIGITSLONG+mTextsize,mPaint4);

       canvas.rotate(-mMinuteDegree,mCx,mCy);

       canvas.rotate(mSecondDegree,mCx,mCy);

       canvas.drawLine(mCx,mCy+DIGISTSHART,mCx,DIGISTSHART+mTextsize,mPaint5);

       canvas.rotate(-mSecondDegree,mCx,mCy);

   }

   private void drawLoge(Canvas canvas) {

       canvas.drawBitmap(mbitmap,mCx-mTextsize/2,CIRCLESIZE+DIGITSLONG+mTextsize,mPaint2);

   }

   private void drawText(Canvas canvas) {

       for (int i=1;i<=12;i++){

           canvas.rotate(30,mCx,mCy);

           //调整位置

           canvas.rotate(-30*i,mCx,CIRCLESIZE+DIGITSLONG+mTextsize/2);

           canvas.drawText(i+"",mCx-CIRCLESIZE,CIRCLESIZE*2+DIGITSLONG+mTextsize/2,mPaint1);

           canvas.rotate(30*i,mCx,CIRCLESIZE+DIGITSLONG+mTextsize/2);

       }

   }

   private void drawDigits(Canvas canvas) {

       for (int i=0 ;i<60;i++) {

           int startX = mCx;

           int startY = CIRCLESIZE;

           int stopX = mCx;

           int stopY;

           if(i%5==0){

               stopY = DIGITSLONG;

           }else{

               stopY=DIGISTSHART;

           }

           canvas.drawLine(startX, startY, stopX, stopY, mPaint1);

           canvas.rotate(6, mCx, mCy);

       }

   }

   @Override

   //当view绑定到window的时候调用

   protected void onAttachedToWindow() {

       super.onAttachedToWindow();

       //开启指针动画

       startTick();

   }

   private void startTick() {

       //1秒执行一次

       postDelayed(runnable,1000);

   }

   private Runnable runnable=new Runnable() {

       @Override

       public void run() {

           //计算指针便宜角度

           calculateDegree();

           //重新绘制

           invalidate();

           //递归

           startTick();

       }

   };

   private void calculateDegree() {

       //获取当前时间

       Calendar instance = Calendar.getInstance();

       instance.setTimeInMillis(System.currentTimeMillis());

       int hour = instance.get(Calendar.HOUR);

       int minute = instance.get(Calendar.MINUTE);

       int second = instance.get(Calendar.SECOND);

       //计算角度

       mHourDegree = hour * 30;

       mMinuteDegree = minute * 6;

       mSecondDegree = second * 6;

   }

}

//布局中的代码

   android:layout_centerInParent="true"

   android:layout_width="200dp"

   android:layout_height="200dp"

   android:background="#ffffff"/>

相关文章

  • 时钟的布局实现

    这个案例是通过view来制作一个Android时钟 //创建一个类继承view public class Cloc...

  • Swift-时钟

    时钟的实现主要功能点有时针,分针,秒针旋转的角度,定时刷新,实现效果如下: 时钟布局: 定时刷新:

  • CSS Flex实现各种布局

    实现两栏布局 实现栅格布局 实现三栏布局 附录 双飞翼布局 圣杯布局

  • CSS简单布局任务

    实现一个单栏布局 实现一个三栏布局 实现圣杯布局 实现双飞翼布局 实现效果

  • js基础作业3

    实现文字时钟

  • Linux C/C++定时器的实现原理和使用方法

    定时器的实现原理 定时器的实现依赖的是CPU时钟中断,时钟中断的精度就决定定时器精度的极限。一个时钟中断源如何实现...

  • Xilinx FPGA器件中时钟资源的说明以及使用

    xilinx 时钟资源分为两种:全局时钟和第二全局时钟。 一、全局时钟资源 Xilinx 全局时钟采用全铜工艺实现...

  • CSS的几种布局实现

    左右布局 这个布局比较简单,直接使用float就可以实现 左中右布局 实现1:使用左右布局的思路实现 实现2:使用...

  • 【时钟中断的实现】

    效果 下面那一行,是main函数中的死循环输出变量!右上方的字符是时钟中断服务程序打印的。_start.asm c...

  • 时钟练习

    做了一个小时钟,本来想找ios的时钟模型,但是找不到就只能随便百度了一个。直接上代码吧。1.html布局: 布局比...

网友评论

      本文标题:时钟的布局实现

      本文链接:https://www.haomeiwen.com/subject/yntqpttx.html