如何使透明区域作为贱民在Android的ImageView的? [英] how to make transparent area as untouchable for ImageView in Android?

查看:130
本文介绍了如何使透明区域作为贱民在Android的ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我GOOGLE了很多,但找不到任何有用的东西。我有一个复杂的PNG图像,我想让它可触但仅在不透明区域。我设置了触摸监听器,但它派出连我点击了一个透明的区域,这就是我不想要的东西。

I googled a lot but could not find anything useful. I have a complex png image and I want to make it touchable but for its opaque area only. I set a touch listener for it but it dispatched even I clicked on a transparent area, thats what I DON'T want.

推荐答案

由于@Cata说,触摸事件将与整个图像相关联。然而,触摸事件将告诉你在哪里图像中的触摸是,如此可code像这样(忽略检查正确的操作等):

As @Cata says, a touch event will be associated with the whole of the image. However, the touch event will tell you where in the image the touch was, and so can code something like this (ignoring checking for correct action etc):

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean eventHandled = false;

    int x = (int) (event.getX());
    int y = (int) (event.getY());
            if (imageIsOpaque(x,y) {
                 //Do the stuff

                 eventHandled = true;
            }

    return eventHandled;
}

这里的关键是那么imageIsOpaque,你将需要实现,以三种方式之一:

The key here is then imageIsOpaque, which you will need to implement, in one of three ways:


  1. 图片可以很容易段放入不透明和非透明在这种情况下领域:

  1. The image may be easy to segment into areas of opaque and non opaque in which case:

boolean imageIsOpaque(int touchX, int touchY) {
    ArayList<Rect> rectsOfOpaqueness; // You will need to define these ...

    boolean isOpaque = false;
    for (int i=0; i<rectsOfOpaqueness.size() && !isOpaque; i++) {
    if (rectsOfOpaqueness.get(i).contains(touchX, touchY)) {
        isOpaque = true;
        }

    return isOpaque;
}


  • 图片可能不容易处理的方式,在这种情况下,您将需要使用X和Y触摸位置源图像(缩放到它的屏幕大小),并检查是否点是不透明的或没有。编辑:您似乎已经习惯在一个相当巧妙的方法该解决方案在您的评论下面@vinod,所以我会推荐给其他读者检查注释掉以及

  • The image may not be easy to handle that way, in which case you will need to use the x and y touch position to check in the source image (scaled to the size it's on the screen) whether the point is opaque or not. You seem to have used this solution in a rather neat way in your comment to @vinod below, so I would recommend to other readers to check that comment out as well.

    更​​复杂,它可能是你contructing动态图像,并永远不会真正知道它在一个可搜索的方式最终状态。如果是这样的话,你将需要建立布尔的一个单独的二维数组为您塑造形象确定哪些点是不透明的,哪些不是。

    Even more complex, it may be an image that you are contructing on the fly and never really know it's final state in a searchable way. If this is the case, you will need to build up a separate 2D array of booleans as you create the image determining which points are opaque and which aren't.

    这篇关于如何使透明区域作为贱民在Android的ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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