欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义控件(实现状态提示图表)

程序员文章站 2024-03-02 11:17:52
前面分析那么多系统源码了,也该暂停下来休息一下,趁昨晚闲着看见一个有意思的需求就操练一下分析源码后的实例演练—-自定义控件。 这个实例很适合新手入门自定义控件。先看下效果...

前面分析那么多系统源码了,也该暂停下来休息一下,趁昨晚闲着看见一个有意思的需求就操练一下分析源码后的实例演练—-自定义控件。

这个实例很适合新手入门自定义控件。先看下效果图:

横屏模式如下: Android自定义控件(实现状态提示图表)
竖屏模式如下:
Android自定义控件(实现状态提示图表)
看见没有,这个控件完全自定义的,连文字等都是自定义的,没有任何图片等资源,就仅仅是一个小的java文件,这个界面只有一个控件。如下咱们看下实现代码。

实例代码

如下就是整个工程的源码了。

自定义上面展示的控件areachartsview源码:

/**
 * author    : yanbo
 * date     : 2015-06-03
 * time     : 09:22
 * description : 自定义区域描述图表view
 */
public class areachartsview extends view {
  private paint mpaint;

  private int[] mzeropos = new int[2];
  private int[] mmaxypos = new int[2];
  private int[] mmaxxpos = new int[2];

  private int mwidth, mhight;
  private int mrealwidth, mrealhight;
  private string mtitley, mtitlex;

  private arraylist<integer> mxlevel = new arraylist<>();
  private arraylist<integer> mylevel = new arraylist<>();
  private arraylist<string> mgridleveltext = new arraylist<>();
  private arraylist<integer> mgridcolorlevel = new arraylist<>();
  private arraylist<integer> mgridtxtcolorlevel = new arraylist<>();

  private int mgridlevel = mxlevel.size() - 1;

  //title字符大小
  private int mxytitletextsize = 40;

  private int mmeasurexpos, mmeasureypos;

  public areachartsview(context context, attributeset attrs) {
    super(context, attrs);

    mpaint = new paint(paint.anti_alias_flag);
    mpaint.setantialias(true);
    mpaint.setfilterbitmap(true);
  }

  @override
  protected void onlayout(boolean changed, int left, int top, int right, int bottom) {
    super.onlayout(changed, left, top, right, bottom);
    mwidth = getwidth();
    mhight = getheight();
  }

  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);

    initposition();
    drawxytitle(canvas);
    drawxyline(canvas);
    drawcontent(canvas);
  }

  private void initposition() {
    //初始化坐标图的xy交点原点坐标
    mzeropos[0] = mxytitletextsize * 2;
    mzeropos[1] = mhight - mxytitletextsize * 4;
    //初始化坐标图的x轴最大值坐标
    mmaxxpos[0] = mwidth;
    mmaxxpos[1] = mhight - mxytitletextsize * 4;
    //初始化坐标图的y轴最大值坐标
    mmaxypos[0] = mxytitletextsize * 2;
    mmaxypos[1] = mxytitletextsize * 2;
  }

  private void drawxytitle(canvas canvas) {
    mpaint.setcolor(color.parsecolor("#1fb0e7"));
    mpaint.settextsize(mxytitletextsize);
    mpaint.settextalign(paint.align.left);
    //画y轴顶的title
    canvas.drawtext(mtitley, mmaxypos[0] - mxytitletextsize * 2, mmaxypos[1] - mxytitletextsize, mpaint);
    mpaint.settextalign(paint.align.right);
    //画x轴顶的title
    canvas.drawtext(mtitlex, mmaxxpos[0], mmaxxpos[1] + mxytitletextsize * 2, mpaint);
  }

  private void drawxyline(canvas canvas) {
    mpaint.setcolor(color.dkgray);
    mpaint.settextalign(paint.align.right);
    //画xy轴
    canvas.drawline(mmaxypos[0], mmaxypos[1], mzeropos[0], mzeropos[1], mpaint);
    canvas.drawline(mzeropos[0], mzeropos[1], mmaxxpos[0], mmaxxpos[1], mpaint);
  }

  private void drawcontent(canvas canvas) {
    mgridlevel = mxlevel.size() - 1;
    //计算出偏移title等显示尺标后的真实xy轴长度,便于接下来等分
    mrealwidth = (mwidth - mxytitletextsize * 2);
    mrealhight = (mhight - mxytitletextsize * 4);
    //算出等分间距
    int offsetx = mrealwidth/(mgridlevel);
    int offsety = mrealhight/(mgridlevel+1);
    //循环绘制content
    for (int index=0; index<mgridlevel+1; index++) {
      mpaint.setcolor(color.dkgray);
      mpaint.settextalign(paint.align.right);
      mpaint.settextsize(mxytitletextsize-5);
      //绘制x轴的那些坐标区间点,包含0点坐标
      canvas.drawtext(string.valueof(mxlevel.get(index)), mzeropos[0]+(index*offsetx), mzeropos[1] + mxytitletextsize, mpaint);

      if (index != 0) {
        //绘制y轴坐标区间点,不包含0点坐标,x轴已经画过了
        canvas.drawtext(string.valueof(mylevel.get(index)), mzeropos[0], mzeropos[1]-(index*offsety), mpaint);
      }

      if (index == mgridlevel) {
        //坐标区间 = 真实区间 + 1
        break;
      }

      mpaint.setcolor(mgridcolorlevel.get(mgridlevel - 1 - index));
      mpaint.setstyle(paint.style.fill);
      //绘制区间叠加图谱方块,从远到0坐标,因为小的图会覆盖大的图
      canvas.drawrect(mmaxypos[0], mmaxypos[1] + index*offsety, mmaxxpos[0]-index*offsetx, mmaxxpos[1], mpaint);

      mpaint.setcolor(mgridtxtcolorlevel.get(index));
      mpaint.settextalign(paint.align.right);
      mpaint.settextsize(mxytitletextsize);
      //绘制每个方块状态区间的提示文字
      canvas.drawtext(mgridleveltext.get(index), mmaxxpos[0] - index * offsetx - mxytitletextsize,
          mmaxypos[1] + index * offsety + mxytitletextsize, mpaint);
    }
    //绘制当前坐标
    drawnotice(canvas, offsetx, offsety);
  }

  private void drawnotice(canvas canvas, int offsetx, int offsety) {
    int realposx = 0;
    int realposy = 0;
    //计算传入的x值与真实屏幕坐标的像素值的百分比差值转换
    for (int index=0; index<mgridlevel; index++) {
      if (mmeasurexpos >= mxlevel.get(index) && mmeasurexpos < mxlevel.get(index+1)) {
        int subvalue = mmeasurexpos - mxlevel.get(index);
        int offset = mxlevel.get(index+1) - mxlevel.get(index);
        realposx = mzeropos[0] + index*offsetx + (subvalue / offset);
        break;
      }
    }
    //计算传入的y值与真实屏幕坐标的像素值的百分比差值转换
    for (int index=0; index<mgridlevel; index++) {
      if (mmeasureypos >= mylevel.get(index) && mmeasureypos < mylevel.get(index+1)) {
        int subvalue = mmeasureypos - mylevel.get(index);
        int offset = mylevel.get(index+1) - mylevel.get(index);
        realposy = mzeropos[1] - index*offsety - (offsety - (subvalue / offset));
        break;
      }
    }
    //画我们传入的坐标点的标记小红点
    mpaint.setcolor(color.red);
    mpaint.setstyle(paint.style.fill);
    canvas.drawcircle(realposx, realposy, 8, mpaint);

    int[] centerpos = {mzeropos[0] + mrealwidth/2, mzeropos[1] - mrealhight/2};

    mpaint.setcolor(color.white);
    mpaint.setstyle(paint.style.fill_and_stroke);
    rectf rectf = null;
    path path = new path();
    //画红点旁边的提示框和文字,有四个区域,然后提示框的小三角指标方位不同
    if (realposx <= centerpos[0] && realposy >= centerpos[1]) {
      //left-bottom
      //画三角形
      path.moveto(realposx+5, realposy+5);
      path.lineto(realposx+15, realposy+15);
      path.lineto(realposx+15, realposy-15);
      //画矩形背景
      rectf = new rectf(realposx+15, realposy-40, realposx+200, realposy + 30);
      canvas.drawroundrect(rectf, 15, 15, mpaint);
      //画提示框的文字
      mpaint.reset();
      mpaint.setcolor(color.red);
      mpaint.settextsize(mxytitletextsize - 5);
      canvas.drawtext("("+mmeasurexpos+", "+mmeasureypos+")", realposx+30, realposy, mpaint);
    }
    else if (realposx <= centerpos[0] && realposy < centerpos[1]) {
      //left-top
      path.moveto(realposx+5, realposy+5);
      path.lineto(realposx+15, realposy+15);
      path.lineto(realposx + 15, realposy - 15);

      rectf = new rectf(realposx+15, realposy - 20, realposx+200, realposy + 50);
      canvas.drawroundrect(rectf, 15, 15, mpaint);

      mpaint.reset();
      mpaint.setcolor(color.red);
      mpaint.settextsize(mxytitletextsize - 5);
      canvas.drawtext("("+mmeasurexpos+", "+mmeasureypos+")", realposx+30, realposy+20, mpaint);
    }
    else if (realposx > centerpos[0] && realposy >= centerpos[1]) {
      //right-bottom
      path.moveto(realposx-5, realposy+5);
      path.lineto(realposx-15, realposy+15);
      path.lineto(realposx - 15, realposy - 15);

      rectf = new rectf(realposx-200, realposy-40, realposx-15, realposy + 30);
      canvas.drawroundrect(rectf, 15, 15, mpaint);

      mpaint.reset();
      mpaint.setcolor(color.red);
      mpaint.settextsize(mxytitletextsize - 5);
      canvas.drawtext("("+mmeasurexpos+", "+mmeasureypos+")", realposx-180, realposy, mpaint);
    }
    else if (realposx > centerpos[0] && realposy < centerpos[1]) {
      //right-top
      path.moveto(realposx-5, realposy+5);
      path.lineto(realposx-15, realposy+15);
      path.lineto(realposx - 15, realposy - 15);

      rectf = new rectf(realposx-200, realposy - 20, realposx-15, realposy + 50);
      canvas.drawroundrect(rectf, 15, 15, mpaint);

      mpaint.reset();
      mpaint.setcolor(color.red);
      mpaint.settextsize(mxytitletextsize - 5);
      canvas.drawtext("("+mmeasurexpos+", "+mmeasureypos+")", realposx-180, realposy+30, mpaint);
    }

    path.close();
    mpaint.setcolor(color.white);
    mpaint.setstyle(paint.style.fill_and_stroke);
    canvas.drawpath(path, mpaint);
  }

  //设置当前比值
  public void updatevalues(int x, int y) {
    mmeasurexpos = x;
    mmeasureypos = y;

    postinvalidate();
  }

  //设置xy轴顶角的title字体大小
  public void settitletextsize(int size) {
    mxytitletextsize = size;
  }

  //初始化x轴的坐标区间点值,可以不均等分
  public void initxleveloffset(arraylist<integer> list) {
    mxlevel.clear();
    mxlevel.addall(list);
  }

  //初始化y轴的坐标区间点值,可以不均等分
  public void inityleveloffset(arraylist<integer> list) {
    mylevel.clear();
    mylevel.addall(list);
  }

  //初始化每个区间的提示文字,如果不想显示可以设置""
  public void initgridleveltext(arraylist<string> list) {
    mgridleveltext.clear();
    mgridleveltext.addall(list);
  }

  //初始化每个区间的颜色
  public void initgridcolorlevel(arraylist<integer> list) {
    mgridcolorlevel.clear();
    mgridcolorlevel.addall(list);
  }

  //初始化每个区间的提示文字颜色
  public void initgridtxtcolorlevel(arraylist<integer> list) {
    mgridtxtcolorlevel.clear();
    mgridtxtcolorlevel.addall(list);
  }

  //初始化xy轴title
  public void inittitlexy(string x, string y) {
    mtitlex = x;
    mtitley = y;
  }
}

再来看下布局文件:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.yanbober.customerviewdemo.areachartsview.areachartsview
    android:id="@+id/area_charts_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"/>

</relativelayout>

再看看主界面:

public class mainactivity extends appcompatactivity {
  private areachartsview mareachartsview;
  private timer timer;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    mareachartsview = (areachartsview) this.findviewbyid(r.id.area_charts_view);

    //初始化自定义图表的规格和属性
    arraylist<integer> mxlevel = new arraylist<>();
    arraylist<integer> mylevel = new arraylist<>();
    arraylist<string> mgridleveltext = new arraylist<>();
    arraylist<integer> mgridcolorlevel = new arraylist<>();
    arraylist<integer> mgridtxtcolorlevel = new arraylist<>();
    //初始化x轴坐标区间
    mxlevel.add(0);
    mxlevel.add(60);
    mxlevel.add(90);
    mxlevel.add(100);
    mxlevel.add(110);
    mxlevel.add(120);
    //初始化y轴坐标区间
    mylevel.add(0);
    mylevel.add(90);
    mylevel.add(140);
    mylevel.add(160);
    mylevel.add(180);
    mylevel.add(200);
    //初始化区间颜色
    mgridcolorlevel.add(color.parsecolor("#1fb0e7"));
    mgridcolorlevel.add(color.parsecolor("#4fc7f4"));
    mgridcolorlevel.add(color.parsecolor("#4fddf2"));
    mgridcolorlevel.add(color.parsecolor("#90e9f4"));
    mgridcolorlevel.add(color.parsecolor("#b2f6f1"));
    //初始化区间文字提示颜色
    mgridtxtcolorlevel.add(color.parsecolor("#ea8868"));
    mgridtxtcolorlevel.add(color.parsecolor("#ea8868"));
    mgridtxtcolorlevel.add(color.parsecolor("#ea8868"));
    mgridtxtcolorlevel.add(color.white);
    mgridtxtcolorlevel.add(color.black);
    //初始化区间文字
    mgridleveltext.add("异常");
    mgridleveltext.add("过高");
    mgridleveltext.add("偏高");
    mgridleveltext.add("正常");
    mgridleveltext.add("偏低");

    mareachartsview.initgridcolorlevel(mgridcolorlevel);
    mareachartsview.initgridleveltext(mgridleveltext);
    mareachartsview.initgridtxtcolorlevel(mgridtxtcolorlevel);
    mareachartsview.initxleveloffset(mxlevel);
    mareachartsview.inityleveloffset(mylevel);
    mareachartsview.inittitlexy("投入量(h)", "产出量(h)");
  }

  @override
  protected void onstart() {
    super.onstart();
    timer = new timer();
    timer.schedule(new timertask() {
      @override
      public void run() {
        random random = new random();
        int x = random.nextint(120) % (120 + 1) + 0;
        random randomy = new random();
        int y = randomy.nextint(200) % (200 + 1) + 0;
        //随机模拟赋值
        mareachartsview.updatevalues(x, y);
      }
    }, 0, 1000);
  }

  @override
  protected void onpause() {
    super.onpause();
    timer.cancel();
  }
}

总结

上面代码很简单,核心的都已经注释了,不需要过多解释。核心思路就是一些坐标点的计算。该控件支持设置mergin及width与hight等属性,支持自定义所有颜色及显示及坐标区分等,唯一缺陷就是没来得及写attr属性xml设置这些值,有兴趣的自己实现吧,我是没时间了。

可以发现,自定义view无非就是重写前面文章分析的那三个方法而已。

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