像Android中的SVG非缩放中风?

时间:2011-11-14 13:24:11

标签: android graphics svg android-canvas stroke

我有一个来自Picture beginRecording()方法的Canvas。
我在画布中记录内容,然后调用endRecording()。
我希望能够记录在画布之后缩放时不会缩放的笔划 我在Paint类中看不到类似的东西。你可以setStrokeWidth(float w),但是:
- 如果w == 0你有类似我想要的功能,但只有1px
- 如果w!= 0,画布缩放也意味着缩放比例。
有什么想法吗?

4 个答案:

答案 0 :(得分:2)

从当前变换矩阵中提取比例,并使用其相反来设置笔触宽度。

答案 1 :(得分:0)

这是一个愚蠢的答案:

在w = 0的情况下,将你的笔画画出X次。

答案 2 :(得分:0)

您可能需要在自定义SVG对象中跟踪宽度。随着对象的缩放,您可以找到新宽度与初始大小之间的比率,并将其乘以初始笔触宽度。它不一定是宽度,它可以很好地是高度或对角线。这取决于你的对象如何缩放。

或者您可以看到这是否已经满足您的需求:

http://code.google.com/p/svg-android/

答案 3 :(得分:0)

由于解决方案包括扩展课程,我将发布详细信息。我没有对它进行过广泛的测试,只是在我需要它的情况下 我希望从一系列动作中获得一个Drawable,就像Picture.recording()的工作方式一样 幸运的是,Path对象可以记录动作,然后我们可以将它们绘制到画布中 不幸的是,通过canvas.drawPath()绘制它并不提供无缩放笔画功能。

所以感谢@Andrew提供的提示,我将Shape扩展为类似于PathShape但在onResize()中有一些不同的逻辑

public class NonScalingStrokePathShape extends Shape{
    private Path    mPath;
    private float   mInitialWidth;
    private float   mInitialHeight;
    private float   mCurrentWidth;    
    private float   mCurrentHeight;    

    public NonScalingStrokePathShape(Path pPath, float pInitialWidth, float pInitialHeight) {
        mPath = pPath;
        mInitialWidth = pInitialWidth;
        mInitialHeight = pInitialHeight;
        mCurrentWidth = mInitialWidth;
        mCurrentHeight = mInitialHeight;
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawPath(mPath,paint);
    }

    @Override
    protected void onResize(float width, float height) {
        Matrix matrix = new Matrix();
        matrix.setScale(width / mCurrentWidth, height / mCurrentHeight);
        mCurrentWidth = width;
        mCurrentHeight = height;
        mPath.transform(matrix);
    }

    @Override
    public NonScalingStrokePathShape clone() throws CloneNotSupportedException {
        NonScalingStrokePathShape shape = (NonScalingStrokePathShape) super.clone();
        shape.mPath = new Path(mPath);
        shape.mInitialHeight =  mInitialHeight;
        shape.mInitialWidth = mInitialWidth;
        shape.mCurrentWidth = mInitialWidth;
        shape.mCurrentHeight = mInitialHeight;
        return shape;
    }

}

这可以在ShapeDrawable中使用,它是一个Drawable,它已经通过调用Shape resize(float w,float h)方法来调整大小的范围。