Drawing a squircle shape on canvas (Android)

后端 未结 2 693
南笙
南笙 2021-01-06 03:15

Here is what I\'m using to draw a circle shape on to the canvas (and then an icon bitmap on it):

private static Bitmap makeIcon(int radius, int color, Bitmap         


        
2条回答
  •  天命终不由人
    2021-01-06 03:32

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Path squirclePath = getSquirclePaath(150, 250, 400);
            canvas.drawPath(squirclePath, mPaint);
        }
    
        private static Path getSquirclePaath(int left, int top, int radius){
            //Formula: (|x|)^3 + (|y|)^3 = radius^3
            final double radiusToPow = radius * radius * radius;
    
            Path path = new Path();
            path.moveTo(-radius, 0);
            for (int x = -radius ; x <= radius ; x++)
                path.lineTo(x, ((float) Math.cbrt(radiusToPow - Math.abs(x * x * x))));
            for (int x = radius ; x >= -radius ; x--)
                path.lineTo(x, ((float) -Math.cbrt(radiusToPow - Math.abs(x * x * x))));
            path.close();
    
            Matrix matrix = new Matrix();
            matrix.postTranslate(left + radius, top + radius);
            path.transform(matrix);
    
            return path;
        }
    

    Hope this helps, here is a preview:

提交回复
热议问题