【问题标题】:NativeScript View _createUI Accessing Canvas?NativeScript View _createUI 访问画布?
【发布时间】:2016-04-19 01:04:23
【问题描述】:

我想通过 NativeScript 访问 Canvas,但我似乎找不到桥接入口点。因为 _CreateUI 不会在画布中传递,而 Java 中的 onDraw 会传递。

我想做以下 NativeScript Equiv:

@Override 
protected void onDraw(Canvas canvas) { 
    // floor the border width to avoid gaps between the border and the image 
    float roundedBorderWidth = (float) Math.floor(this.borderWidth); 
    float innerRadius = Math.max(0, this.cornerRadius - roundedBorderWidth); 

    // The border width is included in the padding so there is no need for 
    // clip if there is no inner border radius. 
    if (innerRadius != 0) { 
        this.rect.set( 
                roundedBorderWidth, 
                roundedBorderWidth,  
                this.getWidth() - roundedBorderWidth,  
                this.getHeight() - roundedBorderWidth); 

        this.path.reset(); 
        this.path.addRoundRect(rect, innerRadius, innerRadius, android.graphics.Path.Direction.CW); 

        canvas.clipPath(this.path); 
    } 

    super.onDraw(canvas); 
} 

谢谢 菲尔

【问题讨论】:

    标签: android-canvas nativescript


    【解决方案1】:

    这里是一个关于如何在 NativeScript Android 应用中构建 Canvas 的快速示例。使用带有 NativeScript 的 Placeholder component 来创建原生组件,除非您想编写插件/模块,否则您将完全采用不同的方法。

    XML:

    <Page> <Placeholder creatingView="createCanvas" height="200" /> </Page>
    

    JavaScript 代码:

    function createCanvas(args) {
         var canvas = new android.graphics.Canvas(); // construct an empty canvas
         args.view = canvas; // this puts the canvas on the interface where the placeholder is.
    }
    exports createCanvas = createCanvas;`
    

    【讨论】:

    • 嗯,这很有趣,因为我刚刚在 npm 上发布了 1 个插件,您建议以什么方式将其作为插件?
    • 如果您创建一个插件供其他人使用或重用,那么请按照我的几个用于 nativescript 的插件作为一个很好的指南。这是一个利用 createUI() 函数的好方法。 github.com/bradmartin/nativescript-gif
    • 嗨,布拉德,您能指导我如何在 nativescript 中使用“android.graphics.Bitmap”。我想使用 Bitmap 类创建位图,但没有找到任何文档。谢谢!
    • @OnkarNene 打开一个关于 SO 的问题,至少有一些你想要什么的上下文,也许还有一个尝试的代码 sn-p,我很乐意提供帮助 :)
    • @BradMartin 请检查链接并告诉我解决方案:) - stackoverflow.com/questions/37724634/nativescript-bitmap-canvas
    猜你喜欢
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多