使用缩放功能时,Android App按钮单击事件问题

时间:2012-03-03 15:30:16

标签: android android-layout

我要求我有一个图像,在图像上点击特定区域的区域很少,它会打开另一个图像。

我在这些区域创建了透明按钮。我正在使用ScaleGestureDetector进行捏缩放和拖动图像功能。现在我的问题是onClick事件不适用于按钮,而在ACTION_UP事件中,如果点击按钮或其他地方,我无法获得。

请帮我在图像上实现捏拉缩放和按钮点击事件。

以下是我正在使用的代码

public class TouchView extends View implements OnClickListener {
private static final int INVALID_POINTER_ID = -1;

private final Drawable mIcon;
LinearLayout l1;
LinearLayout l2;
private final Button btn1;
private final Button btn2;
private final Button btn3;
private float mPosX;
private float mPosY;

private float mLastTouchX;
private float mLastTouchY;
private int mActivePointerId = INVALID_POINTER_ID;

private final ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;

public TouchView(Context context) {
    this(context, null, 0);
}

public TouchView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public TouchView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mIcon = context.getResources().getDrawable(R.drawable.park);
    mIcon.setBounds(0, 0, 1200, 720);
    l1 = new LinearLayout(context);
    l2 = new LinearLayout(context);
    l1.setLeft(110);
    l1.setTop(280);
    l1.setRight(600);
    l1.setBottom(600);
    l1.setRotation(-25);
    l2.setLeft(350);
    l2.setTop(780);
    l2.setRight(600);
    l2.setBottom(960);
    l2.setRotation(-15);
    btn1 = new Button(context);
    btn1.layout(160, 148, 400, 240);
    btn1.setText("BTN1");
    btn1.setRotation(-20);
    btn1.setOnClickListener(this);

    btn2 = new Button(context);
    btn2.layout(380, 490, 640, 600);
    btn2.setRotation(-55);
    btn2.setText("BTN2");
    btn3 = new Button(context);
    btn3.layout(300, 300, 500, 600);
    btn3.setPadding(300, 300, 500, 600);
    btn3.setText("BTN3");
    btn3.setRotation(-12);
    l1.addView(btn1);
    l2.addView(btn2);

    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();

        mLastTouchX = x;
        mLastTouchY = y;
        mActivePointerId = ev.getPointerId(0);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        final int pointerIndex = ev.findPointerIndex(mActivePointerId);
        final float x = ev.getX(pointerIndex);
        final float y = ev.getY(pointerIndex);

        // Only move if the ScaleGestureDetector isn't processing a gesture.
        if (!mScaleDetector.isInProgress()) {
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;

            mPosX += dx;
            mPosY += dy;

            invalidate();
        }

        mLastTouchX = x;
        mLastTouchY = y;

        break;
    }

    case MotionEvent.ACTION_UP: {
        mActivePointerId = INVALID_POINTER_ID;
        Log.d("ImageButton1:", "clicked Event" + this.getId());
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        mActivePointerId = INVALID_POINTER_ID;
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = ev.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            // This was our active pointer going up. Choose a new
            // active pointer and adjust accordingly.
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastTouchX = ev.getX(newPointerIndex);
            mLastTouchY = ev.getY(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
        }
        break;
    }
    }

    return true;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.translate(mPosX, mPosY);
    canvas.scale(mScaleFactor, mScaleFactor);
    mIcon.draw(canvas);
    l1.draw(canvas);
    l2.draw(canvas);
    // btn2.draw(canvas);
    // btn3.draw(canvas);
    canvas.restore();
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

@Override
public void onClick(View v) {
    Log.d("ImageButton1:", "clicked on " + v.getId());
    Toast.makeText(TouchView.this.getContext(),
            "Inside Available to View ", Toast.LENGTH_SHORT).show();

}
}

0 个答案:

没有答案