0

我有自定义视图,其中包含许多矩形和椭圆形......我可以使用 canvas.Rotate(degree,cneterX,centerY) 旋转我的整个画布(矩形和椭圆形......)

但是在我的视图的底部,我想要一个类似菜单的东西,当我使用 canvas.rotate() 时它不会旋转。这意味着我想旋转这些矩形和椭圆,但不想旋转将使用相同画布创建的菜单。

@Override 
  protected void onDraw(Canvas canvas) {

    float ringWidth = textHeight + 4; 
    int height = getMeasuredHeight(); 
    int width =getMeasuredWidth(); 

    int px = width; 
    int py = height/2; 
    Point center = new Point(px,py ); 

    int radius = Math.max(px, py)-2; 

    int UpperSide = center.y;

    RectF boundingBox = new RectF(center.x - radius,  center.y - radius, center.x + radius, center.y + radius); 

    RectF innerBoundingBox = new RectF(center.x - radius , center.y - radius , center.x + radius, center.y + radius ); 


    RectF GroundSectionBox = new RectF(center.x - radius,UpperSide ,center.x + radius,center.y + radius);

    RectF RightPanel = new RectF(center.x + width -(width/4), center.y - (radius/2)+10, center.x+radius- ringWidth, center.y + (radius/2)-10);
    RectF LeftPanel = new RectF(center.x - radius + ringWidth ,center.y - (radius/2)+10, center.x - width + (width/4), center.y + (radius/2)-10);

    RectF RightBlack = new RectF(center.x + width -(width/4),center.y-(radius/10),center.x+radius- ringWidth,center.y + (radius/10));
    RectF leftBlack = new RectF(center.x - radius + ringWidth ,center.y - (radius/10), center.x - width + (width/4), center.y + (radius/10));

        .
        .
    canvas.drawRect(innerBoundingBox, groundPaint);

    canvas.drawPath(skyPath, skyPaint);     
    canvas.drawRect(GroundSectionBox, skyPaint);

    canvas.drawPath(skyPath, markerPaint);

    canvas.drawRect(RightBlack, sideBlack);
    canvas.drawRect(leftBlack, sideBlack);


    canvas.drawRect(RightPanel, RightPanelup);
    canvas.drawRect(LeftPanel, LeftPanelUp);
        .
        .
      }
4

1 回答 1

0

您可以使用 Canvas 上的 save 和 restore 方法在转换中绘制内容,然后恢复到原始状态。这是一个例子:

protected void onDraw(Canvas canvas)
{
     canvas.save(); // Canvas is in original state. Save it before anything

     // Perform any transformations you want here (like rotate)
     // Draw while your transformations are in place (like ovals) 

     canvas.restore(); // Canvas state returned to what it was when you called save

     // Draw things that need to be drawn without any transformations
}

希望这可以帮助 :)

于 2013-10-03T12:44:21.890 回答