多个OnTouchListeners

时间:2013-10-20 10:18:32

标签: android android-view ontouchlistener

我制作了一个具有OnTouchEvent(MotionEvent event)方法的自定义视图。这使得视图在按下时会改变颜色。

我还为我的活动实施了OnTouchListener,然后通过执行此操作注册了该视图view.setOnTouchListener(this);这会产生一个问题,即只有活动的OnTouchListener有效。< / p>

有解决方法吗?

这是我在尝试评论中给出的链接后的代码。对不起!!

我的自定义视图:

public class DotView extends View implements OnTouchListener {

    int RADIUS = 30;
    int prevRADIUS;

    private Paint paint = new Paint();

    float mTranslateX;
    float mTranslateY;

    int dotX, dotY, color;

    private Activity context = null;

    public void ReservedCanvas(Activity context) {
        super(context); 
        this.context = context;

        this.paint = new Paint();

        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setAntiAlias(true);
    }

    public DotView(Context context, AttributeSet attrs) {
        super(context, attrs);

        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setColor(Color.BLACK);

    }

    public void setColor(int color) {
        paint.setColor(color);
        invalidate();
    }

    public int getColor() {
        return paint.getColor();
    }

    public void setRadius(int radius) {
        RADIUS = radius;
        invalidate();
    }

    public int getRadius() {
        return RADIUS;
    }

    public void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        canvas.save();
        canvas.translate(mTranslateX, mTranslateY);
        canvas.drawCircle(0, 0, RADIUS, paint);
        canvas.restore();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int dia = RADIUS * 4; 
        int w = resolveSize(dia, widthMeasureSpec);
        int h = resolveSize(dia, heightMeasureSpec);
        setMeasuredDimension(w, h);
        float radius = Math.min(w, h) / 2F;
        mTranslateX = radius;
        mTranslateY = radius;
    }


    private void setOnTouchFeedback(Boolean bool) {
        // TODO Check this works

        if (bool == true) {

        int rad = getRadius();
        prevRADIUS = rad;
        float touchedRad = (float) (rad * 1.4);
        int rounded = Math.round(touchedRad);
        setRadius(rounded);
        }


    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;

        switch (action) {
        case MotionEvent.ACTION_DOWN: {
            this.pointerPressed(event);
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            this.pointerDragged(event);
            break;
        }
        case MotionEvent.ACTION_UP: {
            this.pointerReleased();
            break;
        }
        }

        return true;
    }

    protected void pointerPressed(MotionEvent event) {
        // you can save touch point here!

        setOnTouchFeedback(true);
    }

    protected void pointerDragged(MotionEvent event) {
        // for get x ==> eveevent.getX()
        // for get y ==> eveevent.getY()

        this.repaint();
    }

    protected void pointerReleased() {
    }

    public void repaint() {
        this.invalidate();
    }
}

1 个答案:

答案 0 :(得分:2)

从onTouchListener的文档 - http://developer.android.com/reference/android/view/View.OnTouchListener.html我认为监听器在显示toast后需要返回false,以便调用View的onTouchMethod。

你尝试过吗?