如何将eventlistener附加到可绘制的线条上



我知道我画的线不是对象,但有没有办法把线变成可点击的?

我不能使用视图,因为会有太多行。提前感谢。

这是我画线的代码。

public class Draw_can extends View {
    Paint paint_obj;
    public Draw_can(Context context) {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        paint_obj = new Paint();
        draw_lines(canvas);
    }   
    public void draw_lines(Canvas canvas) {
        int width = 100;
        paint_obj.setStrokeWidth(5);
        paint_obj.setColor(Color.GRAY);     
        for (int j = 1; j < 8; j++) {
            for (int i = 1; i <= 8; i++) {
                canvas.drawLine(50*j, 50 * i, 50*j+50, 50 * i, paint_obj);                              
            }
        }
        for (int j = 1; j < 8; j++) {
            for (int i = 1; i <= 8; i++) {
                canvas.drawLine(50 * i , 50*j, 50 * i , 50*j+50, paint_obj);                
            }
        }               
    }   
}

检查这个,我使用onTouchEvent():

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    tx = event.getX();
    ty = event.getY();
    int action = event.getAction();
    switch(action){
        case MotionEvent.ACTION_DOWN:
        tx = event.getX();
        ty = event.getY();

        //something like this

        if(ty==line.y&&tx>=line.x&&tx<=line.x+line.width        
            break;
        case MotionEvent.ACTION_MOVE:
            tx = event.getX();
            ty = event.getY();
            break;
        case MotionEvent.ACTION_UP:
                //  ammo.touchRelease(tx,ty);
                break;
        case MotionEvent.ACTION_CANCEL:
                break;
        case MotionEvent.ACTION_OUTSIDE:
                break;
        default:
    }
    return true; //processed
}

相关内容

  • 没有找到相关文章

最新更新