簡體   English   中英

檢測圖像Android上的矩形內單擊

[英]Detect click inside rectangle on image android

我有一個包含一些矩形的圖像。 我想在矩形內部觸摸時檢測事件,以及如何獲取坐標x和y。

在此先感謝您的幫助。

請看上面的照片:

在此處輸入圖片說明

要獲取觸摸的x和y坐標,您可以在Touch上覆蓋,然后獲取X和Y坐標。

@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;
}

為了獲得觸摸顏色

 int touchColor = getHotspotColor(R.id.image, evX, evY);

在getHotspotColor中返回觸摸的顏色

public int getHotspotColor(int hotspotId, int x, int y) {
    if (imgHome == null) {
        if (IConstants.debug)
            Loger.d("ImageAreasActivity", "Hot spot image not found");
        return 0;
    } else {
        imgHome.setDrawingCacheEnabled(true);
        Bitmap hotspots = Bitmap.createBitmap(imgHome.getDrawingCache());
        if (hotspots == null || ((x < 1 || y < 1) || (x > hotspots.getWidth() || y > hotspots.getHeight()))) {
            if (IConstants.debug)
                Loger.d("ImageAreasActivity", "Hot spot bitmap was not created");
            return 0;
        } else {
            imgHome.setDrawingCacheEnabled(false);
            return hotspots.getPixel(x, y);
        }
    }
}

您得到觸摸顏色。

您可以指定一個可點擊區域,並拒絕該區域以外的點擊:

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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM