这是我用来在画布上绘制圆形(然后在它上面的图标位图):
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;}
但我不知道如何绘制松鼠形状而不是圆形.仅供参考,以下是使用松鼠形状的图标的一些示例:
解决方法:
@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; }
希望这有帮助,这是一个预览:
总结以上是内存溢出为你收集整理的在画布上绘制一个松鼠形状(Android)全部内容,希望文章能够帮你解决在画布上绘制一个松鼠形状(Android)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)