public class SDAvatarListLayout extendsHorizontalScrollView {privateContext context;/*** 存放创建的最大的ImageView集合*/
private ListimageViewList;/*** 默认图片大小*/
private int imageSize = Math.round(SDTransitionUtil.dp2px(50));/*** 默认图片数量*/
private int imageMaxCount = 6;/*** 默认图片偏移百分比 0~1*/
private float imageOffset = 0.3f;publicSDAvatarListLayout(Context context) {this(context, null);
}publicSDAvatarListLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);
}public SDAvatarListLayout(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);this.context =context;
TypedArray ta=getResources().obtainAttributes(attrs, R.styleable.SDAvatarListLayout);
imageMaxCount=ta.getInt(R.styleable.SDAvatarListLayout_image_max_count, imageMaxCount);
imageSize= (int) ta.getDimension(R.styleable.SDAvatarListLayout_image_size, imageSize);
imageOffset=ta.getFloat(R.styleable.SDAvatarListLayout_image_offset, imageOffset);
imageOffset= imageOffset > 1 ? 1: imageOffset;
init();
ta.recycle();
}private voidinit() {
setHorizontalScrollBarEnabled(false);
RelativeLayout relativeLayout= newRelativeLayout(context);int offset = imageSize - (int) (imageSize *imageOffset);
imageViewList= new ArrayList<>(imageMaxCount);for (int i = 0; i < imageMaxCount; i++) {
SDCircleImageView imageView= newSDCircleImageView(context);
imageView.setId(imageView.hashCode()+i);
imageView.setBorderColor(Color.WHITE);
imageView.setBorderWidth(Math.round(SDTransitionUtil.dp2px(1)));
RelativeLayout.LayoutParams params= newRelativeLayout.LayoutParams(imageSize, imageSize);
params.addRule(ALIGN_PARENT_LEFT);
params.setMargins((imageMaxCount- i - 1) * offset, 0, 0, 0);
relativeLayout.addView(imageView, params);
imageViewList.add(imageView);
}
relativeLayout.setLayoutParams(newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));this.addView(relativeLayout);
}public voidsetAvatarListListener(ShowAvatarListener listener) {
hideAllImageView();
listener.showImageView(imageViewList);
}public void setAvatarListListener(ListdrawableList) {if (drawableList == null) {return;
}
hideAllImageView();int i = imageMaxCount - 1;for (inturl : drawableList) {
imageViewList.get(i).setImageResource(url);
imageViewList.get(i).setVisibility(VISIBLE);if (i == 0) {break;
}--i;
}
}private voidhideAllImageView() {for(SDCircleImageView head : imageViewList) {
head.setVisibility(View.GONE);
}
}public interfaceShowAvatarListener {void showImageView(ListimageViewList);
}
}