矩形触摸android

时间:2016-11-30 09:40:19

标签: android canvas

我创建了一个名为Rectangle的新类,它包含以下代码:

undefined is not a function

我希望从中创建对象并使用canvas.drawRect()绘制它,就像打击代码一样:

Choose File    name=file    C:\\Users\\John\\Test.txt

但是当我使用吹码来检测矩形对象上的触摸事件时,它不起作用:

package com.example.komeil.speedcolor;

/**
 * Created by Komeil on 20/11/2016.
 */

public class Rectangle extends Shape {
    private int left;
    private int top;
    private int right;
    private int bottom;
public Rectangle(String name,int color,int left,int top,int right,int bottom)
{
    super(name,color);
    setLeft(left);
    setTop(top);
    setRight(right);
    setBottom(bottom);
}


public void setLeft(int left)
{
    this.left = left;
}

public int getLeft(){
    return left;
}

public int getTop()
{
    return top;
}

public void setTop(int top){
    this.top = top;
}

public void setRight(int right){
    this.right = right;
}

public int getRight(){
    return right;
}

public void setBottom(int bottom){
    this.bottom = bottom;
}

public int getBottom(){
    return bottom;
}
}

那么如何检测矩形触摸?

1 个答案:

答案 0 :(得分:1)

将yourCustomView更改为viewName。

请相应更改

yourCustomView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){

            int x = event.getX();
            int y = event.getY();
            if(x > rectLeftX && x < rectRightX && y > rectBottomY && y < rectTopY){
                /* Trigger your action here */
            }

            }
            return true;
        }
    });

检查一下,你会得到一些提示Detect touch