基于合金应用

时间:2016-05-07 11:49:11

标签: javascript appcelerator appcelerator-titanium

我正在构建一个合金应用程序,我需要根据他在图像上的点击位置对用户进行身份验证,并加载ImageView。该图像具有很少的标记认证点,其中任何一个点击时,显示点击位置是对还是错。我怎么能建立这个?

到目前为止,我只能使用ImageView的rect属性获取触摸事件侦听器中的tapped位置和图像属性。如何确定图像中标记的位置以识别这些特定位置的点击?

以下是我正在使用的代码(源于此:How to convert coordinates of the image view to the coordinates of the bitmap?):

    // eventX, eventY are x and y coordinates of tapped location
    function getScaledCoordinates(imageView, eventX, eventY) {
    //original height and width of the image 
    var originalImageBounds = imageView.toBlob();                                                                                                                                                                 
    var intrinsicHeight = originalImageBounds.height;
    var intrinsicWidth = originalImageBounds.width;

    //height and width of the visible (scaled) image
    var imageBounds = imageView.getRect();                                                                                                                                                         
    var scaledHeight = imageBounds.height;
    var scaledWidth = imageBounds.width;

    //Find the ratio of the original image to the scaled image                                                                                                                                                                                                                                                                                           
    var heightRatio = intrinsicHeight / scaledHeight;
    var widthRatio = intrinsicWidth / scaledWidth;

    //get the distance from the left and top of the image bounds                                                                                                                                             
    var scaledImageOffsetX = (eventX - imageBounds.x);
    var scaledImageOffsetY = (eventY - imageBounds.y);

    // get the units in device pixel                                                                                                                                                                         
    // getUnitsInDevicePixel(scaledImageOffsetX, scaledImageOffsetY)

    //scale these distances according to the ratio of your scaling                                                                                                                                                                                                                                                                                                                      
    var originalImageOffsetX = scaledImageOffsetX * widthRatio;
    var originalImageOffsetY = scaledImageOffsetY * heightRatio;

    // Return the coordinates in original image adjusted for scale factor                                                                                                                                    
    return [originalImageOffsetX, originalImageOffsetY];
}

我有存储在数据结构中的原始标记点的坐标。 我甚至能够让这个工作,但解决方案缺乏准确性。有时,点击的位置会正确映射到原始图像,有时则不会。有什么建议可以改进吗?

我一直在寻找clickable area of image的内容,但找不到足够的文档来在Titanium中实现这些内容。另外,我读到了在标记位置放置隐形按钮的想法,但不知道如何在Titanium中使用它。

任何帮助将不胜感激。提前谢谢!

0 个答案:

没有答案