您好,登錄后才能下訂單哦!
小編給大家分享一下Android如何實(shí)現(xiàn)朋友圈多圖顯示功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體內(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 List<String> imagesList; /** * 長(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<String> 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ì)不齊問(wèn)題 // } 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()來(lái)測(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: // 手指離開(kāi)或取消操作時(shí)恢復(fù)原色 case MotionEvent.ACTION_CANCEL: setColorFilter(Color.TRANSPARENT); break; default: break; } return false; } }
用法
<com.lvfq.myworkingtest.dynamic.view.MultiImageView android:id="@+id/multi_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/dp_10" /> MultiImageView multiImageView = findViewById(R.id.multi_image); multiImageView.setList(imgs); // List<String> 類(lèi)型的圖片地址列表 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)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。