Android 高仿微信朋友圈圖片合并顯示

因為圖片顯示的寬高是固定的 所以測量那沒有做判斷 用 MeasureSpec.EXACTLY

自己寫的自定義view 比較一般 如果各位有其他好的想法 可以一起討論下 不足的地方歡迎大家指正吼蚁。

public class MergePictureView extends LinearLayout {
public static int MAX_WIDTH = 0; 

private List<String> imagesList = new ArrayList<>();
private int pxImagePadding = DiPUtils.dip2px(getContext(), 1);// 圖片間的間距

private LayoutParams onePicPara; //一張圖的寬高
private LayoutParams twoPicPara; //四張圖的寬高
private LayoutParams threePicPare;// 四張圖的寬 一張圖的高
private LayoutParams fourPicPare;//一張圖的寬 四張圖的高

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

public MergePictureView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public MergePictureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


public void setImagesList(List<String> imagesList) {
    this.imagesList = imagesList;
    initImageLayoutParams();
    initView();
}

private void initImageLayoutParams() {
    onePicPara = new LayoutParams(MAX_WIDTH, MAX_WIDTH);

    twoPicPara = new LayoutParams(MAX_WIDTH / 2, MAX_WIDTH / 2);

    threePicPare = new LayoutParams(MAX_WIDTH / 2, MAX_WIDTH);

    fourPicPare = new LayoutParams(MAX_WIDTH, MAX_WIDTH / 2);


}

private void initView() {
    this.setOrientation(HORIZONTAL);
    this.removeAllViews();
    ImageView imageViewOne;
    ImageView imageViewTwo;
    ImageView imageViewThree;
    ImageView imageViewFour;
    if (imagesList.size() == 1) {
        imageViewOne = new ImageView(getContext());
        imageViewOne.setLayoutParams(onePicPara);
        imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
        addView(imageViewOne);
    } else if (imagesList.size() == 2) {
        imageViewOne = new ImageView(getContext());
        imageViewOne.setLayoutParams(threePicPare);
        imageViewOne.setPadding(0, 0, pxImagePadding, 0);
        imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
        addView(imageViewOne);
        imageViewTwo = new ImageView(getContext());
        imageViewTwo.setLayoutParams(threePicPare);
        imageViewTwo.setPadding(pxImagePadding, 0, 0, 0);
        imageViewTwo.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(1)).into(imageViewTwo);
        addView(imageViewTwo);
    } else if (imagesList.size() == 3) {
        imageViewOne = new ImageView(getContext());
        imageViewOne.setLayoutParams(threePicPare);
        imageViewOne.setPadding(0, 0, pxImagePadding, 0);
        imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
        addView(imageViewOne);

        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(VERTICAL);
        linearLayout.setLayoutParams(threePicPare);
        linearLayout.setPadding(pxImagePadding, 0, 0, 0);

        imageViewTwo = new ImageView(getContext());
        imageViewTwo.setLayoutParams(twoPicPara);
        imageViewTwo.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageViewTwo.setPadding(0, 0, 0, pxImagePadding);
        Glide.with(getContext()).load(imagesList.get(1)).into(imageViewTwo);
        linearLayout.addView(imageViewTwo);

        imageViewThree = new ImageView(getContext());
        imageViewThree.setLayoutParams(twoPicPara);
        imageViewThree.setPadding(0, pxImagePadding, 0, 0);
        imageViewThree.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(2)).into(imageViewThree);
        linearLayout.addView(imageViewThree);

        addView(linearLayout);

    } else if (imagesList.size() > 3) {
        this.setOrientation(VERTICAL);
        LinearLayout top = new LinearLayout(getContext());
        top.setOrientation(HORIZONTAL);
        top.setPadding(0, 0, 0, pxImagePadding);
        top.setLayoutParams(fourPicPare);

        imageViewOne = new ImageView(getContext());
        imageViewOne.setLayoutParams(twoPicPara);
        imageViewOne.setPadding(0, 0, pxImagePadding, 0);
        imageViewOne.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(0)).into(imageViewOne);
        top.addView(imageViewOne);

        imageViewTwo = new ImageView(getContext());
        imageViewTwo.setLayoutParams(twoPicPara);
        imageViewTwo.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageViewTwo.setPadding(pxImagePadding, 0, 0, 0);
        Glide.with(getContext()).load(imagesList.get(1)).into(imageViewTwo);
        top.addView(imageViewTwo);

        addView(top);

        LinearLayout bottom = new LinearLayout(getContext());
        bottom.setOrientation(HORIZONTAL);
        bottom.setLayoutParams(fourPicPare);
        bottom.setPadding(0, pxImagePadding, 0, 0);

        imageViewThree = new ImageView(getContext());
        imageViewThree.setLayoutParams(twoPicPara);
        imageViewThree.setPadding(0, 0, pxImagePadding, 0);
        imageViewThree.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(2)).into(imageViewThree);
        bottom.addView(imageViewThree);

        imageViewFour = new ImageView(getContext());
        imageViewFour.setLayoutParams(twoPicPara);
        imageViewFour.setPadding(pxImagePadding, 0, 0, 0);
        imageViewFour.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(imagesList.get(3)).into(imageViewFour);
        bottom.addView(imageViewFour);

        addView(bottom);
    }

}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  if(MAX_WIDTH==0){
        MAX_WIDTH = measureWidth(widthMeasureSpec);
        if (imagesList != null && imagesList.size() > 0) {
              setImagesList(imagesList);
          }
    }
  
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

private int measureWidth(int measureSpec) {
    int result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        // We were told how big to be
        result = specSize;
    } else {
        // Measure the text
        // result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
        // + getPaddingRight();
        if (specMode == MeasureSpec.AT_MOST) {
            // Respect AT_MOST value if that was what is called for by
            // measureSpec
            result = Math.min(result, specSize);
        }
    }
    return result;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蜂奸,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辙纬,死亡現場離奇詭異,居然都是意外死亡叭喜,警方通過查閱死者的電腦和手機贺拣,發(fā)現死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來捂蕴,“玉大人譬涡,你說我怎么就攤上這事∩侗妫” “怎么了涡匀?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長溉知。 經常有香客問我陨瘩,道長,這世上最難降的妖魔是什么级乍? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任拾酝,我火速辦了婚禮,結果婚禮上卡者,老公的妹妹穿的比我還像新娘蒿囤。我一直安慰自己,他們只是感情好崇决,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布材诽。 她就那樣靜靜地躺著,像睡著了一般恒傻。 火紅的嫁衣襯著肌膚如雪脸侥。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天盈厘,我揣著相機與錄音睁枕,去河邊找鬼。 笑死沸手,一個胖子當著我的面吹牛外遇,可吹牛的內容都是我干的。 我是一名探鬼主播契吉,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼跳仿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了捐晶?” 一聲冷哼從身側響起菲语,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤妄辩,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后山上,有當地人在樹林里發(fā)現了一具尸體眼耀,經...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年佩憾,在試婚紗的時候發(fā)現自己被綠了哮伟。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡鸯屿,死狀恐怖澈吨,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情寄摆,我是刑警寧澤谅辣,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站婶恼,受9級特大地震影響桑阶,放射性物質發(fā)生泄漏。R本人自食惡果不足惜勾邦,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一蚣录、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧眷篇,春花似錦萎河、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至昧港,卻和暖如春擎椰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背创肥。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工达舒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叹侄。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓巩搏,卻偏偏與公主長得像,于是被迫代替她去往敵國和親圈膏。 傳聞我的和親對象是個殘疾皇子塔猾,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內容