填充正弦图形Android

5

我有这段代码:

public class TestView extends View {

private Path mPath;
private Paint mPaint = new Paint();

public TestView(Context context) {
    this(context, null);
}

public TestView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public TestView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mPath = new Path();
    mPath.moveTo(0, 0);
    mPath.quadTo(50, -50, 100, 0);
    mPath.quadTo(150, 50, 200, 0);
    mPath.quadTo(250, -50, 300, 0);
    mPath.quadTo(350, 50, 400, 0);
    mPath.quadTo(450, -50, 500, 0);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.save();
    canvas.translate(0, getMeasuredHeight() / 2F);
    // just making sure to clip starting and ending curve
    canvas.clipRect(50, -100, 450, 100, Region.Op.INTERSECT);
    canvas.drawPath(mPath, mPaint);
    canvas.restore();
}
}

这会产生以下结果:

enter image description here

但我想要这个(请原谅我的绘画技巧):

enter image description here

欢迎提供任何有关填充正弦曲线下方区域的帮助。


什么是Region post其导入行或实现,从这一行开始canvas.clipRect(50, -100, 450, 100, Region.Op.INTERSECT)? - Ahmad Dwaik 'Warlock'
@Warlock 是 import android.graphics.Region; - M-Wajeeh
@Warlock 问题已经解决了,我只需要稍微调整一下 quadTo() 的值。无论如何还是谢谢你。 - M-Wajeeh
2个回答

6

将您的值更改为

mPath.moveTo(0,100);
mPath.quadTo(50, -50, 100, 0);
mPath.quadTo(150, 50, 200, 0);
mPath.quadTo(250, -50, 300, 0);
mPath.quadTo(350, 50, 400, 0);
mPath.quadTo(450, -50, 500, 100);

2
有点晚了 :) ? 我已经用类似的方法解决了。无论如何,谢谢。 - M-Wajeeh

2
没关系,这个产生了我想要的结果:
    mPath = new Path();
    mPath.moveTo(0, 0);
    mPath.quadTo(50, -100, 100, -50);
    mPath.quadTo(150, -0, 200, -50);
    mPath.quadTo(250, -100, 300, -50);
    mPath.quadTo(350, -0, 400, -50);
    mPath.quadTo(450, -100, 500, -0);
    mPath.close();

enter image description here


1
你仍然不需要在我的解决方案中改变太多的值 :) 只需更改起点和终点的y值即可。 - Ahmad Dwaik 'Warlock'
如果想将其设置为屏幕的一半背景(底部到屏幕的一半),参数应该是什么? - DJhon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接