最近任務(wù)不是很多听怕,于是自己就瞎研究了捧挺,很久之前做過關(guān)于類似朋友圈圖片的顯示,然后沒想到在回來看的時候尿瞭,各種框架啊松忍,自定義就出來了,于是我就看了一下人家寫的筷厘,稍稍改造了一下鸣峭。然后做了一名合格的搬運工。參考的人的博客是:http://www.reibang.com/p/63fbd4dba956
當前還有三方的酥艳,在這里:https://blog.csdn.net/nihaozhanghua/article/details/78753341大家自己去看吧啊哈哈摊溶。。
效果圖:
//自定義ViewGroup
public class WeChatFriendsCircleImageLayoutextends ViewGroup {
/*** 顯示的行數(shù)*/
? ? private int mColumnCount;
? ? /*** 默認間距*/
? ? private final float DEFAULT_SPACING =2.5f;
? ? private float mSpacing;
? ? /*** 圖片寬高比(黨為多張圖片的時候為1)
* 一般在url中會有寬高 可計算*/
? ? private float mItemAspectRatio;
? ? /*** 最寬的時候相對可用空間比例(當childCount=1的時候)
* 當只有一張圖片的 最大顯示寬度和高度相對于可用寬的的比例*/
? ? private final float MAX_WIDTH_PERCENTAGE =270f /350;
? ? private int mItemWidth;
? ? private int mItemHeight;
? ? public WeChatFriendsCircleImageLayout(Context context) {
this(context, null);
? ? }
public WeChatFriendsCircleImageLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
? ? }
public WeChatFriendsCircleImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
? ? ? ? mSpacing = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_SPACING,
? ? ? ? ? ? ? ? context.getResources().getDisplayMetrics());
? ? }
@Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
? ? ? ? final int width = MeasureSpec.getSize(widthMeasureSpec);
? ? ? ? //當只有一張圖片的時候 顯示一張相對比較大的圖片
//當大于1小于等于4張圖片的時候 一排顯示兩張圖片
//當只有一張圖片的時候
? ? ? ? if (count ==1) {
mColumnCount =1;
? ? ? ? ? ? int mItemMaxWidth = (int) (width *MAX_WIDTH_PERCENTAGE);
? ? ? ? ? ? int mItemMaxHeight = mItemMaxWidth;
? ? ? ? ? ? if (mItemAspectRatio <1) {
mItemHeight = mItemMaxHeight;
? ? ? ? ? ? ? ? mItemWidth = (int) (mItemHeight *mItemAspectRatio);
? ? ? ? ? ? }else {
mItemWidth = mItemMaxWidth;
? ? ? ? ? ? ? ? mItemHeight = (int) (mItemMaxWidth /mItemAspectRatio);
? ? ? ? ? ? }
}else {
if (count <=3) {
mColumnCount =3;
? ? ? ? ? ? }else if (count ==4) {
mColumnCount =2;
? ? ? ? ? ? }else {
mColumnCount =3;
? ? ? ? ? ? }
mItemWidth = (int) ((width - getPaddingLeft() - getPaddingRight() -2 *mSpacing) /3);
? ? ? ? ? ? mItemHeight = (int) (mItemWidth /mItemAspectRatio);
? ? ? ? }
for (int i =0; i < getChildCount(); i++) {
View view = getChildAt(i);
? ? ? ? ? ? LayoutParams layoutParams = view.getLayoutParams();
? ? ? ? ? ? layoutParams.width =mItemWidth;
? ? ? ? ? ? layoutParams.height =mItemHeight;
? ? ? ? ? ? measureChild(view, widthMeasureSpec, heightMeasureSpec);
? ? ? ? }
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
? ? ? ? if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(
getDesiredHeight(mItemHeight), MeasureSpec.EXACTLY);
? ? ? ? }
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
? ? ? ? if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
super.onMeasure(MeasureSpec.makeMeasureSpec(
getDesiredWidth(mItemWidth), MeasureSpec.EXACTLY), heightMeasureSpec);
? ? ? ? }else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? }
}
@Override
? ? protected void measureChild(View child, int parentWidthMeasureSpec,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
? ? ? ? //獲取子控件的寬高約束規(guī)則
? ? ? ? final int childWidthMeasureSpec =getChildMeasureSpec(parentWidthMeasureSpec,
? ? ? ? ? ? ? ? getPaddingLeft() + getPaddingRight(), lp.width);
? ? ? ? final int childHeightMeasureSpec =getChildMeasureSpec(parentHeightMeasureSpec,
? ? ? ? ? ? ? ? getPaddingLeft() + getPaddingRight(), lp.height);
? ? ? ? child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
? ? }
private int getDesiredHeight(int mItemHeight) {
int totalHeight = getPaddingTop() + getPaddingBottom();
? ? ? ? int count = getChildCount();
? ? ? ? if (count >0) {
int row = (count -1) /mColumnCount;
? ? ? ? ? ? totalHeight = (int) ((row +1) * mItemHeight + (row) *mSpacing) + totalHeight;
? ? ? ? }
return totalHeight;
? ? }
private int getDesiredWidth(int mItemWidth) {
int totalWidth = getPaddingLeft() + getPaddingRight();
? ? ? ? int count = getChildCount();
? ? ? ? if (count >0) {
if (count
totalWidth = (int) (count * mItemWidth + (count -1) *mSpacing) + totalWidth;
? ? ? ? ? ? }else {
totalWidth = (int) (count * mItemWidth + (count -1) *mSpacing) + totalWidth;
? ? ? ? ? ? }
}
return totalWidth;
? ? }
@Override
? ? protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i =0; i < getChildCount(); i++) {
View imageView = getChildAt(i);
? ? ? ? ? ? int column = i %mColumnCount;
? ? ? ? ? ? int row = i /mColumnCount;
? ? ? ? ? ? int left = (int) (getPaddingLeft() + column * (mSpacing +mItemWidth));
? ? ? ? ? ? int top = (int) (getPaddingTop() + row * (mSpacing +mItemHeight));
? ? ? ? ? ? imageView.layout(left, top, left +mItemWidth, top +mItemHeight);
? ? ? ? }
}
/**
* 顯示圖片
*/
? ? public void setImageUrls(final List imageUrls) {
removeAllViews();
? ? ? ? if (imageUrls ==null || imageUrls.size() ==0) {
return;
? ? ? ? }
int count = imageUrls.size();
? ? ? ? if (count ==1) {
//一般在url中會有寬高 可以算出寬高比
//測試url固定用的 1000:1376
//? ? ? ? ? ? mItemAspectRatio = AppUtil.getAspectRatio(imageUrls.get(0));
? ? ? ? ? ? mItemAspectRatio =1000 /1376f;
? ? ? ? }else {
mItemAspectRatio =1;
? ? ? ? }
for (int i =0; i < imageUrls.size(); i++) {
ImageView imageView =new ImageView(getContext());
? ? ? ? ? ? Uri uri = Uri.parse(imageUrls.get(i));
//? ? ? ? ? ? imageView.setImageURI(uri);
? ? ? ? ? ? imageView.setImageDrawable(getContext().getResources().getDrawable(R.drawable.icon_public_placeholder_android));
? ? ? ? ? ? addView(imageView);
? ? ? ? ? ? //點擊查看大圖
? ? ? ? ? ? imageView.setOnClickListener(new OnClickListener() {
@Override
? ? ? ? ? ? ? ? public void onClick(View v) {
}
});
? ? ? ? }
}
public int getItemWidth() {
return mItemWidth;
? ? }
public int getItemHeight() {
return mItemHeight;
? ? }
public int getColumnCount() {
return mColumnCount;
? ? }
public void setColumnCount(int columnCount) {
mColumnCount = columnCount;
? ? ? ? invalidate();
? ? }
public float getSpacing() {
return mSpacing;
? ? }
public void setSpacing(float spacing) {
mSpacing = spacing;
? ? ? ? invalidate();
? ? }
public float getItemAspectRatio() {
return mItemAspectRatio;
? ? }
public void setItemAspectRatio(float itemAspectRatio) {
mItemAspectRatio = itemAspectRatio;
? ? }
}
//XML中使用
<com.example.imitmationfriendcycledemo.widgt.WeChatFriendsCircleImageLayout
? android:id="@+id/friends_circle_item_image_layout"
? ? android:layout_width="wrap_content"
? ? android:layout_height="wrap_content"
? ? android:layout_marginTop="5dp" />
//adapter中使用:
ContentViewHolder viewHolder = (ContentViewHolder) holder;
//? ? ? ? viewHolder.mAvatarView.setImageURI(mImageUrl);
? ? ? ? viewHolder.mNameTv.setText("張新成充石、吳倩");
? ? ? ? viewHolder.mContentTv.setText("超喜歡你們啊哈哈.我是一個絕世超級無敵大美女莫换,啊哈哈。");
? ? ? ? //int count = (int) (Math.random() * 9);
? ? ? ? List imageUrls =new ArrayList<>();
? ? ? ? int count = position %10;
? ? ? ? for (int i =0; i < count; i++) {
? ?imageUrls.add(mImageUrl);? }
viewHolder.mImageLayout.setImageUrls(imageUrls);