如何将eventlistener附加到我的drawable行

时间:2014-03-28 05:16:15

标签: android line drawable clickable

我知道我的可绘制线条不是对象,但有任何方法可以将线条点击。

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

这是我绘制线条的代码。

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

检查一下,我使用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
}