如何正确缩放矢量图形?

时间:2013-02-13 14:30:03

标签: android drawing vector-graphics

我正在学习如何使用矢量绘图API,我遇到了以下问题。

我在画布上绘制线条和圆弧,包含此画布的视图可以缩放缩放。放大时,线条完美缩放,但弧线没有。

公共类MyView扩展了View {

public MyView(Context context, AttributeSet attrs)
{
    super(context, attrs);

    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    mGestureDetector = new GestureDetector(context, new GestureListener());
    rects = new RectF[1000];

    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
}

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

    canvas.translate(mOffset.x * mScaleFactor + canvas.getWidth() * 0.5f, mOffset.y * mScaleFactor + canvas.getHeight() * 0.5f);
    canvas.scale(mScaleFactor, mScaleFactor);

    canvas.drawLine(-10, -5, 10, 5, paint);
    canvas.drawLine(10, -10, -10, 10, paint);
    paint.setStrokeWidth(1);
    RectF f = new RectF(-50, -50, 50, 50);
    canvas.drawArc(f, 0.f, 360.f, true, paint);
    canvas.restore(); 
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener       
    {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(0.01f, Math.min(mScaleFactor, 100.0f));
        invalidate();
        return true;
    }
}

}

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为你这样做是正确的,你的设备上的代码效果很好。enter image description here