Android 自定義布局仿照朋友圈實現(xiàn)圖片的顯示

最近任務(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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市拉岁,隨后出現(xiàn)的幾起案子坷剧,更是在濱河造成了極大的恐慌,老刑警劉巖喊暖,帶你破解...
    沈念sama閱讀 221,635評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件惫企,死亡現(xiàn)場離奇詭異,居然都是意外死亡陵叽,警方通過查閱死者的電腦和手機狞尔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巩掺,“玉大人偏序,你說我怎么就攤上這事∨痔妫” “怎么了研儒?”我有些...
    開封第一講書人閱讀 168,083評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長独令。 經(jīng)常有香客問我殉摔,道長,這世上最難降的妖魔是什么记焊? 我笑而不...
    開封第一講書人閱讀 59,640評論 1 296
  • 正文 為了忘掉前任逸月,我火速辦了婚禮,結(jié)果婚禮上遍膜,老公的妹妹穿的比我還像新娘碗硬。我一直安慰自己,他們只是感情好瓢颅,可當我...
    茶點故事閱讀 68,640評論 6 397
  • 文/花漫 我一把揭開白布恩尾。 她就那樣靜靜地躺著,像睡著了一般挽懦。 火紅的嫁衣襯著肌膚如雪翰意。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評論 1 308
  • 那天信柿,我揣著相機與錄音冀偶,去河邊找鬼。 笑死渔嚷,一個胖子當著我的面吹牛进鸠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播形病,決...
    沈念sama閱讀 40,833評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼客年,長吁一口氣:“原來是場噩夢啊……” “哼霞幅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起量瓜,我...
    開封第一講書人閱讀 39,736評論 0 276
  • 序言:老撾萬榮一對情侶失蹤司恳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后绍傲,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扔傅,經(jīng)...
    沈念sama閱讀 46,280評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,369評論 3 340
  • 正文 我和宋清朗相戀三年唧取,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片划提。...
    茶點故事閱讀 40,503評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡枫弟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出鹏往,到底是詐尸還是另有隱情淡诗,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布伊履,位于F島的核電站韩容,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏唐瀑。R本人自食惡果不足惜群凶,卻給世界環(huán)境...
    茶點故事閱讀 41,870評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望哄辣。 院中可真熱鬧请梢,春花似錦、人聲如沸力穗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽当窗。三九已至够坐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間崖面,已是汗流浹背元咙。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留巫员,地道東北人蛾坯。 一個月前我還...
    沈念sama閱讀 48,909評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像疏遏,于是被迫代替她去往敵國和親脉课。 傳聞我的和親對象是個殘疾皇子救军,可洞房花燭夜當晚...
    茶點故事閱讀 45,512評論 2 359