4

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

4

4 回答 4

2

从当前变换矩阵中提取比例并使用它的倒数来设置您的笔画宽度。

于 2011-11-15T13:35:36.257 回答
0

由于解决方案包括扩展一个类,我将发布详细信息。我没有对它进行广泛的测试,但只是在我需要它的上下文中。
我想从像 Picture.recording() 工作方式这样的动作列表中获得一个 Drawable。
幸运的是,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) 方法考虑了边界调整大小。

于 2011-11-15T14:41:49.673 回答
0

这是一个愚蠢的答案:

画你的笔画 X 次, w = 0 并排。

于 2011-11-14T13:28:48.070 回答
0

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

或者你可以看看这是否已经做了你想要的:

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

于 2011-11-14T13:45:44.737 回答