带有onDraw()的手势检测器多次调用

时间:2013-05-20 06:57:07

标签: android gesture

我正在使用GestureDetector来识别触摸。我有两个Relative layouts相同的布局大小。我正在尝试在一个布局上绘制图像A并移动图像B(使用{{创建) 1}})在另一个布局上。虽然我移动我的图像BI使用onDraw来识别触摸。当我移动图像B时,多次调用图像A(视图)GestureDetector方法。请帮我避免多次拨打onDraw()。这是我的代码

onDraw()

TouchExampleView.java

public class TouchExampleActivity extends Activity {

RelativeLayout r1;
RelativeLayout r2;
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    context = TouchExampleActivity.this;
    r1 = (RelativeLayout) findViewById(R.id.r1);
    r2 = (RelativeLayout) findViewById(R.id.r2);

    TouchExampleView view = new TouchExampleView(this);

    DrawCircle drawCircle = new DrawCircle(context);

    r1.addView(view);
    r2.addView(drawCircle);

}

class DrawCircle extends View {

    Paint p;

    public DrawCircle(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        p = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        Log.v("Touch", "Called Draw Circle");
        p.setColor(Color.BLUE);
        canvas.drawCircle(60, 50, 50, p);

    }
}

}

我的样本xml

public class TouchExampleView extends View {
private Drawable mIcon;
private float mPosX;
private float mPosY;
static boolean fStartDrag = false;
static boolean fDrag = false;
private float ypos = 40;
private float xpos = 30;
public static float sx, sy;

private VersionedGestureDetector mDetector;
private float mScaleFactor = 1.f;

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

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

public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mIcon = context.getResources().getDrawable(R.drawable.icon);
    mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());

    mDetector = VersionedGestureDetector.newInstance(context, new GestureCallback());
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    mDetector.onTouchEvent(ev);
    return true;
}

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

    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setStrokeWidth(5);

    canvas.save();
    canvas.translate(mPosX, 0);
    canvas.scale(mScaleFactor, mScaleFactor);

    canvas.drawRect(0, 0, 30, 30, paint);
    canvas.restore();
}

private class GestureCallback implements VersionedGestureDetector.OnGestureListener {
    public void onDrag(float dx, float dy) {



        if ((dx == -1) && (dy == -1)) {

            fStartDrag = true;
            fDrag = false;
        }

        else if (fStartDrag) {

            if (dy >= mPosY && dy <= ypos + mPosY && dx >= mPosX && dx <= mPosX + xpos) {

                fDrag = true;
                fStartDrag = false;
            }
        } else if (fDrag) {
            System.out.println("fDrag");
            if (mPosX < 3)
                mPosX = 3;
            else if (mPosX > 400)
                mPosX = 400;
            else

                mPosX = dx;

            // mPosY = dy;

            postInvalidate();
        }
    }

    public void onScale(float scaleFactor) {
        mScaleFactor *= scaleFactor;


        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));


        postInvalidate();
    }
}

}

如果我使用两个不同大小和位置的相对布局,那么它可以正常工作,<RelativeLayout android1:id="@+id/r1" android1:layout_width="620dip" android1:layout_height="320dip" android1:layout_alignParentBottom="true" android1:layout_alignParentLeft="true" android1:layout_marginBottom="94dp" > </RelativeLayout> <RelativeLayout android1:id="@+id/r2" android1:layout_width="620dip" android1:layout_height="320dip" android1:layout_alignParentBottom="true" android1:layout_alignParentLeft="true" android1:layout_marginBottom="94dp" > </RelativeLayout> 不会被多次调用

1 个答案:

答案 0 :(得分:4)

尝试了这么多后,我发现了我的问题的答案。setDrawingCacheEnabled(true)无法多次调用onDraw()。它为我工作。我希望我走对了。

   View circleView;
   r2 = (RelativeLayout) findViewById(R.id.r2);
   circleView= r2;         
   circleView.setDrawingCacheEnabled(true);