Android 自定義ViewGroup顯示不同布局的圖片

正文

今天我們來自定義一個ViewGroup砰碴,讓它可以根據(jù)圖片的數(shù)量顯示不同的布局

我們在微信逛朋友圈的時候會看到有圖片的朋友圈垒在,當(dāng)只有一張圖片的時候镰吵,顯示是鋪滿所在區(qū)域的,當(dāng)有9張的時候旺入,是九宮格的形式顯示的兑凿,那么我們能不能根據(jù)圖片的數(shù)量顯示不同的布局呢凯力,比如兩張的時候我想讓其均分,三張的時候我想一張大圖在左礼华,另外兩張小圖在右咐鹤,還有其他數(shù)量也可以自己想象

你有沒有想到用什么方法實現(xiàn)這個需求呢,你可能會想到用不同的布局文件圣絮,只要你不嫌麻煩祈惶,因為你的布局?jǐn)?shù)量和圖片數(shù)量是成正比的,所以布局是比較浪費資源的扮匠,那有什么好方法嗎捧请,當(dāng)然有,就是我們今天要講的自定義ViewGroup

正式開始

我們知道ViewGroup是控件的組合棒搜,它可以添加View到其內(nèi)部疹蛉,我們可以新建一個類并繼承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方法不用管

我們創(chuàng)建好之后就可以開始編寫代碼了力麸,下面是完整代碼
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);
        }
    }
}

里面的方法都寫得比較清楚可款,你可以復(fù)制下來,然后試著往這個ViewGroup中addView

今天就到這里克蚂。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末筑舅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子陨舱,更是在濱河造成了極大的恐慌,老刑警劉巖版仔,帶你破解...
    沈念sama閱讀 206,214評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件游盲,死亡現(xiàn)場離奇詭異,居然都是意外死亡蛮粮,警方通過查閱死者的電腦和手機(jī)益缎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來然想,“玉大人莺奔,你說我怎么就攤上這事”湫梗” “怎么了令哟?”我有些...
    開封第一講書人閱讀 152,543評論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長妨蛹。 經(jīng)常有香客問我屏富,道長,這世上最難降的妖魔是什么蛙卤? 我笑而不...
    開封第一講書人閱讀 55,221評論 1 279
  • 正文 為了忘掉前任狠半,我火速辦了婚禮噩死,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘神年。我一直安慰自己已维,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,224評論 5 371
  • 文/花漫 我一把揭開白布已日。 她就那樣靜靜地躺著垛耳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪捂敌。 梳的紋絲不亂的頭發(fā)上艾扮,一...
    開封第一講書人閱讀 49,007評論 1 284
  • 那天,我揣著相機(jī)與錄音占婉,去河邊找鬼泡嘴。 笑死,一個胖子當(dāng)著我的面吹牛逆济,可吹牛的內(nèi)容都是我干的酌予。 我是一名探鬼主播,決...
    沈念sama閱讀 38,313評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼奖慌,長吁一口氣:“原來是場噩夢啊……” “哼抛虫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起简僧,我...
    開封第一講書人閱讀 36,956評論 0 259
  • 序言:老撾萬榮一對情侶失蹤建椰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后岛马,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棉姐,經(jīng)...
    沈念sama閱讀 43,441評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,925評論 2 323
  • 正文 我和宋清朗相戀三年啦逆,在試婚紗的時候發(fā)現(xiàn)自己被綠了伞矩。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,018評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡夏志,死狀恐怖乃坤,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情沟蔑,我是刑警寧澤湿诊,帶...
    沈念sama閱讀 33,685評論 4 322
  • 正文 年R本政府宣布惦银,位于F島的核電站萍恕,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏直奋。R本人自食惡果不足惜宇色,卻給世界環(huán)境...
    茶點故事閱讀 39,234評論 3 307
  • 文/蒙蒙 一九杂、第九天 我趴在偏房一處隱蔽的房頂上張望颁湖。 院中可真熱鬧,春花似錦例隆、人聲如沸甥捺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽镰禾。三九已至,卻和暖如春唱逢,著一層夾襖步出監(jiān)牢的瞬間吴侦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評論 1 261
  • 我被黑心中介騙來泰國打工坞古, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留备韧,地道東北人。 一個月前我還...
    沈念sama閱讀 45,467評論 2 352
  • 正文 我出身青樓痪枫,卻偏偏與公主長得像织堂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子奶陈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,762評論 2 345

推薦閱讀更多精彩內(nèi)容