nativescript - 如何使用 Angular2 在 Nativescript 中获取图像坐标

标签 nativescript angular2-nativescript nativescript-angular

我有一个图像要在我的 nativescript(使用 Angular2)应用程序中显示,我想让图像的不同部分可点击。例如人体图像,我只想知道用户点击了哪个部分。

有没有办法像 html 一样创建图像映射???

<CardView height="450" width="350" marginTop="10">
    <Image src="res://nerves" height="304" width="114" horizontalAlignment="center" verticalAlignment="center"></Image>
</CardView>

enter image description here

最佳答案

使用 (touch)您的 Image 上的事件绑定(bind)元素。

这是一个在您单击图像的第四象限时打印控制台消息的示例。

import {
  Component
} from '@angular/core';
import * as platform from 'platform';
import {
  TouchGestureEventData
} from 'tns-core-modules/ui/gestures';

@Component({
  moduleId: module.id,
  selector: 'your-component',
  template: `
    <GridLayout>
      <Image src="res://your_image" width="128" height="128"
             (touch)="touchImage($event)"
             verticalAlignment="middle" horizontalAlignment="center"></Image>
    </GridLayout>
  `
})
export class YourComponent {
  touchImage(event: TouchGestureEventData) {
    // This is the density of your screen, so we can divide the measured width/height by it.
    const scale: number = platform.screen.mainScreen.scale;
    if (event.action === 'down') {
      // this is the point that the user just clicked on, expressed as x/y
      // values between 0 and 1.
      const point = {
        y: event.getY() / (event.view.getMeasuredHeight() / scale),
        x: event.getX() / (event.view.getMeasuredWidth() / scale)
      };

      // add custom code to figure out if something significant was "hit"
      if (point.x > 0.5 && point.y > 0.5) {
        console.log('you clicked on the lower right part of the image.');
      }
    }
  }
}

关于nativescript - 如何使用 Angular2 在 Nativescript 中获取图像坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46485454/

相关文章:

nativescript - Android 在 NativeScript 中不显示字体图标

nativescript - 如何在 NativeScript <Button> 中使用字体图标

Angular 2 - 错误 : Supplied parameters do not match any signature of call target

ios - NativeScript:禁用 iOS WebView 缩放控件的方法?

webpack - NativeScript 应用程序与 Webpack 捆绑时出现 Javascript 堆错误

Nativescript 6.5 无法在 iOS 上构建模块 'nanopb'

Nativescript:套接字连接超时

android - NativeScript + Angular 2 : Use of back button click listener

android - 添加 nativescript-ui-dataform 插件后 NativeScript tns 构建失败

android - 如何在 Nativescript 中正确获取 Android Application Context