DirectionDragImageButton

This commit is contained in:
serso 2016-03-08 23:08:05 +01:00
parent 56d1d2b249
commit 675626bc77
13 changed files with 172 additions and 51 deletions

View File

@ -12,33 +12,18 @@ import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.solovyev.android.calculator.ActivityLauncher;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.Keyboard;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.android.calculator.PreferredPreferences;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.views.Adjuster;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragListener;
import org.solovyev.android.views.dragbutton.DragButton;
import org.solovyev.android.views.dragbutton.DragDirection;
import org.solovyev.android.views.dragbutton.DragEvent;
import java.util.ArrayList;
import java.util.List;
import org.solovyev.android.views.dragbutton.*;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.view.HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING;
import static android.view.HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING;
import static android.view.HapticFeedbackConstants.KEYBOARD_TAP;
import static android.view.HapticFeedbackConstants.*;
import static org.solovyev.android.calculator.App.cast;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile;
@ -53,7 +38,7 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
public static final float IMAGE_SCALE_ERASE = 0.4f;
@NonNull
private final List<DragButton> dragButtons = new ArrayList<>();
private final List<DragView> dragButtons = new ArrayList<>();
@NonNull
protected final DirectionDragListener listener;
@Inject
@ -125,6 +110,17 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
button.setOnClickListener(this);
}
protected final void prepareButton(@Nullable DirectionDragImageButton button) {
if (button == null) {
return;
}
dragButtons.add(button);
button.setVibrateOnDrag(keyboard.isVibrateOnKeypress());
prepareButton((ImageView) button);
button.setOnDragListener(listener);
button.setTypeface(typeface);
}
protected final void prepareButton(@Nullable DirectionDragButton button) {
if (button == null) {
return;
@ -173,7 +169,7 @@ public abstract class BaseKeyboardUi implements SharedPreferences.OnSharedPrefer
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
if (Preferences.Gui.vibrateOnKeypress.isSameKey(key)) {
final boolean vibrate = Preferences.Gui.vibrateOnKeypress.getPreference(preferences);
for (DragButton dragButton : dragButtons) {
for (DragView dragButton : dragButtons) {
dragButton.setVibrateOnDrag(vibrate);
}
}

View File

@ -10,7 +10,10 @@ import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.AngleUnit;
import jscl.NumeralBase;
import org.solovyev.android.calculator.ActivityLauncher;
import org.solovyev.android.calculator.Engine;
import org.solovyev.android.calculator.R;
@ -18,23 +21,15 @@ import org.solovyev.android.calculator.buttons.CppSpecialButton;
import org.solovyev.android.calculator.history.History;
import org.solovyev.android.calculator.view.AngleUnitsButton;
import org.solovyev.android.views.dragbutton.DirectionDragButton;
import org.solovyev.android.views.dragbutton.DirectionDragImageButton;
import org.solovyev.android.views.dragbutton.DragButton;
import org.solovyev.android.views.dragbutton.DragDirection;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import butterknife.Bind;
import butterknife.ButterKnife;
import jscl.AngleUnit;
import jscl.NumeralBase;
import static org.solovyev.android.calculator.Engine.Preferences.angleUnit;
import static org.solovyev.android.calculator.Engine.Preferences.multiplicationSign;
import static org.solovyev.android.calculator.Engine.Preferences.numeralBase;
import static org.solovyev.android.views.dragbutton.DragDirection.down;
import static org.solovyev.android.views.dragbutton.DragDirection.left;
import static org.solovyev.android.views.dragbutton.DragDirection.up;
import static org.solovyev.android.calculator.Engine.Preferences.*;
import static org.solovyev.android.views.dragbutton.DragDirection.*;
public class KeyboardUi extends BaseKeyboardUi {
@ -74,7 +69,7 @@ public class KeyboardUi extends BaseKeyboardUi {
@Bind(R.id.cpp_button_functions)
DirectionDragButton functionsButton;
@Bind(R.id.cpp_button_history)
DirectionDragButton historyButton;
DirectionDragImageButton historyButton;
@Bind(R.id.cpp_button_multiplication)
DirectionDragButton multiplicationButton;
@Bind(R.id.cpp_button_plus)

View File

@ -0,0 +1,63 @@
package org.solovyev.android.views.dragbutton;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;
import javax.annotation.Nonnull;
public class DirectionDragImageButton extends DragImageButton {
private final DirectionTextView textView = new DirectionTextView();
private final TextPaint baseTextPaint = new TextPaint();
public DirectionDragImageButton(Context context) {
super(context);
init(null);
}
public DirectionDragImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public DirectionDragImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
public DirectionDragImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init(@Nullable AttributeSet attrs) {
final TextView view = new TextView(getContext(), attrs);
baseTextPaint.set(view.getPaint());
textView.init(this, attrs, baseTextPaint);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
textView.draw(canvas);
}
@Nonnull
protected DirectionTextView.Text getText(@NonNull DragDirection direction) {
return textView.getText(direction);
}
public void setTypeface(@Nonnull Typeface newTypeface) {
final Typeface oldTypeface = baseTextPaint.getTypeface();
if(oldTypeface == newTypeface) {
return;
}
baseTextPaint.setTypeface(newTypeface);
textView.setBaseTextPaint(baseTextPaint);
}
}

View File

@ -12,9 +12,7 @@ import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import com.google.common.base.Strings;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.R;
@ -178,25 +176,32 @@ public class DirectionTextView {
private void calculatePosition() {
paint.getTextBounds(value, 0, value.length(), bounds);
final int paddingLeft = padding;
final int paddingRight = padding;
final int paddingTop = padding;
final int paddingBottom = padding;
final int verticalPaddings = 0;
switch (direction) {
case up:
case down:
position.x = view.getWidth() - view.getPaddingLeft() - padding - bounds.width();
position.x = view.getWidth() - paddingLeft - bounds.width();
if (direction == DragDirection.up) {
position.y = view.getPaddingTop() + padding + bounds.height();
position.y = paddingTop + bounds.height();
} else {
position.y = view.getHeight() - view.getPaddingBottom() - padding;
position.y = view.getHeight() - paddingBottom;
}
break;
case left:
case right:
if (direction == DragDirection.left) {
position.x = view.getPaddingLeft() + padding;
position.x = paddingLeft;
} else {
position.x = view.getWidth() - view.getPaddingLeft() - padding - bounds.width();
position.x = view.getWidth() - paddingRight - bounds.width();
}
final int availableHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom();
position.y = view.getPaddingTop() + padding + availableHeight / 2 + bounds.height() / 2;
final int availableHeight = view.getHeight() - verticalPaddings;
position.y = paddingTop + availableHeight / 2 + bounds.height() / 2;
break;
}
}

View File

@ -9,7 +9,7 @@ import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class DragButton extends Button {
public class DragButton extends Button implements DragView {
@NonNull
private final DragGestureDetector dragDetector = new DragGestureDetector(this);
@ -41,10 +41,12 @@ public class DragButton extends Button {
return super.onTouchEvent(event);
}
@Override
public void setOnDragListener(@Nullable DragListener listener) {
dragDetector.setListener(listener);
}
@Override
public void setVibrateOnDrag(boolean vibrateOnDrag) {
dragDetector.setVibrateOnDrag(vibrateOnDrag);
}

View File

@ -0,0 +1,52 @@
package org.solovyev.android.views.dragbutton;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageButton;
public class DragImageButton extends ImageButton implements DragView {
@NonNull
private final DragGestureDetector dragDetector = new DragGestureDetector(this);
public DragImageButton(Context context) {
super(context);
}
public DragImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DragImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public DragImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (dragDetector.onTouchEvent(event)) {
final MotionEvent cancelEvent = DragGestureDetector.makeCancelEvent(event);
super.onTouchEvent(cancelEvent);
cancelEvent.recycle();
return true;
}
return super.onTouchEvent(event);
}
public void setOnDragListener(@Nullable DragListener listener) {
dragDetector.setListener(listener);
}
public void setVibrateOnDrag(boolean vibrateOnDrag) {
dragDetector.setVibrateOnDrag(vibrateOnDrag);
}
}

View File

@ -0,0 +1,9 @@
package org.solovyev.android.views.dragbutton;
import android.support.annotation.Nullable;
public interface DragView {
void setOnDragListener(@Nullable DragListener listener);
void setVibrateOnDrag(boolean vibrateOnDrag);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 870 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 604 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -21,12 +21,11 @@
~ Email: se.solovyev@gmail.com
~ Site: http://se.solovyev.org
-->
<org.solovyev.android.views.dragbutton.DirectionDragButton
<org.solovyev.android.views.dragbutton.DirectionDragImageButton
xmlns:a="http://schemas.android.com/apk/res/android"
xmlns:c="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
a:id="@id/cpp_button_history"
style="?attr/cpp_button_style_control"
a:text="@string/c_history_button"
a:textStyle="bold"
c:directionTextDown="@string/cpp_kb_redo"
c:directionTextUp="@string/cpp_kb_undo" />
a:src="@drawable/ic_history_white_48dp"
app:directionTextDown="@string/cpp_kb_redo"
app:directionTextUp="@string/cpp_kb_undo" />