Android学习之仿QQ讨论组和微信群聊头像

    这是自己总结的,希望对你们有帮助

  1:先看效果


  

 

                                                       

2:具体代码实现


2.1 直接粘代码

 

public class GroupView extends ImageView {
    /** 图片之间的距离 */
    private  int padding = 2;
    /** 圆角值 */
    private  int cornor = 0;

    private  int width,height;;
    /** 头像模式 圆的 */
    public static final int FACETYPE_CIRCLE = 1;
    /** 头像模式 方的 最多9个 */
    public static final int FACETYPE_SQUARE = 2;

    private Context mContext;

    private int mViewType;//默认圆形
    private Bitmap[] mBitmaps ;
    private int background;
    public GroupView(Context context,Bitmap[] bitmaps) {
        super(context);
        this.mBitmaps = bitmaps;
    }

    public GroupView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GroupView, defStyleAttr, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.GroupView_type:
                    mViewType = a.getInt(R.styleable.GroupView_type, FACETYPE_CIRCLE);
                    break;
                case R.styleable.GroupView_cornor:
                    cornor = (int) a.getDimension(R.styleable.GroupView_cornor,0);
                    break;
                case R.styleable.GroupView_padding:
                    padding = (int) a.getDimension(R.styleable.GroupView_padding,3);
                case R.styleable.GroupView_backgroundGP:
                    background = a.getColor(R.styleable.GroupView_backgroundGP,Color.parseColor("#DDDFD4"));
                    break;
                default:
                    break;
            }
        }

        a.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int widthMeasureSpec){
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.AT_MOST) {
            width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        } else {
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        return width;
    }

    private int measureHeight(int heightMeasureSpec){
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.AT_MOST) {
            height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        } else {
            height = MeasureSpec.getSize(heightMeasureSpec);
        }
        return height;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // super.onDraw(canvas);
        Paint paint = new Paint();
        canvas.drawBitmap(createGroupFace(mViewType, mContext, mBitmaps),0,0,paint);
    }

    public void setImageBitmaps(Bitmap[] bitmaps) {
        mBitmaps = bitmaps;
    }

    public void setImageBitmaps(List<Bitmap> bitmaps) {
        mBitmaps = bitmaps.toArray(new Bitmap[bitmaps.size()]);
    }

    public  Bitmap createGroupFace(int type, Context context,
                                   Bitmap[] bitmapArray) {
        System.out.println("hiwhitley"+"type"+type);
        if (type == FACETYPE_CIRCLE) {
            return createGroupBitCircle(bitmapArray, context);
        }
        return createTogetherBit(bitmapArray, context);
    }

    private static Bitmap scaleBitmap(float paramFloat, Bitmap paramBitmap) {
        Matrix localMatrix = new Matrix();
        localMatrix.postScale(paramFloat, paramFloat);
        return Bitmap.createBitmap(paramBitmap, 0, 0, paramBitmap.getWidth(),
                paramBitmap.getHeight(), localMatrix, true);
    }

    /**
     * 拼接群头像 圆形版的
     *
     * @param bitmapArray
     * @param context
     * @return
     */
    private  Bitmap createGroupBitCircle(Bitmap[] bitmapArray,
                    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值