小編給大家分享一下Android如何實(shí)現(xiàn)朋友圈多圖顯示功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
公司主營(yíng)業(yè)務(wù):網(wǎng)站建設(shè)、成都做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出銅仁免費(fèi)做網(wǎng)站回饋大家。
具體內(nèi)容如下
Android實(shí)現(xiàn)朋友圈評(píng)論回復(fù)列表
Android實(shí)現(xiàn)朋友圈點(diǎn)贊列表
Android實(shí)現(xiàn)朋友圈多圖顯示功能
正文
先看一下效果圖:
MultiImageView:
public class MultiImageView extends LinearLayout { public static int MAX_WIDTH = 0; // 照片的Url列表 private ListimagesList; /** * 長(zhǎng)度 單位為Pixel * */ private int pxOneMaxWandH; // 單張圖最大允許寬高 private int pxMoreWandH = 0;// 多張圖的寬高 private int pxImagePadding = LvDPUtil.dip2px(3);// 圖片間的間距 private int MAX_PER_ROW_COUNT = 3;// 每行顯示最大數(shù) private LayoutParams onePicPara; private LayoutParams morePara, moreParaColumnFirst; private LayoutParams rowPara; private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } public MultiImageView(Context context) { super(context); } public MultiImageView(Context context, AttributeSet attrs) { super(context, attrs); } public void setList(List lists) throws IllegalArgumentException { if (lists == null) { throw new IllegalArgumentException("imageList is null..."); } imagesList = lists; if (MAX_WIDTH > 0) { // 如果需要兩張和四張圖橫向鋪滿,這里去掉注釋即可。 // if (lists.size() == 2 || lists.size() == 4) { // pxMoreWandH = (MAX_WIDTH - pxImagePadding) / 2; // } else { pxMoreWandH = (MAX_WIDTH - pxImagePadding * 2) / 3; //解決右側(cè)圖片和內(nèi)容對(duì)不齊問題 // } pxOneMaxWandH = MAX_WIDTH * 2 / 3; // 一張圖的時(shí)候,圖片寬度 initImageLayoutParams(); } initView(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (MAX_WIDTH == 0) { int width = measureWidth(widthMeasureSpec); if (width > 0) { MAX_WIDTH = width - getPaddingLeft() - getPaddingRight(); if (imagesList != null && imagesList.size() > 0) { setList(imagesList); } } } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * Determines the width of this view * * @param measureSpec A measureSpec packed into an int * @return The width of the view, honoring constraints from measureSpec */ 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; } private void initImageLayoutParams() { int wrap = LayoutParams.WRAP_CONTENT; int match = LayoutParams.MATCH_PARENT; onePicPara = new LayoutParams(pxOneMaxWandH, wrap); moreParaColumnFirst = new LayoutParams(pxMoreWandH, pxMoreWandH); morePara = new LayoutParams(pxMoreWandH, pxMoreWandH); morePara.setMargins(pxImagePadding, 0, 0, 0); rowPara = new LayoutParams(match, wrap); } // 根據(jù)imageView的數(shù)量初始化不同的View布局,還要為每一個(gè)View作點(diǎn)擊效果 private void initView() { this.setOrientation(VERTICAL); this.removeAllViews(); if (MAX_WIDTH == 0) { //為了觸發(fā)onMeasure()來測(cè)量MultiImageView的最大寬度,MultiImageView的寬設(shè)置為match_parent addView(new View(getContext())); return; } if (imagesList == null || imagesList.size() == 0) { return; } if (imagesList.size() == 1) { addView(createImageView(0, false)); } else { int allCount = imagesList.size(); if (allCount == 4) { MAX_PER_ROW_COUNT = 2; } else { MAX_PER_ROW_COUNT = 3; } int rowCount = allCount / MAX_PER_ROW_COUNT + (allCount % MAX_PER_ROW_COUNT > 0 ? 1 : 0);// 行數(shù) for (int rowCursor = 0; rowCursor < rowCount; rowCursor++) { LinearLayout rowLayout = new LinearLayout(getContext()); rowLayout.setOrientation(LinearLayout.HORIZONTAL); rowLayout.setLayoutParams(rowPara); if (rowCursor != 0) { rowLayout.setPadding(0, pxImagePadding, 0, 0); } int columnCount = allCount % MAX_PER_ROW_COUNT == 0 ? MAX_PER_ROW_COUNT : allCount % MAX_PER_ROW_COUNT;//每行的列數(shù) if (rowCursor != rowCount - 1) { columnCount = MAX_PER_ROW_COUNT; } addView(rowLayout); int rowOffset = rowCursor * MAX_PER_ROW_COUNT;// 行偏移 for (int columnCursor = 0; columnCursor < columnCount; columnCursor++) { int position = columnCursor + rowOffset; rowLayout.addView(createImageView(position, true)); } } } } private ImageView createImageView(final int position, final boolean isMultiImage) { String url = ""; if (!TextUtils.isEmpty(imagesList.get(position))) { url = imagesList.get(position); } ImageView imageView = new ColorFilterImageView(getContext()); if (isMultiImage) { imageView.setScaleType(ScaleType.CENTER_CROP); imageView.setLayoutParams(position % MAX_PER_ROW_COUNT == 0 ? moreParaColumnFirst : morePara); } else { imageView.setAdjustViewBounds(true); imageView.setScaleType(ScaleType.FIT_START); imageView.setMaxHeight(pxOneMaxWandH); imageView.setLayoutParams(onePicPara); } imageView.setId(url.hashCode()); imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener != null) { mOnItemClickListener.onItemClick(v, position); } } }); // 加載網(wǎng)絡(luò)圖片/設(shè)置圖片顯示 Glide.with(getContext()).load(url).into(imageView); return imageView; } public interface OnItemClickListener { void onItemClick(View view, int position); } }
點(diǎn)擊有陰影的 ImageView :
public class ColorFilterImageView extends ImageView implements OnTouchListener { public ColorFilterImageView(Context context) { this(context, null, 0); } public ColorFilterImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ColorFilterImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 按下時(shí)圖像變灰 setColorFilter(Color.GRAY, Mode.MULTIPLY); break; case MotionEvent.ACTION_UP: // 手指離開或取消操作時(shí)恢復(fù)原色 case MotionEvent.ACTION_CANCEL: setColorFilter(Color.TRANSPARENT); break; default: break; } return false; } }
用法
MultiImageView multiImageView = findViewById(R.id.multi_image); multiImageView.setList(imgs); // List 類型的圖片地址列表 multiImage.setOnItemClickListener(new MultiImageView.OnItemClickListener() { @Override public void onItemClick(View view, int position) { // To do something or 查看大圖. } });
附:如果需要完整朋友圈項(xiàng)目的話,這里推薦一個(gè) Github 項(xiàng)目仿微信實(shí)現(xiàn)的朋友圈
以上是“Android如何實(shí)現(xiàn)朋友圈多圖顯示功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!