【问题标题】:Shifting a graphics Path object over移动图形路径对象
【发布时间】:2011-08-26 18:27:38
【问题描述】:

在 Java,特别是 Android 中,如何将 Path 对象转换为 100 像素以上?就像在 C# 中一样,我将使用以下代码来执行此操作:

// Create a path and add and ellipse.
GraphicsPath myPath = new GraphicsPath();
myPath.AddEllipse(0, 0, 100, 200);

// Draw the starting position to screen.
e.Graphics.DrawPath(Pens.Black, myPath);

// Move the ellipse 100 points to the right.
Matrix translateMatrix = new Matrix();
translateMatrix.Translate(100, 0);
myPath.Transform(translateMatrix);

// Draw the transformed ellipse to the screen.
e.Graphics.DrawPath(new Pen(Color.Red, 2), myPath);

如何在 Android 中执行此操作?我已经有了我的 Path 对象:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint pnt = new Paint();
    Path p = new Path();    

    pnt.setStyle(Paint.Style.STROKE);
    pnt.setColor(Color.WHITE);

    p.moveTo(97.4f, 87.6f);
    p.lineTo(97.4f, 3.8f);

    p.lineTo(-1.2f, 1.2f);
    p.lineTo(-0.4f,-0.4f);

    p.lineTo(-0.4f,87f);

    p.lineTo(97.4f, 87.6f);
    p.close();          

    canvas.drawPath(p, pnt);            
}

为了将我的 Path 对象移动超过 100 像素,我需要做什么?

【问题讨论】:

    标签: java android path matrix


    【解决方案1】:

    几乎一样:

    Matrix translateMatrix = new Matrix();
    translateMatrix.setTranslate(100,0);
    p.transform(translateMatrix);
    

    我没有测试它,只是查看了 API。

    【讨论】:

      【解决方案2】:

      我认为Path.offset(float dx, float dy) 是您正在寻找的。​​p>

      【讨论】:

        猜你喜欢
        • 2016-08-25
        • 2011-09-03
        • 2013-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        相关资源
        最近更新 更多