android如何在画布上绘制三角形,星形,方形,心形

nic*_*.me 11 android

我可以使用path.addCircle()和path.addRect()在画布上绘制圆形和矩形.就像我想要触摸的那样,我能够绘制三角形,星形,方形和心形.我怎样才能做到这一点?给我一个示例示例.谢谢

roo*_*tek 26

对于未来的直接答案者,我使用画布绘制了一个几乎对称的星形,如图所示:

明星形象

主要工具是使用路径.

假设你有设置:

Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);

Path path = new Path();
Run Code Online (Sandbox Code Playgroud)

然后在你的onDraw中你可以像我下面那样使用路径.它可以适当地缩放到任何尺寸的帆布

@Override
protected void onDraw(Canvas canvas) {

    float mid = getWidth() / 2;
    float min = Math.min(getWidth(), getHeight());
    float fat = min / 17;
    float half = min / 2;
    float rad = half - fat;
    mid = mid - half;

    paint.setStrokeWidth(fat);
    paint.setStyle(Paint.Style.STROKE);

    canvas.drawCircle(mid + half, half, rad, paint);

    path.reset();

    paint.setStyle(Paint.Style.FILL);


        // top left
        path.moveTo(mid + half * 0.5f, half * 0.84f);
        // top right
        path.lineTo(mid + half * 1.5f, half * 0.84f);
        // bottom left
        path.lineTo(mid + half * 0.68f, half * 1.45f);
        // top tip
        path.lineTo(mid + half * 1.0f, half * 0.5f);
        // bottom right
        path.lineTo(mid + half * 1.32f, half * 1.45f);
        // top left
        path.lineTo(mid + half * 0.5f, half * 0.84f);

        path.close();
        canvas.drawPath(path, paint);

    super.onDraw(canvas);

}
Run Code Online (Sandbox Code Playgroud)

  • 其他答案倾向于说你需要在每个lineTo之后调用moveTo来创建一个填充多边形,这对我来说不起作用.谢天谢地,你的回答就是诀窍! (2认同)

Pro*_*mer 12

对于每个需要心脏形状的人:

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Paint.Style;
    import android.graphics.Path;
    import android.view.View;

    public class Heart extends View {

        private Path path;

        private Paint paint;

        public Heart(Context context) {
            super(context);

            path = new Path();
            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        }

            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);

                // Fill the canvas with background color
                canvas.drawColor(Color.WHITE);
                paint.setShader(null);

                float width = getContext().getResources().getDimension(R.dimen.heart_width);
                float height = getContext().getResources().getDimension(R.dimen.heart_height);

                // Starting point
                path.moveTo(width / 2, height / 5); 

                // Upper left path
                path.cubicTo(5 * width / 14, 0,
                        0, height / 15,
                        width / 28, 2 * height / 5);

                // Lower left path
                path.cubicTo(width / 14, 2 * height / 3,
                        3 * width / 7, 5 * height / 6,
                        width / 2, height);

                // Lower right path
                path.cubicTo(4 * width / 7, 5 * height / 6,
                        13 * width / 14, 2 * height / 3,
                        27 * width / 28, 2 * height / 5);

                // Upper right path
                path.cubicTo(width, height / 15,
                        9 * width / 14, 0,
                        width / 2, height / 5);

                paint.setColor(Color.RED);
                paint.setStyle(Style.FILL);
                canvas.drawPath(path, paint);

            }
    }
Run Code Online (Sandbox Code Playgroud)

对不起所有的数字,但这些对我来说效果最好:)结果如下:

在此输入图像描述


Mos*_*oss 10

你必须找出这些数字背后的数学.三角形和星形很容易画出来.以下是如何画出一颗心:http://www.mathematische-basteleien.de/heart.htm

要绘制特殊路径,您应该通过添加点,椭圆等来创建它们.画布支持指定路径的剪贴蒙版,因此您可以设置心脏的剪贴蒙版,将路径推送到矩阵,绘制心脏的内容,然后再次弹出它.

这就是我正在做的实现对andriod的模拟2D页面卷曲效果:http://code.google.com/p/android-page-curl/

希望这可以帮助!


edu*_*ayo 8

此方法将返回一个路径,其中给定宽度的正方形内给出的角数.添加更多参数来处理小半径等事情.

    private Path createStarBySize(float width, int steps) {
    float halfWidth = width / 2.0F;
    float bigRadius = halfWidth;
    float radius = halfWidth / 2.0F;
    float degreesPerStep = (float) Math.toRadians(360.0F / (float) steps);
    float halfDegreesPerStep = degreesPerStep / 2.0F;
    Path ret = new Path();
    ret.setFillType(FillType.EVEN_ODD);
    float max = (float) (2.0F* Math.PI);
    ret.moveTo(width, halfWidth);
    for (double step = 0; step < max; step += degreesPerStep) {
        ret.lineTo((float)(halfWidth + bigRadius * Math.cos(step)), (float)(halfWidth + bigRadius * Math.sin(step)));
        ret.lineTo((float)(halfWidth + radius * Math.cos(step + halfDegreesPerStep)), (float)(halfWidth + radius * Math.sin(step + halfDegreesPerStep)));
    }
    ret.close();
    return ret;
}
Run Code Online (Sandbox Code Playgroud)