美文网首页
Android裁剪画布构造圆形图片

Android裁剪画布构造圆形图片

作者: dlihasa | 来源:发表于2018-11-18 21:26 被阅读6次
    圆形图片
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.support.annotation.Nullable;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class CustomCircleView extends View{
    
        private Bitmap bitmap;
        private Paint mPaint;
        private Path mPath;
    
        public CustomCircleView(Context context) {
            super(context);
            init();
        }
    
        public CustomCircleView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public CustomCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            setLayerType(LAYER_TYPE_SOFTWARE,null);
            bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon_mobile);
            mPaint = new Paint();
            mPath = new Path();
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            mPath.addCircle(width/2,height/2,width/2,Path.Direction.CCW);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.save();
            canvas.clipPath(mPath);
            canvas.drawBitmap(bitmap,0,0,mPaint);
            canvas.restore();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android裁剪画布构造圆形图片

          本文链接:https://www.haomeiwen.com/subject/ojhnfqtx.html