教你搞定Android自定义ViewGroup

上一篇我们介绍了Android中自定义View的知识,并实现了一个类似Google彩虹进度条的自定义View,今天我们将进一步学习如何去自定义一个ViewGroup。

ViewGroup

我们知道ViewGroup就是View的容器类,我们经常用的LinearLayout,RelativeLayout等都是ViewGroup的子类,因为ViewGroup有很多子View,所以它的整个绘制过程相对于View会复杂一点,但是还是三个步骤measure,layout,draw,我们一次说明。

  • Measure
    Measure过程还是测量ViewGroup的大小,如果layout_widht和layout_height是match_parent或具体的xxxdp,就很简答了,直接调用setMeasuredDimension()方法,设置ViewGroup的宽高即可,如果是wrap_content,就比较麻烦了,我们需要遍历所有的子View,然后对每个子View进行测量,然后根据子View的排列规则,计算出最终ViewGroup的大小。

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      int childCount = this.getChildCount();
      for (int i = 0; i < childCount; i++) {
          View child = this.getChildAt(i);
          this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
          int cw = child.getMeasuredWidth();
          // int ch = child.getMeasuredHeight();
      }
    }
    

你可能需要类似上面的代码,其中getChildCount()方法,返回子View的数量,measureChild()方法,调用子View的测量方法。

  • Layout
    上一篇中,我们稍微提到了,layout过程其实就是对子View的位置进行排列,onLayout方法给我一个机会,来按照我们想要的规则自定义子View排列。
    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    int childCount = this.getChildCount();
    for (int i = 0; i < childCount; i++) {
    View child = this.getChildAt(i);
    LayoutParams lParams = (LayoutParams) child.getLayoutParams();
    child.layout(lParams.left, lParams.top, lParams.left + childWidth,
    lParams.top + childHeight);
    }
    }
    你同样可能需要类似上面的代码,其中child.layout(left,top,right,bottom)方法可以对子View的位置进行设置,四个参数的意思大家通过变量名都应该清楚了。
  • Draw
    ViewGroup在draw阶段,其实就是按照子类的排列顺序,调用子类的onDraw方法,因为我们只是View的容器, 本身一般不需要draw额外的修饰,所以往往在onDraw方法里面,只需要调用ViewGroup的onDraw默认实现方法即可。

LayoutParams

ViewGroup还有一个很重要的知识LayoutParams,LayoutParams存储了子View在加入ViewGroup中时的一些参数信息,在继承ViewGroup类时,一般也需要新建一个新的LayoutParams类,就像SDK中我们熟悉的LinearLayout.LayoutParams,RelativeLayout.LayoutParams类等一样,那么可以这样做,在你定义的ViewGroup子类中,新建一个LayoutParams类继承与ViewGroup.LayoutParams。
public static class LayoutParams extends ViewGroup.LayoutParams {

    public int left = 0;
    public int top = 0;

    public LayoutParams(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public LayoutParams(int arg0, int arg1) {
        super(arg0, arg1);
    }

    public LayoutParams(android.view.ViewGroup.LayoutParams arg0) {
        super(arg0);
    }

  }

那么现在新的LayoutParams类已经有了,如何让我们自定义的ViewGroup使用我们自定义的LayoutParams类来添加子View呢,ViewGroup同样提供了下面这几个方法供我们重写,我们重写返回我们自定义的LayoutParams对象即可。
@Override
public android.view.ViewGroup.LayoutParams generateLayoutParams(
AttributeSet attrs) {
return new NinePhotoView.LayoutParams(getContext(), attrs);
}

  @Override
  protected android.view.ViewGroup.LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
  }

  @Override
  protected android.view.ViewGroup.LayoutParams generateLayoutParams(
        android.view.ViewGroup.LayoutParams p) {
    return new LayoutParams(p);
  }

  @Override
  protected boolean checkLayoutParams(android.view.ViewGroup.LayoutParams p) {
    return p instanceof NinePhotoView.LayoutParams;
  }

实例

我们还是做一个实例来说明,我们今天做一个类似微信朋友圈 存储要发送图片的控件,点击+号图片,可以一直加图片,最多9张。那么微信是4个一排,我们这里是3个一排,因为一般常规都是三个一排,这些都是细节不要在意(另外偷偷告诉大家,微信的实现是用TableLayout,-.-)。


微信朋友圈发送图片
  public class NinePhotoView extends ViewGroup {

  public static final int MAX_PHOTO_NUMBER = 9;

  private int[] constImageIds = { R.drawable.girl_0, R.drawable.girl_1,
        R.drawable.girl_2, R.drawable.girl_3, R.drawable.girl_4,
        R.drawable.girl_5, R.drawable.girl_6, R.drawable.girl_7,
        R.drawable.girl_8 };

  // horizontal space among children views
  int hSpace = Utils.dpToPx(10, getResources());
  // vertical space among children views
  int vSpace = Utils.dpToPx(10, getResources());

  // every child view width and height.
  int childWidth = 0;
  int childHeight = 0;

  // store images res id
  ArrayList<integer> mImageResArrayList = new ArrayList<integer>(9);
  private View addPhotoView;

  public NinePhotoView(Context context) {
    super(context);
  }

  public NinePhotoView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public NinePhotoView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray t = context.obtainStyledAttributes(attrs,
            R.styleable.NinePhotoView, 0, 0);
    hSpace = t.getDimensionPixelSize(
            R.styleable.NinePhotoView_ninephoto_hspace, hSpace);
    vSpace = t.getDimensionPixelSize(
            R.styleable.NinePhotoView_ninephoto_vspace, vSpace);
    t.recycle();
    
    addPhotoView = new View(context);
    addView(addPhotoView);
    mImageResArrayList.add(new integer());
  }

目前为止,都跟上一篇说的大致差不多,另外拍照和从相册选择图片不是我们这一篇的重点,所以我们把图片硬编码到代码中(全是美女...),ViewGroup初始化时我们添加了一个+号按钮,给用户点击添加新的图片。

  • Measure
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int rw = MeasureSpec.getSize(widthMeasureSpec);
    int rh = MeasureSpec.getSize(heightMeasureSpec);

      childWidth = (rw - 2 * hSpace) / 3;
      childHeight = childWidth;
    
      int childCount = this.getChildCount();
      for (int i = 0; i < childCount; i++) {
          View child = this.getChildAt(i);
          //this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
    
          LayoutParams lParams = (LayoutParams) child.getLayoutParams();
          lParams.left = (i % 3) * (childWidth + hSpace);
          lParams.top = (i / 3) * (childWidth + vSpace);
      }
    
      int vw = rw;
      int vh = rh;
      if (childCount < 3) {
          vw = childCount * (childWidth + hSpace);
      }
      vh = ((childCount + 3) / 3) * (childWidth + vSpace);
      setMeasuredDimension(vw, vh);
    }
    

我们的子View三个一排,而且都是正方形,所以我们上面通过循环很好去得到所有子View的位置,注意我们上面把子View的左上角坐标存储到我们自定义的LayoutParams 的left和top二个字段中,Layout阶段会使用,最后我们算得整个ViewGroup的宽高,调用setMeasuredDimension设置。

  • Layout
    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    int childCount = this.getChildCount();
    for (int i = 0; i < childCount; i++) {
    View child = this.getChildAt(i);
    LayoutParams lParams = (LayoutParams) child.getLayoutParams();
    child.layout(lParams.left, lParams.top, lParams.left + childWidth,
    lParams.top + childHeight);

          if (i == mImageResArrayList.size() - 1 && mImageResArrayList.size() != MAX_PHOTO_NUMBER) {
              child.setBackgroundResource(R.drawable.add_photo);
              child.setOnClickListener(new View.OnClickListener() {
                  
                  @Override
                  public void onClick(View arg0) {
                      addPhotoBtnClick();
                  }
              });
          }else {
              child.setBackgroundResource(constImageIds[i]);
              child.setOnClickListener(null);
          }
      }
    }
    
    public void addPhoto() {
      if (mImageResArrayList.size() < MAX_PHOTO_NUMBER) {
          View newChild = new View(getContext());
          addView(newChild);
          mImageResArrayList.add(new integer());
          requestLayout();
          invalidate();
      }
    }
    
    public void addPhotoBtnClick() {
      final CharSequence[] items = { "Take Photo", "Photo from gallery" };
    
      AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
      builder.setItems(items, new DialogInterface.OnClickListener() {
    
          @Override
          public void onClick(DialogInterface arg0, int arg1) {
              addPhoto();
          }
    
      });
      builder.show();
    }
    

最核心的就是调用layout方法,根据我们measure阶段获得的LayoutParams中的left和top字段,也很好对每个子View进行位置排列。然后判断在图片未达到最大值9张时,默认最后一张是+号图片,然后设置点击事件,弹出对话框供用户选择操作。

  • Draw
    不需要重写,使用ViewGroup默认实现即可。

附上布局文件

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

<com.sw.demo.widget.NinePhotoView
    android:id="@+id/photoview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:ninephoto_hspace="10dp"
    app:ninephoto_vspace="10dp"
    app:rainbowbar_color="@android:color/holo_blue_bright" >

</com.sw.demo.widget.NinePhotoView>

</LinearLayout>

最后还是加上程序运行的效果图,今天自定义ViewGroup的讲解就这么多了,祝大家每天都有新收获,每天都有好心情~~~


NiewPhotoView.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容