触摸路径android画布上的监听器

时间:2015-06-08 06:30:34

标签: android canvas

我的自定义视图在画布上包含多个路径,需要在同一视图但不同路径上侦听触摸事件。那么我如何才能实现这一点就是我的代码:

All I need to listen path area touched 

这是视图类

public class Hexa extends View implements OnTouchListener {

int displayX;
int displayY;
Path path1;
Path path2;

public Hexa(Context context) {
    super(context);
    initializeDisplay();
    this.setOnTouchListener(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.parseColor("#1b1c20"));
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setDither(true);
    paint.setColor(Color.parseColor("#2a2f35"));
    // Path path ;
    Point a, b, c, d, e;

    // draw upper
    a = new Point(displayX * 5 / 100, 0);
    b = new Point(displayX * 95 / 100, 0);
    c = new Point(displayX * 5 / 100, displayY
            - ((int) displayY * (100 - 30) / 100));
    d = new Point(displayX * 95 / 100, displayY
            - ((int) displayY * (100 - 30) / 100));
    e = new Point(displayX / 2, displayY
            - ((int) displayY * (100 - 40) / 100));

    path1 = new Path();
    path1.setFillType(FillType.EVEN_ODD);
    path1.lineTo(a.x, a.y);
    path1.lineTo(b.x, b.y);
    path1.lineTo(d.x, d.y);
    path1.lineTo(e.x, e.y);
    path1.lineTo(c.x, c.y);
    path1.lineTo(a.x, a.y);
    path1.close();
    canvas.drawPath(path1, paint);

    // draw lower
    a = new Point(displayX * 5 / 100, displayY);
    b = new Point(displayX * 95 / 100, displayY);

    c = new Point(displayX * 5 / 100, displayY
            - ((int) displayY * (100 - 70) / 100));
    d = new Point(displayX * 95 / 100, displayY
            - ((int) displayY * (100 - 70) / 100));
    e = new Point(displayX / 2, displayY
            - ((int) displayY * (100 - 60) / 100));

    path2 = new Path();
    path2.setFillType(FillType.EVEN_ODD);
    path2.lineTo(a.x, a.y);
    path2.lineTo(b.x, b.y);
    path2.lineTo(d.x, d.y);
    path2.lineTo(e.x, e.y);
    path2.lineTo(c.x, c.y);
    path2.lineTo(a.x, a.y);
    path2.close();
    canvas.drawPath(path2, paint);

}

@Override
public boolean onTouch(View view, MotionEvent event) {

    int e=event.getAction();
    switch(e){
    case MotionEvent.ACTION_DOWN:
        //here i need touch listener
        if(area of path1){
            Log.d("PATH1", "AREA TOUCHED");
        }
        else if (area of path2){
            Log.d("PATH2", "AREA TOUCHED");
        }

        break;
    }



    return true;
 }
}

是否有办法在同一视图上侦听多个侦听器。

0 个答案:

没有答案