Android 自定义ViewGroup显示不同布局的图片

正文

今天我们来自定义一个ViewGroup,让它可以根据图片的数量显示不同的布局

我们在微信逛朋友圈的时候会看到有图片的朋友圈,当只有一张图片的时候,显示是铺满所在区域的,当有9张的时候,是九宫格的形式显示的,那么我们能不能根据图片的数量显示不同的布局呢,比如两张的时候我想让其均分,三张的时候我想一张大图在左,另外两张小图在右,还有其他数量也可以自己想象

你有没有想到用什么方法实现这个需求呢,你可能会想到用不同的布局文件,只要你不嫌麻烦,因为你的布局数量和图片数量是成正比的,所以布局是比较浪费资源的,那有什么好方法吗,当然有,就是我们今天要讲的自定义ViewGroup

正式开始

我们知道ViewGroup是控件的组合,它可以添加View到其内部,我们可以新建一个类并继承ViewGroup

public class VideoLayout extends ViewGroup {

    public VideoLayout(Context context) {
        this(context, null);
    }

    public VideoLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
    }
}

我们重写一个ViewGroup来满足今天的需求其实需要重写onMeasure和onLayout方法,还有一个onDraw方法不用管

我们创建好之后就可以开始编写代码了,下面是完整代码
PostContentLayout.java

package com.yk.mchat.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class PostContentLayout extends ViewGroup {

    public static final int PIC_ONE = 1;

    public static final int PIC_TWO = 2;

    public static final int PIC_THREE = 3;

    public static final int PIC_FOUR = 4;

    public static final int PIC_FIVE = 5;

    public static final int PIC_SIX = 6;

    public static final int PIC_SEVEN = 7;

    public static final int PIC_EIGHT = 8;

    public static final int PIC_NINE = 9;

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

    public PostContentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        if (childCount == 0) {
            return;
        }
        switch (childCount) {
            case PIC_ONE:
                onMeasureOne(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_TWO:
                onMeasureTwo(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_THREE:
                onMeasureThree(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_FOUR:
                onMeasureFour(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_FIVE:
                onMeasureFive(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_SIX:
                onMeasureSix(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_SEVEN:
                onMeasureSeven(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_EIGHT:
                onMeasureEight(widthMeasureSpec, heightMeasureSpec);
                break;
            case PIC_NINE:
                onMeasureNice(widthMeasureSpec, heightMeasureSpec);
                break;
            default:
                break;
        }
    }

    private void onMeasureOne(int widthMeasureSpec, int heightMeasureSpec) {
        View child = getChildAt(0);
        child.measure(widthMeasureSpec, heightMeasureSpec);
    }

    private void onMeasureTwo(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
            child.measure(childWidth, heightMeasureSpec);
        }
    }

    private void onMeasureThree(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        int mainChildWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
        mainChild.measure(mainChildWidth, heightMeasureSpec);

        for (int i = 1; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 2, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    private void onMeasureFour(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 2, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    private void onMeasureFive(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 2, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }

        for (int i = 2; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 3, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 2, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    private void onMeasureSix(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        int mainChildWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 3 * 2, MeasureSpec.EXACTLY);
        int mainChildHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 3 * 2, MeasureSpec.EXACTLY);
        mainChild.measure(mainChildWidth, mainChildHeight);

        for (int i = 1; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 3, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 3, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    private void onMeasureSeven(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        int mainChildWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
        int mainChildHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 2, MeasureSpec.EXACTLY);
        mainChild.measure(mainChildWidth, mainChildHeight);

        for (int i = 1; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 2, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 4, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    private void onMeasureEight(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        int mainChildWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 4 * 3, MeasureSpec.EXACTLY);
        int mainChildHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 4 * 3, MeasureSpec.EXACTLY);
        mainChild.measure(mainChildWidth, mainChildHeight);

        for (int i = 1; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 4, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 4, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    private void onMeasureNice(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            int childWidth = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(widthMeasureSpec) / 3, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec) / 3, MeasureSpec.EXACTLY);
            child.measure(childWidth, childHeight);
        }
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int childCount = getChildCount();

        if (childCount == 0) {
            return;
        }
        switch (childCount) {
            case PIC_ONE:
                onLayoutOne();
                break;
            case PIC_TWO:
                onLayoutTwo();
                break;
            case PIC_THREE:
                onLayoutThree();
                break;
            case PIC_FOUR:
                onLayoutFour();
                break;
            case PIC_FIVE:
                onLayoutFive();
                break;
            case PIC_SIX:
                onLayoutSix();
                break;
            case PIC_SEVEN:
                onLayoutSeven();
                break;
            case PIC_EIGHT:
                onLayoutEight();
                break;
            case PIC_NINE:
                onLayoutNine();
                break;
            default:
                break;
        }
    }

    private void onLayoutOne() {
        View child = getChildAt(0);
        child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
    }

    private void onLayoutTwo() {
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * i, 0, child.getMeasuredWidth() * (i + 1), child.getMeasuredHeight());
        }
    }

    private void onLayoutThree() {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        mainChild.layout(0, 0, mainChild.getMeasuredWidth(), mainChild.getMeasuredHeight());

        for (int i = 1; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth(), child.getMeasuredHeight() * (i - 1), child.getMeasuredWidth() * 2, child.getMeasuredHeight() * i);
        }
    }

    private void onLayoutFour() {
        int childCount = getChildCount();

        for (int i = 0; i < 2; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * i, 0, child.getMeasuredWidth() * (i + 1), child.getMeasuredHeight());
        }

        for (int i = 2; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 2), child.getMeasuredHeight(), child.getMeasuredWidth() * (i - 1), child.getMeasuredHeight() * 2);
        }
    }

    private void onLayoutFive() {
        int childCount = getChildCount();

        for (int i = 0; i < 2; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * i, 0, child.getMeasuredWidth() * (i + 1), child.getMeasuredHeight());
        }

        for (int i = 2; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 2), child.getMeasuredHeight(), child.getMeasuredWidth() * (i - 1), child.getMeasuredHeight() * 2);
        }
    }

    private void onLayoutSix() {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        mainChild.layout(0, 0, mainChild.getMeasuredWidth(), mainChild.getMeasuredHeight());

        for (int i = 1; i < 3; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * 2, child.getMeasuredHeight() * (i - 1), child.getMeasuredWidth() * 3, child.getMeasuredHeight() * i);
        }

        for (int i = 3; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 3), child.getMeasuredHeight() * 2, child.getMeasuredWidth() * (i - 2), child.getMeasuredHeight() * 3);
        }
    }

    private void onLayoutSeven() {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        mainChild.layout(0, 0, mainChild.getMeasuredWidth(), mainChild.getMeasuredHeight());

        for (int i = 1; i < 3; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth(), child.getMeasuredHeight() * (i - 1), child.getMeasuredWidth() * 2, child.getMeasuredHeight() * i);
        }

        for (int i = 3; i < 5; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 3), child.getMeasuredHeight() * 2, child.getMeasuredWidth() * (i - 2), child.getMeasuredHeight() * 3);
        }

        for (int i = 5; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 5), child.getMeasuredHeight() * 3, child.getMeasuredWidth() * (i - 4), child.getMeasuredHeight() * 4);
        }
    }

    private void onLayoutEight() {
        int childCount = getChildCount();

        View mainChild = getChildAt(0);
        mainChild.layout(0, 0, mainChild.getMeasuredWidth(), mainChild.getMeasuredHeight());

        for (int i = 1; i < 4; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * 3, child.getMeasuredHeight() * (i - 1), child.getMeasuredWidth() * 4, child.getMeasuredHeight() * i);
        }

        for (int i = 4; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 4), child.getMeasuredHeight() * 3, child.getMeasuredWidth() * (i - 3), child.getMeasuredHeight() * 4);
        }
    }

    private void onLayoutNine() {
        int childCount = getChildCount();

        for (int i = 0; i < 3; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * i, 0, child.getMeasuredWidth() * (i + 1), child.getMeasuredHeight());
        }

        for (int i = 3; i < 6; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 3), child.getMeasuredHeight(), child.getMeasuredWidth() * (i - 2), child.getMeasuredHeight() * 2);
        }

        for (int i = 6; i < 9; i++) {
            View child = getChildAt(i);
            child.layout(child.getMeasuredWidth() * (i - 6), child.getMeasuredHeight() * 2, child.getMeasuredWidth() * (i - 5), child.getMeasuredHeight() * 3);
        }
    }
}

里面的方法都写得比较清楚,你可以复制下来,然后试着往这个ViewGroup中addView

今天就到这里。

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

推荐阅读更多精彩内容