检测图像android上的单击矩形内部

时间:2015-12-02 09:31:26

标签: java android

我有一个包含一些矩形的图像。 我想在触摸矩形内部时检测事件以及如何获取坐标x&是的。

提前感谢您的帮助。

请参阅上面的照片:

enter image description here

2 个答案:

答案 0 :(得分:1)

获取触摸x和y坐标,您可以在Touch上覆盖,然后获取X和Y坐标。

var buildWatcher = chokidar.watch(paths, { ignoreInitial: true });

buildWatcher
    .on('add', function(filePath) {
        gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(filePath) + ' add'));
        gulp.start(key + suffix);
    })
    .on('change', function(filePath) {
        gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(filePath) + ' change'));
        gulp.start(key + suffix);
    })
    .on('unlink', function(filePath) {
        gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(filePath) + ' unlink'));
        gulp.start(key + suffix);
    });

获取触摸颜色

gulp.watch(paths, function(event) {
    gulpUtil.log("File event: " + gulpUtil.colors.cyan(path.basename(event.path) + ' ' + event.type));
    gulp.start(key + suffix);
}).on('error', function(error) {
    gulpUtil.log(error);
});

在getHotspotColor中返回触摸颜色

@Override
public boolean onTouch(View v, MotionEvent ev) {

    boolean handledHere = false;

    final int action = ev.getAction();

    final int evX = (int) ev.getX();
    final int evY = (int) ev.getY();

    switch (action) {
        case MotionEvent.ACTION_DOWN:

            handledHere = true;
            break;

        case MotionEvent.ACTION_UP:

            try {
                InputMethodManager imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
            } catch (Exception e) {
            }

            defineArea(evX, evY);

            handledHere = true;
            break;

        default:
            handledHere = false;
    } // end switch

    return handledHere;
}

你得到了触摸颜色。

答案 1 :(得分:1)

您可以指定可点击区域并拒绝此区域外的点击次数:

MainActivity

// Status Bar Height
final int statusBarId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
final int statusBarHeight = statusBarId > 0 ? this.getResources().getDimensionPixelSize(statusBarId) : 0;

// OnTouchZone
final OnTouchZone onTouchZone = new OnTouchZone(100, 50, 350, 150);

// Image
final ImageView image = new ImageView(this);
image.setImageResource(R.drawable.your_image);
image.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                if (onTouchZone.contains(event.getX(), event.getY() - statusBarHeight)) {
                    // Your action

                    return true;
                }
                break;
        }

        return false;
    }
});

OnTouchZone

public final class OnTouchZone {

    private final int left, top, right, bottom;

    public OnTouchZone(final int left, final int top, final int right, final int bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    public final boolean contains(final int x, final int y) {
            return x > this.left && x < this.right && y > this.top && y < this.bottom;
    }

}