onscreen calculator improvement

This commit is contained in:
Sergey Solovyev 2012-12-01 22:20:53 +04:00
parent e7fe6a3c36
commit 7a4d4485de
2 changed files with 29 additions and 25 deletions

View File

@ -86,11 +86,6 @@
<item name="android:layout_width">match_parent</item> <item name="android:layout_width">match_parent</item>
</style> </style>
<style name="cpp_onscreen_main_layout_style" parent="cpp_default_main_layout_style">
<item name="android:layout_height">400dp</item>
<item name="android:layout_width">250dp</item>
</style>
<style name="cpp_default_actionbar_style" parent="@style/Widget.Sherlock.ActionBar"> <style name="cpp_default_actionbar_style" parent="@style/Widget.Sherlock.ActionBar">
<item name="background">@drawable/default_abs__ab_transparent_dark_holo</item> <item name="background">@drawable/default_abs__ab_transparent_dark_holo</item>
<item name="android:background">@drawable/default_abs__ab_transparent_dark_holo</item> <item name="android:background">@drawable/default_abs__ab_transparent_dark_holo</item>

View File

@ -2,7 +2,9 @@ package org.solovyev.android.calculator.onscreen;
import android.content.Context; import android.content.Context;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
@ -10,7 +12,6 @@ import android.widget.ImageView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.CalculatorButton;
/** /**
* User: serso * User: serso
@ -214,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)); onscreenTitleImageView.setOnTouchListener(new WindowDragTouchListener(wm, root, context.getResources().getDisplayMetrics()));
initialized = true; initialized = true;
} }
@ -248,6 +249,8 @@ public class CalculatorOnscreenView {
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT); PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
wm.addView(root, params); wm.addView(root, params);
attached = true; attached = true;
} }
@ -362,6 +365,10 @@ public class CalculatorOnscreenView {
@NotNull @NotNull
private final View view; private final View view;
private final int displayWidth;
private final int displayHeight;
/* /*
********************************************************************** **********************************************************************
* *
@ -371,9 +378,12 @@ 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;
this.displayHeight = (int) (displayMetrics.heightPixels - 25 * displayMetrics.density);
} }
@Override @Override
@ -381,8 +391,8 @@ public class CalculatorOnscreenView {
//Log.d(TAG, "Action: " + event.getAction()); //Log.d(TAG, "Action: " + event.getAction());
final float x1 = event.getX(); final float x1 = event.getRawX();
final float y1 = event.getY(); final float y1 = event.getRawY();
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
@ -396,12 +406,12 @@ public class CalculatorOnscreenView {
if ( currentTime - time >= TIME_EPS ) { if ( currentTime - time >= TIME_EPS ) {
time = currentTime; time = currentTime;
for (int i = 0; i < event.getHistorySize(); i++) { /*for (int i = 0; i < event.getHistorySize(); i++) {
final float xi = event.getHistoricalX(i); final float xi = event.getHistoricalX(i);
final float yi = event.getHistoricalY(i); final float yi = event.getHistoricalY(i);
processMove(xi, yi); processMove(xi, yi);
} }*/
processMove(x1, y1); processMove(x1, y1, event.getPointerCount());
} }
return true; return true;
} }
@ -409,7 +419,7 @@ public class CalculatorOnscreenView {
return false; return false;
} }
private void processMove(float x1, float y1) { private void processMove(float x1, float y1, int pointerCount) {
final float Δx = x1 - x0; final float Δx = x1 - x0;
final float Δy = y1 - y0; final float Δy = y1 - y0;
@ -420,25 +430,24 @@ public class CalculatorOnscreenView {
boolean yInBounds = isDistanceInBounds(Δy); boolean yInBounds = isDistanceInBounds(Δy);
if (xInBounds || yInBounds) { if (xInBounds || yInBounds) {
if (xInBounds) { if (pointerCount == 1) {
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.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);