onscreen calculator improvement

This commit is contained in:
Sergey Solovyev 2012-12-02 16:32:38 +04:00
parent cd025c3a89
commit 85b36f3fdc

View File

@ -215,7 +215,7 @@ public class CalculatorOnscreenView {
}); });
final ImageView onscreenTitleImageView = (ImageView) root.findViewById(R.id.onscreen_title); final ImageView onscreenTitleImageView = (ImageView) root.findViewById(R.id.onscreen_title);
onscreenTitleImageView.setOnTouchListener(new WindowDragTouchListener(wm, root, context.getResources().getDisplayMetrics())); onscreenTitleImageView.setOnTouchListener(new WindowDragTouchListener(wm, root));
initialized = true; initialized = true;
} }
@ -356,6 +356,8 @@ public class CalculatorOnscreenView {
@NotNull @NotNull
private final WindowManager wm; private final WindowManager wm;
private int orientation;
private float x0; private float x0;
private float y0; private float y0;
@ -365,9 +367,9 @@ public class CalculatorOnscreenView {
@NotNull @NotNull
private final View view; private final View view;
private final int displayWidth; private int displayWidth;
private final int displayHeight; private int displayHeight;
/* /*
********************************************************************** **********************************************************************
@ -378,16 +380,18 @@ public class CalculatorOnscreenView {
*/ */
public WindowDragTouchListener(@NotNull WindowManager wm, public WindowDragTouchListener(@NotNull WindowManager wm,
@NotNull View view, @NotNull View view) {
@NotNull DisplayMetrics displayMetrics) {
this.wm = wm; this.wm = wm;
this.view = view; this.view = view;
this.displayWidth = displayMetrics.widthPixels; initDisplayParams();
this.displayHeight = (int) (displayMetrics.heightPixels - 25 * displayMetrics.density);
} }
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
if (orientation != this.wm.getDefaultDisplay().getOrientation()) {
// orientation has changed => we need to check display width/height each time window moved
initDisplayParams();
}
//Log.d(TAG, "Action: " + event.getAction()); //Log.d(TAG, "Action: " + event.getAction());
@ -411,7 +415,7 @@ public class CalculatorOnscreenView {
final float yi = event.getHistoricalY(i); final float yi = event.getHistoricalY(i);
processMove(xi, yi); processMove(xi, yi);
}*/ }*/
processMove(x1, y1, event.getPointerCount()); processMove(x1, y1);
} }
return true; return true;
} }
@ -419,7 +423,17 @@ public class CalculatorOnscreenView {
return false; return false;
} }
private void processMove(float x1, float y1, int pointerCount) { private void initDisplayParams() {
this.orientation = this.wm.getDefaultDisplay().getOrientation();
final DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
this.displayWidth = displayMetrics.widthPixels;
this.displayHeight = (int) (displayMetrics.heightPixels - 25 * displayMetrics.density);
}
private void processMove(float x1, float y1) {
final float Δx = x1 - x0; final float Δx = x1 - x0;
final float Δy = y1 - y0; final float Δy = y1 - y0;
@ -430,24 +444,28 @@ public class CalculatorOnscreenView {
boolean yInBounds = isDistanceInBounds(Δy); boolean yInBounds = isDistanceInBounds(Δy);
if (xInBounds || yInBounds) { if (xInBounds || yInBounds) {
if (pointerCount == 1) { if (xInBounds) {
params.x = (int) (params.x + Δx); params.x = (int) (params.x + Δx);
}
if (yInBounds) {
params.y = (int) (params.y + Δy); params.y = (int) (params.y + Δy);
} }
params.x = (int) (params.x - params.width * 0.5f);
params.y = (int) (params.y - params.height * 0.5f);
// keep window inside edges
params.x = Math.min(Math.max(params.x, 0), displayWidth - params.width); params.x = Math.min(Math.max(params.x, 0), displayWidth - params.width);
params.y = Math.min(Math.max(params.y, 0), displayHeight - params.height); params.y = Math.min(Math.max(params.y, 0), displayHeight - params.height);
wm.updateViewLayout(view, params); wm.updateViewLayout(view, params);
if (xInBounds) {
x0 = x1; x0 = x1;
}
if (yInBounds) {
y0 = y1; y0 = y1;
} }
} }
}
private boolean isDistanceInBounds(float δx) { private boolean isDistanceInBounds(float δx) {
δx = Math.abs(δx); δx = Math.abs(δx);