组合式控件-用户头像

十分简单的控件,用户头像+标识

1:设置attrs

    
        
        
        
        
        
        
        
        
        
        
    

2:主要代码

/**
 * create by zj on 2018/10/18
 *
 */
public class CommonAvatar extends RelativeLayout implements View.OnClickListener {


    CircleImageView avatar;
    ImageView icon;
    private Context mContext;
    private int type;//跳转类型
    private String userId;
    public CommonAvatar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initViews(context);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonAvatar);
        int gradeIconWidth = (int) mTypedArray.getDimension(R.styleable.CommonAvatar_gradeIconWidth, DisplayUtils.dp2px(context, 10));
        int gradeIconHeight = (int) mTypedArray.getDimension(R.styleable.CommonAvatar_gradeIconHeight, DisplayUtils.dp2px(context, 10));
        int gradeIconMarginRight = (int) mTypedArray.getDimension(R.styleable.CommonAvatar_gradeIconMarginRight, 0);
        int gradeIconMarginBottom = (int) mTypedArray.getDimension(R.styleable.CommonAvatar_gradeIconMarginBottom, 0);
        Boolean clickable = mTypedArray.getBoolean(R.styleable.CommonAvatar_clickable, false);
        avatar.invalidate();
        LayoutParams params = (LayoutParams) icon.getLayoutParams();
        params.setMargins(0, 0, gradeIconMarginRight, gradeIconMarginBottom);
        params.width = gradeIconWidth;
        params.height = gradeIconHeight;
        if (clickable) {
            setOnClickListener(this);
        }
    }
    private void initViews(Context context) {
        avatar = new CircleImageView(context);
        RelativeLayout.LayoutParams paramsAvatar = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        paramsAvatar.addRule(CENTER_IN_PARENT);
        addView(avatar, paramsAvatar);
        icon= new ImageView(context);
        RelativeLayout.LayoutParams paramsIcon = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        paramsIcon.addRule(ALIGN_PARENT_BOTTOM);
        paramsIcon.addRule(ALIGN_PARENT_RIGHT);
        addView( icon, paramsIcon);
    }

    //设置头像
    public void setAvatar(String avatarUrl,String gradeUrl,int goType,String userId){
        GlideApp.with(mContext).load(avatarUrl).placeholder(R.drawable.icon_logo_default).into(avatar);
        if (gradeUrl==null||gradeUrl.equals("")){
            icon.setVisibility(GONE);
        }else{
            icon.setVisibility(VISIBLE);
            GlideApp.with(mContext).load(gradeUrl).into(icon);
        }
        type=goType;
        this.userId=userId;
    }

    @Override
    public void onClick(View v) {

        switch (type){
            case 0: //跳转个人信息修改界面

                break;
            case 1://个人中心
                
                break;
            case 2://查看大图

                break;
            case 3:

                break;
            default:

                break;
        }
    }
}

主要包括,创建两个imageview,头像以及标识icon;

设置点击以及类型,跳转不同的界面。

你可能感兴趣的:(组合式控件-用户头像)