【问题标题】:Drawing a squircle shape on canvas (Android)在画布上绘制松鼠形状 (Android)
【发布时间】:2018-05-07 02:40:19
【问题描述】:

这是我用来在画布上绘制圆形(然后在其上绘制图标位图)的方法:

private static Bitmap makeIcon(int radius, int color, Bitmap icon) {
    final Bitmap output = Bitmap.createBitmap(radius, radius, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawARGB(0, 0, 0, 0);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
        canvas.drawCircle(radius / 2, radius / 2, radius / 2, paint);
    else
        canvas.drawRect(0, 0, radius, radius, paint);
    int cx = (radius - icon.getWidth()) >> 1; // same as (...) / 2
    int cy = (radius - icon.getHeight()) >> 1;
    canvas.drawBitmap(icon, cx, cy, paint);
    icon.recycle();
    return output;
}

但我不知道如何绘制松鼠形状而不是圆形。仅供参考,以下是一些使用松鼠形状的图标示例:

【问题讨论】:

    标签: android drawing android-canvas shape


    【解决方案1】:
        @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;
        }
    

    这是预览:

    【讨论】:

    • 为唯一一个拯救生命的答案投票,谢谢先生
    【解决方案2】:

    另一种方法是使用BitmapShader

    注意:蒙版和图像的大小必须相同,因此您必须调整图像的大小。

    注意2:此代码是为启动器图标开发的,自适应图标适应能力较差。

    baseIconSize 是“目标”大小。

    fun Drawable.toBitmap(width: Int, height: Int, config: Bitmap.Config): Bitmap {
        val bitmap = Bitmap.createBitmap(width, height, config)
        val canvas = Canvas(bitmap)
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (this is AdaptiveIconDrawable) {
                background.setBounds(0, 0, width, height)
                background.draw(canvas)
    
                foreground.setBounds(0, 0, width, height)
                foreground.draw(canvas)
            } else {
                setBounds(0, 0, width, height)
    
                draw(canvas)
            }
        } else {
            setBounds(0, 0, width, height)
    
            draw(canvas)
        }
    
        return bitmap
    }
    
            val maskBitmap = requireNotNull(context.getDrawable(R.drawable.mask_squircle))
                .toBitmap(
                    width = baseIconSize,
                    height = baseIconSize,
                    config = Bitmap.Config.ALPHA_8
                )
    
            val iconBitmap = Bitmap.createBitmap(
                baseIconSize,
                baseIconSize,
                Bitmap.Config.ARGB_8888
            )
    
            val originalBitmap = if (bitmap.width == baseIconSize && bitmap.height == baseIconSize) {
                bitmap
            } else {
                bitmap.scale(baseIconSize, baseIconSize)
            }
    
            iconShapePaint.shader = BitmapShader(
                originalBitmap,
                Shader.TileMode.REPEAT,
                Shader.TileMode.REPEAT
            )
    
            val canvas = Canvas(iconBitmap)
            canvas.drawBitmap(maskBitmap, 0f, 0f, iconShapePaint)
    
            originalBitmap.recycle()
    
            return iconBitmap
    

    之前:

    之后:

    面具:

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="1024dp"
        android:height="1024dp"
        android:viewportWidth="1024"
        android:viewportHeight="1024">
      <path
          android:pathData="M512,1024C736.36,1024 861.08,1024 942.54,942.54C1024,861.08 1024,736.36 1024,512C1024,287.64 1024,162.92 942.54,81.46C861.08,0 736.36,0 512,0C287.64,0 162.92,0 81.46,81.46C0,162.92 0,287.64 0,512C0,736.36 0,861.08 81.46,942.54C162.92,1024 287.64,1024 512,1024Z"
          android:strokeWidth="1"
          android:fillColor="#000000"
          android:fillType="evenOdd"
          android:strokeColor="#00000000"/>
    </vector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      相关资源
      最近更新 更多