0
我有一個代碼,用戶可以在屏幕上移動一個透明的矩形。移動畫布矩形觸摸問題(矩形中心)
我面臨的問題是,矩形當我拖動從左上角移動作爲支點,而我希望它從中心樞軸
移動實施例
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
X = (int) ev.getX();
Y = (int) ev.getY();
invalidate();
break;
}
case MotionEvent.ACTION_MOVE: {
X = (int) ev.getX();
Y = (int) ev.getY();
invalidate();
break;
}
case MotionEvent.ACTION_UP:
break;
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
// draw background
canvas.drawBitmap(bgr, 0, 0, null);
// copy the default overlay into temporary overlay and punch a hole
// in it
c2.drawBitmap(overlayDefault, 0, 0, null); // exclude this line to
// show all as you draw
c2.drawRoundRect(new RectF(X, Y, X+350, Y+350), (float)20.0, (float)20.0, pTouch);
// draw the overlay over the background
canvas.drawBitmap(overlay, 0, 0, null);
}
因此,從X/Y中減去矩形寬度/高度的一半。 – 2014-09-05 02:33:18