当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿小米时钟效果

Android仿小米时钟效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

黍,斗战神虚灵,蒋艳萍不照雅全图

我在一个[博客] android高仿小米时钟(使用camera和matrix实现3d效果)上面看到了小米时钟实现.特别感兴趣.就认真的看了一遍.并自己敲了一遍.下面说下我自己的理解和我的一些改进的地方效果真的特别棒就发布了自己的时钟应用。

先上图(电脑没有gif截图软件.大家凑合看.哪个软件好也可以给我推荐下)

图一

话不多说,首先自定义控件ximiclockview继承view  并做一些初始化的操作
看到的漂亮时钟图片我自己画的效果图(以后妈妈再也不用担心我迟到了)

图二

public ximiclockview(context context) {
  super(context);
  init(context, null);
 }

 public ximiclockview(context context, attributeset attrs) {
  super(context, attrs);
  init(context, attrs);
 }

 public ximiclockview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  init(context, attrs);
 }

 @requiresapi(api = build.version_codes.lollipop)
 public ximiclockview(context context, attributeset attrs, int defstyleattr, int defstyleres) {
  super(context, attrs, defstyleattr, defstyleres);
  init(context, attrs);
 }

 /**
  * 进行一些初始化的操作
  *
  * @param context
  * @param attrs
  */
 private void init(context context, attributeset attrs) {
  if (attrs == null) return;
  typedarray array = context.obtainstyledattributes(attrs, r.styleable.myclockview);
  backgroundcolor = array.getcolor(r.styleable.myclockview_clockbackcolor, color.parsecolor("#2078a8"));
  drakcolor = array.getcolor(r.styleable.myclockview_clockdarkcolor, color.parsecolor("#96c2d8"));
  lightcolor = array.getcolor(r.styleable.myclockview_clocklightcolor, color.parsecolor("#ffffff"));
  array.recycle();//注意这里别忘了调用recycle()方法,[原因](http://www.cnblogs.com/kissazi2/p/4049982.html)
  //设置背景色
  setbackgroundcolor(backgroundcolor);
  //文本画笔
  textpaint = new paint();
  textpaint.setcolor(drakcolor);
  textpaint.settextsize(25);
  textrect = new rect();
 }

然后在onsizechange方法中调用获取时钟半径,和一些其他的计算

 @override
 protected void onsizechanged(int w, int h, int oldw, int oldh) {
  super.onsizechanged(w, h, oldw, oldh);
  //获取时钟的半径
  mradius = math.min(w - getpaddingleft() - getpaddingright(), h - getpaddingtop() - getpaddingbottom()) / 2;
  //为了防止时钟旋转的时候超出了界限,默认加一个padding值
  defaultpadding = 0.12f * mradius;
  //圆弧的宽度
  mcirclewidth = 0.012f * mradius;
  //圆环的宽度
  mcircleringwidth = 0.12f*mradius;
  paddingleft = w / 2 - mradius + getpaddingleft() + defaultpadding;
  paddingtop = h / 2 - mradius + getpaddingtop() + defaultpadding;
  paddingright = w / 2 - mradius + getpaddingright() + defaultpadding;
  paddingbottom = h / 2 - mradius + getpaddingbottom() + defaultpadding;
  msweepgradient = new sweepgradient(w / 2,h / 2 ,new int[]{drakcolor,lightcolor},new float[]{0.75f,1f});
 }

画text

@override
 protected void ondraw(canvas canvas) {
  mcanvas = canvas;
  drawtimetext();
 }

 /**
  * 画12 /3 /6 /9时间
  */
 private void drawtimetext() {
  string str = "12";
  textpaint.gettextbounds(str, 0, str.length(), textrect);
  int lengthtextwidth = textrect.width();
  int textheight = textrect.height();
  int width=getwidth();
  int height=getheight();
  mcanvas.save();//保存画布的状态
  mcanvas.drawtext(str, width / 2-lengthtextwidth/2,paddingtop+textheight,textpaint);
  str="3";
  textpaint.gettextbounds(str, 0, str.length(), textrect);
  int smalltextwidth = textrect.width();
  mcanvas.drawtext("3",width-paddingright-lengthtextwidth/2-smalltextwidth/2,height/2+textheight/2,textpaint);
  mcanvas.drawtext("6",width / 2-smalltextwidth/2,height-paddingbottom,textpaint);
  mcanvas.drawtext("9",paddingleft,height/2+textheight/2,textpaint);
  mcanvas.restore();//取出画布的状态
 }

崩溃写完了,我发表时候提示我没有登录,然后发表异常,然后我一刷新没了,,,又重新写一遍,

接下来画圆弧

 /**
  * 画圆弧
  */
 private void drawcirclearc() {
  mcanvas.save();
  mcirclepaint.setstyle(paint.style.stroke);//设置空心模式
  mcirclepaint.setstrokewidth(mcirclewidth);//设置圆弧宽度
  mcirclerectf.set(paddingleft + textrect.width() / 2, paddingtop + textrect.height() / 2, getwidth() - paddingright - textrect.width() / 2, getheight() - paddingbottom - textrect.height() / 2);
  for (int x = 0; x < 4; x++) {
  //圆弧分四段来画,一段话80度
   mcanvas.drawarc(mcirclerectf, 5 + 90 * x, 80, false, mcirclepaint);
  }
  mcanvas.restore();
 }

画刻度

/**
  * 画圆环和刻度
  */
 private void drawcirclering() {
  mcanvas.save();
  //画圆环
  mcircleringrectf.set(paddingleft+textrect.height()/2+1.5f*mcircleringwidth,paddingtop+textrect.height()/2+1.5f*mcircleringwidth,getwidth()-paddingright-textrect.height()/2-1.5f*mcircleringwidth,
    getheight()-paddingbottom-textrect.height()/2-1.5f*mcircleringwidth);
  mmatrix.setrotate(mdegrees-90,getwidth()/2,getheight()/2);
  msweepgradient.setlocalmatrix(mmatrix);
  mcircleringpaint.setstyle(paint.style.stroke);
  mcircleringpaint.setstrokewidth(mcircleringwidth);
  mcircleringpaint.setshader(msweepgradient);
  mcanvas.drawarc(mcircleringrectf,0,360,false,mcircleringpaint);
  //画刻度
  mscalelinepaint.setstrokewidth(0.1f*mcircleringwidth);//设置线的宽度
  for (int i=0;i<200;i++){
   //画刻度线
   mcanvas.drawline(getwidth()/2,paddingtop+textrect.height()/2+mcircleringwidth,getwidth()/2,paddingtop+textrect.height()/2+2*mcircleringwidth,mscalelinepaint);
   mcanvas.rotate(1.8f,getwidth()/2,getheight()/2);//旋转角度
  }
  mcanvas.restore();
 }

画秒针

 /**
  * 画秒针
  * 秒针针是不规则的图形
  * 就用到了path这个类
  *
  */
 private void drawsoundhand() {
  mcanvas.save();
  path.reset();
  mcanvas.rotate(mdegrees,getwidth()/2,getheight()/2);//旋转的角度和旋转的圆心
  path.moveto(getwidth()/2,paddingtop+textrect.height()/2+0.27f*mradius);//开始的点
  path.lineto(getwidth()/2+0.03f*mradius,paddingtop+textrect.height()/2+0.31f*mradius);//直线到这个位置
  path.lineto(getwidth()/2-0.03f*mradius,paddingtop+textrect.height()/2+0.31f*mradius);//直线到这个位置
  path.close();
  mcanvas.drawpath(path,msoundhandpaint);
  mcanvas.restore();
 }

画时针

 /**
  * 画时针
  */
 private void drawhourhand() {
  mcanvas.save();
  mcanvas.rotate(mdegreeh,getwidth()/2,getheight()/2);
  hourhandpath.reset();
  hourhandpath.moveto(getwidth()/2-0.02f*mradius,getheight()/2);
  hourhandpath.lineto(getwidth()/2-0.01f*mradius,getheight()/2-0.35f*mradius);
  //贝塞尔曲线
  hourhandpath.quadto(getwidth()/2,getheight()/2-0.38f*mradius,getwidth()/2+0.01f*mradius,getheight()/2-0.35f*mradius);
  hourhandpath.lineto(getwidth()/2+0.02f*mradius,getheight()/2);
  hourhandpath.close();
  mcanvas.drawpath(hourhandpath,mhourhandpaint);
  mcanvas.restore();
 }

画分针

 /**
  * 画分针
  */
 private void drawminnutehand() {
  mcanvas.save();
  mcanvas.rotate(mdegreem,getwidth()/2,getheight()/2);
  mminutepath.reset();
  mminutepath.moveto(getwidth()/2-0.012f*mradius,getheight()/2);
  mminutepath.lineto(getwidth()/2-0.006f*mradius,getheight()/2-0.40f*mradius);
  mminutepath.quadto(getwidth()/2,getheight()/2-0.43f*mradius,getwidth()/2+0.006f*mradius,getheight()/2-0.40f*mradius);
  mminutepath.lineto(getwidth()/2+0.012f*mradius,getheight()/2);
  mminutepath.close();
  mcanvas.drawpath(mminutepath,mminutepaint);
  //画圈圈盖着时针的尾部
  mcanvas.drawcircle(getwidth()/2,getheight()/2,0.03f*mradius,mminutepaint);
  mcanvas.drawcircle(getwidth()/2,getheight()/2,0.015f*mradius,mcircleminpaint);
  mcanvas.restore();
 }

上我的measure的方法

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  setmeasureddimension(getmeasure(widthmeasurespec), getmeasure(heightmeasurespec));
 }

 private int getmeasure(int measurespec) {
  int result;
  int mode = measurespec.getmode(measurespec);
  int size = measurespec.getsize(measurespec);
  if (mode == measurespec.exactly) {
   result = size;
  } else {
   result = 800;
   if (mode == measurespec.at_most) {
    result = math.min(result, size);
   }
  }
  return result;
 }

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网