android 通过修改图片像素实现CircleImageView
CircleImageView实现方法有很多种,各有优缺点,因此需要按照不同的场景使用。我们今天使用修改图片像素的方法实现CircleImageView,主要知识点无非是勾股定理和点到圆形的距离。 素材图片 : 效果如下: ** 1、clipPath裁剪画布 ** 该方法支持的最小版本是Android 4.3(API Level 18),方便快捷,但是不支持硬件加,此外也存在Path既有的缺点,不支持抗锯齿。 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint (); mBitmap = BitmapFactory . decodeResource ( getResources (), R . drawable . pic ); mPath = new Path (); mPath . addCircle ( mBitmap . getWidth () / 2 , mBitmap . getHeight () / 2 , mBitmap . getWidth () / 2 , Path . Direction . CCW ); canvas . clipPath ( mPath ); canvas . drawBitmap ( mBitmap ,