如何当一个绘触摸检测 [英] How to detect when a drawable is touched

查看:129
本文介绍了如何当一个绘触摸检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是有一堆用户可以拖动周围的小图像的画布上的应用程序,和一个盒子,用户将拖动以创建新的图像对象或现有图像拖动到它,它将移除。

I'm working on an app that has a canvas with a bunch of little images that the user can drag around, and a "box" that the user will drag from to create new image objects or drag an existing image to it, and it will remove it.

什么是检测用户是否已触摸的区域,或在屏幕上的图像的最佳方法?想到的显然是比较触摸的第一件事情,每个对象的坐标的坐标,但似乎有点麻烦。有没有更好的,或者内置的方式?

What is the best way to detect if the user has touched an area, or an image on the screen? The first thing that comes to mind is obviously comparing the touch coordinates with each object's coordinates, but that seems a little cumbersome. Is there a better, or built in way?

感谢

推荐答案

借助查看 Android中类有一个o​​nClickListener,您可以使用,当用户点击一个视图来检测。如果您实现您的图像(你叫他们)的意见,您可以点击监听器简单地添加到每个找到被单击视图。添加监听器是这样的:

The View class in Android has an onClickListener which you can use to detect when the user clicks on a View. If you implement your 'images' (as you call them) as Views you can simply add a click listener to each to find which view was clicked. Add the listener like this:

// Get a reference to the view from xml (or if you 
// have created it dynamically, just use that)
View imageView = findViewById(R.id.my_view);
imageView.setOnClickListener(new OnClickListener()
{

  @Override
  public void onClick(View v)
  {
    // Do stuff with view (the View v that is passed is the View that was touched)
    doStuff(v);  
  }
});

这篇关于如何当一个绘触摸检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆