android不同形状的头像

这里就是记载一下https://github.com/MostafaGazar/CustomShapeImageView实现的。


使用也很简单

添加

dependencies {
    ...
    compile 'com.mostafagazar:customshapeimageview:1.0.4'
    ...
}

然后在res包下建一个raw包存放.svg文件(你需要图片的形状)

然后使用

<com.meg7.widget.SvgImageView
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:src="@drawable/sample"
    app:svg_raw_resource="@raw/shape_star"
    android:scaleType="centerCrop" />
因为大佬使用的是 
取两层绘制交集。显示上层。
PorterDuff.Mode.DST_IN)

方法。不符合我的需求我要

11.PorterDuff.Mode.DST_ATOP
取上层非交集部分与下层交集部分

所以我就修改了一下

public class HexagonAvatarView extends ImageView {

    private static final String TAG = HexagonAvatarView.class.getSimpleName();

    public static final int DEFAULT_SVG_RAW_RES = R.raw.avatar;//.svg想要的形状

    private int mSvgRawRes = DEFAULT_SVG_RAW_RES;

    protected Context mContext;

   // private static final Xfermode sXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);修改处
    private static final Xfermode sXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP);
    private Bitmap mMaskBitmap;
    private Paint mPaint;
    private WeakReference<Bitmap> mSrcWeakBitmap;

    private int mLastWidth;
    private int mLastHeight;

    public HexagonAvatarView(Context context) {
        super(context);

        sharedConstructor(context, null);
    }

    public HexagonAvatarView(Context context, AttributeSet attrs) {
        super(context, attrs);

        sharedConstructor(context, attrs);
    }

    public HexagonAvatarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        sharedConstructor(context, attrs);
    }

    private void sharedConstructor(Context context, AttributeSet attrs) {
        mContext = context;

        mPaint = new Paint();

        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaskedImageView);

            mSvgRawRes = a != null ? a.getResourceId(R.styleable.MaskedImageView_masked, DEFAULT_SVG_RAW_RES) : DEFAULT_SVG_RAW_RES;
            a.recycle();
        }
    }

    public static void drawBitmap(Canvas canvas, Bitmap bitmap,
                                  Paint paint) {
        drawBitmap(canvas, bitmap, paint, 0, 0);
    }

    public static void drawBitmap(Canvas canvas, Bitmap bitmap,
                                  Paint paint, int left, int top) {
        paint.reset();
        paint.setFilterBitmap(false);
        paint.setXfermode(sXfermode);

        canvas.drawBitmap(bitmap, left, top, paint);
    }

    public void invalidate() {
        mSrcWeakBitmap = null;
        if (mMaskBitmap != null) {
            mMaskBitmap.recycle();
        }
        mLastWidth = 0;
        mLastHeight = 0;

        super.invalidate();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        if (!isInEditMode()) {
            int width = getWidth();
            int height = getHeight();

            int i = canvas.saveLayer(0.0F, 0.0F, width, height, null, Canvas.ALL_SAVE_FLAG);
            try {
                Bitmap srcBitmap = mSrcWeakBitmap != null ? mSrcWeakBitmap.get() : null;
                if (srcBitmap == null || srcBitmap.isRecycled()) {
                    Drawable srcDrawable = getDrawable();
                    if (srcDrawable != null) {
                        srcBitmap = Bitmap.createBitmap(getWidth(),
                                getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas srcBitmapCanvas = new Canvas(srcBitmap);
                        srcDrawable.setBounds(0, 0, getWidth(), getHeight());
                        srcDrawable.draw(srcBitmapCanvas);

                        // Skip and use cached mask.
                        if (mMaskBitmap == null || mMaskBitmap.isRecycled() ||
                                mLastWidth != width || mLastHeight != height) {
                            mMaskBitmap = getMask(width, height);
                        }

                        drawBitmap(srcBitmapCanvas, mMaskBitmap, mPaint);
                        mSrcWeakBitmap = new WeakReference<Bitmap>(srcBitmap);
                    }
                }

                if (srcBitmap != null) {
                    mPaint.setXfermode(null);
                    canvas.drawBitmap(srcBitmap, 0.0F, 0.0F, mPaint);
                }
            } catch (Exception e) {
                System.gc();

                Log.e(TAG, String.format("Unable to draw, view Id :: %s. Error occurred :: %s", getId(), e.toString()));
            } finally {
                canvas.restoreToCount(i);
            }
        } else {
            super.onDraw(canvas);
        }
    }

    private Bitmap getDefaultMask(int width, int height) {
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLACK);
        canvas.drawRect(new RectF(0.0F, 0.0F, width, height), paint);

        return bitmap;
    }

    private Bitmap getMask(int width, int height) {
        SVG svgMask = null;
        if (mLastWidth != width || mLastHeight != height) {
            svgMask = SVGParser.getSVGFromInputStream(
                    mContext.getResources().openRawResource(mSvgRawRes), width, height);

            mLastWidth = width;
            mLastHeight = height;
        }

        if (svgMask != null) {
            Bitmap bitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.BLACK);

            canvas.drawPicture(svgMask.getPicture());

            return bitmap;
        }

        // In case everything failed, return square.
        return getDefaultMask(width, height);
    }

    public void updateMask(int svgRawRes) {
        if (mSvgRawRes != svgRawRes) {
            mSvgRawRes = svgRawRes;

            invalidate();
        }
    }

}

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MaskedImageView">

        <attr name="masked" format="reference" />

    </declare-styleable>
</resources>
使用

<com.olddriver.yihaotu.view.HexagonAvatarView
    android:id="@+id/iv_avatar"
    android:layout_width="@dimen/base55dp"
    android:layout_height="@dimen/base61dp"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

感谢:http://blog.csdn.net/lmj623565791/article/details/42094215

           https://github.com/MostafaGazar/CustomShapeImageView





 
 

猜你喜欢

转载自blog.csdn.net/qq_35698774/article/details/78062146
今日推荐