当前位置: 移动技术网 > 移动技术>移动开发>Android > Android自定义控件之自定义属性(二)

Android自定义控件之自定义属性(二)

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

前言:
上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性。本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解。有关原理知识请参考android自定义控件基本原理详解(一)这篇文章。 

需求产生背景:
为何要引入自定义属性?当android提供的原生属性不能满足实际的需求的时候,比如我们需要自定义圆形百分比半径大小、圆形背景、圆形显示的位置、圆形进度的背景等等。这个时候就需要我们自定义属性了。 

自定义属性步骤:
1.)在res/values文件下添加一个attrs.xml文件,如果项目比较大的话,会导致attrs.xml代码相当庞大,这时可以根据相应的功能模块起名字,方便查找,例如:登录模块相关attrs_login.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="roundimageview">
  <attr name="borderradius" />
  <attr name="type" />
 </declare-styleable>

</resources>

2.)如何声明一组属性 
使用<declare-styleable name="percentview"></declare-styleable>来定义一个属性集合,name就是属性集合的名字,这个名字一定要起的见名知意。 

 <declare-styleable name="percentview">
 <!--添加属性-->
 </declare-styleable> 

然后就是定义属性值了,通过<attr name="textcolor" format="color" /> 方式定义属性值,属性名字同样也要起的见名知意,format表示这个属性的值的类型,类型有以下几种:
 •reference:引用资源
 •string:字符串
 •color:颜色
 •boolean:布尔值
 •dimension:尺寸值
 •float:浮点型
 •integer:整型
 •fraction:百分数
 •enum:枚举类型
 •flag:位或运算 

基于上面的要求,我们可以定义一下百分比控件属性

 <declare-styleable name="percentview">
  <attr name="percent_circle_gravity"><!--圆形绘制的位置-->
   <flag name="left" value="0" />
   <flag name="top" value="1" />
   <flag name="center" value="2" />
   <flag name="right" value="3" />
   <flag name="bottom" value="4" />
  </attr>
  <attr name="percent_circle_radius" format="dimension" /><!--圆形半径-->
  <attr name="percent_circle_progress" format="integer" /><!--当前进度值-->
  <attr name="percent_progress_color" format="color" /><!--进度显示颜色-->
  <attr name="percent_background_color" format="color" /><!--圆形背景色-->
 </declare-styleable>

3.)布局中如何使用 

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:lee="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">

 <com.whoislcj.views.percentview
  android:layout_width="200dp"
  android:layout_height="200dp"
  android:layout_margin="10dp"
  android:background="@color/red"
  android:padding="10dp"
  lee:percent_background_color="@color/gray"
  lee:percent_circle_gravity="left"
  lee:percent_circle_progress="30"
  lee:percent_circle_radius="50dp"
  lee:percent_progress_color="@color/blue" />

</linearlayout>

为属性集设置一个属性集名称,我这里用的lee,我这是因为实在想不起使用什么属性集名称了,建议在真正的项目中使用项目的缩写,比如微信可能就是使用wx。 

4.)自定义控件中如何获取自定义属性 
每一个属性集合编译之后都会对应一个styleable对象,通过styleable对象获取typedarray typedarray,然后通过键值对获取属性值,这点有点类似sharedpreference的取法。 

 typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.percentview);
 if (typedarray != null) {
  backgroundcolor = typedarray.getcolor(r.styleable.percentview_percent_background_color, color.gray);
  progresscolor = typedarray.getcolor(r.styleable.percentview_percent_progress_color, color.blue);
  radius = typedarray.getdimension(r.styleable.percentview_percent_circle_radius, 0);
  progress = typedarray.getint(r.styleable.percentview_percent_circle_progress, 0);
  gravity = typedarray.getint(r.styleable.percentview_percent_circle_gravity, center);
  typedarray.recycle();
  }

5.)完整示例 

public class percentview extends view {
 private final static string tag = percentview.class.getsimplename();
 private paint mpaint;
 private int backgroundcolor = color.gray;
 private int progresscolor = color.blue;
 private float radius;
 private int progress;
 private float centerx = 0;
 private float centery = 0;
 public static final int left = 0;
 public static final int top = 1;
 public static final int center = 2;
 public static final int right = 3;
 public static final int bottom = 4;
 private int gravity = center;
 private rectf rectf; //用于定义的圆弧的形状和大小的界限

 public percentview(context context) {
  super(context);
  init();
 }

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

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

 private void init() {
  mpaint = new paint();
  mpaint.setantialias(true);
  rectf = new rectf();
 }

 private void initparams(context context, attributeset attrs) {
  mpaint = new paint();
  mpaint.setantialias(true);
  rectf = new rectf();
  typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.percentview);
  if (typedarray != null) {
   backgroundcolor = typedarray.getcolor(r.styleable.percentview_percent_background_color, color.gray);
   progresscolor = typedarray.getcolor(r.styleable.percentview_percent_progress_color, color.blue);
   radius = typedarray.getdimension(r.styleable.percentview_percent_circle_radius, 0);
   progress = typedarray.getint(r.styleable.percentview_percent_circle_progress, 0);
   gravity = typedarray.getint(r.styleable.percentview_percent_circle_gravity, center);
   typedarray.recycle();
  }
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  int widthmode = measurespec.getmode(widthmeasurespec);
  int widthsize = measurespec.getsize(widthmeasurespec);
  int heightmode = measurespec.getmode(heightmeasurespec);
  int heightsize = measurespec.getsize(heightmeasurespec);
  log.e(tag, "onmeasure--widthmode-->" + widthmode);
  switch (widthmode) {
   case measurespec.exactly://
    break;
   case measurespec.at_most:
    break;
   case measurespec.unspecified:
    break;
  }
  log.e(tag, "onmeasure--widthsize-->" + widthsize);
  log.e(tag, "onmeasure--heightmode-->" + heightmode);
  log.e(tag, "onmeasure--heightsize-->" + heightsize);
  int with = getwidth();
  int height = getheight();
  log.e(tag, "ondraw---->" + with + "*" + height);
  centerx = with / 2;
  centery = with / 2;
  switch (gravity) {
   case left:
    centerx = radius + getpaddingleft();
    break;
   case top:
    centery = radius + getpaddingtop();
    break;
   case center:
    break;
   case right:
    centerx = with - radius - getpaddingright();
    break;
   case bottom:
    centery = height - radius - getpaddingbottom();
    break;
  }
  float left = centerx - radius;
  float top = centery - radius;
  float right = centerx + radius;
  float bottom = centery + radius;
  rectf.set(left, top, right, bottom);
 }

 @override
 protected void onlayout(boolean changed, int left, int top, int right, int bottom) {
  super.onlayout(changed, left, top, right, bottom);
  log.e(tag, "onlayout");
 }

 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  mpaint.setcolor(backgroundcolor);
  // fill填充, stroke描边,fill_and_stroke填充和描边
  mpaint.setstyle(paint.style.fill_and_stroke);
  canvas.drawcircle(centerx, centery, radius, mpaint);
  mpaint.setcolor(progresscolor);

  double percent = progress * 1.0 / 100;
  int angle = (int) (percent * 360);
  canvas.drawarc(rectf, 270, angle, true, mpaint); //根据进度画圆弧
 }
}

运行结果:
 根据不同的配置显示的两种效果

 

小结: 
通过自定义属性可以达到自定义的控件也能像原生的控件一样实现可配置。但是在实际的项目开发中,像本文介绍的这种自定义控件使用频率并不是最高的,使用频率较高的是通过自定义一个组合控件的方式,来达到布局文件的复用,以减少项目维护成本以及开发成本,下篇文章将重点介绍如何自定义控件组合,。

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网