1

GestureDetector用来识别触摸。我有两个Relative layouts相同的布局大小。我试图在一个布局上绘制图像 A 并在另一个布局上移动图像 B(使用创建onDraw)。当我移动我的图像 BIGestureDetector用于识别时触摸。当我移动我的图像 B 时,图像 A(view)onDraw()方法被多次调用。请帮助我避免onDraw()多次调用。这是我的代码

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

    }
}

}

TouchExampleView.java

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

}

我的示例 xml

<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>

如果我使用两个不同大小和位置的相对布局,那么它工作正常,onDraw()不会被多次调用

4

1 回答 1

4

After trying so much I found this answer to my question.setDrawingCacheEnabled(true) prevents calling onDraw() multiple times.Its working for me.I hope I am going right.

   View circleView;
   r2 = (RelativeLayout) findViewById(R.id.r2);
   circleView= r2;         
   circleView.setDrawingCacheEnabled(true);
于 2013-05-20T11:46:23.220 回答