Skip to content

Commit

Permalink
"Auto"
Browse files Browse the repository at this point in the history
  • Loading branch information
Beppi Menozzi authored and Beppi Menozzi committed Jul 16, 2017
1 parent 6c7a01a commit e20cc48
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -37,7 +37,7 @@ In your Application's or Module's build.gradle file:

dependencies {
...
compile 'com.github.BeppiMenozzi:Knob:1.8.0'
compile 'com.github.BeppiMenozzi:Knob:1.9.0'
...
}

Expand Down Expand Up @@ -92,7 +92,7 @@ Here is the list of the xml attributes that can be used to customize the View. A
<tr><th colspan="2">General</th></tr>
<tr><td><b>kNumberOfStates</b></td><td>Number of possible states. States are numbered from 0 to n-1. This number can be changed runtime, and the indicator will adjust its position accordingly.</td></tr>
<tr><td><b>kDefaultState</b></td><td>The starting state of the knob.</td></tr>
<tr><td><b>kClickBehaviour</b></td><td>What is expected when the knob is clicked. Next value, previous value, reset to default value, or let the user select with a popup menu. Default is next.</td></tr>
<tr><td><b>kClickBehaviour</b></td><td>What is expected when the knob is clicked. Next value, previous value, reset to default value, let the user select with a popup menu, define a custom listener. Default is next. Warning: the popup menu is available only with Compat. Warning: the customer listener (a Runnable) should be defined runtime, with <i>setUserBehaviour()</i></td></tr>
<tr><td><b>kAnimation</b></td><td>Enable / disable indicator's animation.</td></tr>
<tr><td><b>kAnimationSpeed</b></td><td>Parameter "speed" applied to the spring physical model for the indicator's animation.</td></tr>
<tr><td><b>kAnimationBounciness</b></td><td>Parameter "bounciness" applied to the spring physical model for the indicator's animation.</td></tr>
Expand Down Expand Up @@ -144,6 +144,9 @@ Here is the list of the xml attributes that can be used to customize the View. A
You don't have to add any dependency.
Internally, this library uses my other <a href="https://github.com/BeppiMenozzi/BalloonPopup">Balloon Popup library</a> to display... guess... balloon popups.

### New in 1.9.0
* Added 'user' click behaviour

### New in 1.8.0
* Fixed: default swipe direction (thanks for pull request)
* Added different onClick behaviours, included the somewhere asked popup menu
Expand Down
4 changes: 2 additions & 2 deletions knoblibrary/build.gradle
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 25
versionCode 12
versionName "1.8.0"
versionCode 13
versionName "1.9.0"
}
buildTypes {
release {
Expand Down
79 changes: 48 additions & 31 deletions knoblibrary/src/main/java/it/beppi/knoblibrary/Knob.java
Expand Up @@ -44,7 +44,11 @@ public class Knob extends View {
public static final int ONCLICK_PREV = 2;
public static final int ONCLICK_RESET = 3;
public static final int ONCLICK_MENU = 4;
public static final int ONCLICK_USER = 5;

public static final int BALLONANIMATION_POP = 0;
public static final int BALLONANIMATION_SCALE = 1;
public static final int BALLONANIMATION_FADE = 2;

// constructors
public Knob(Context context) {
Expand Down Expand Up @@ -325,10 +329,11 @@ BalloonPopup.BalloonAnimation getBalloonAnimation() {
private int balloonValuesTimeToLive = 400;
private float balloonValuesRelativePosition = 1.3f;
private float balloonValuesTextSize = 9;
private int balloonValuesAnimation = 0;
private int balloonValuesAnimation = BALLONANIMATION_POP;
private CharSequence[] balloonValuesArray = null;
private boolean balloonValuesSlightlyTransparent = true;
private int clickBehaviour = 1; // next
private int clickBehaviour = ONCLICK_NEXT; // next
private Runnable userRunnable = null;


// initialize
Expand Down Expand Up @@ -434,29 +439,30 @@ void loadAttributes(AttributeSet attrs) {
typedArray.recycle();
}
int swipeAttrToInt(String s) {
if (s == null) return 4;
if (s.equals("0")) return 0;
else if (s.equals("1")) return 1; // vertical
else if (s.equals("2")) return 2; // horizontal
else if (s.equals("3")) return 3; // both
else if (s.equals("4")) return 4; // default - circular
else return 4;
if (s == null) return SWIPEDIRECTION_CIRCULAR;
if (s.equals("0")) return SWIPEDIRECTION_NONE;
else if (s.equals("1")) return SWIPEDIRECTION_VERTICAL; // vertical
else if (s.equals("2")) return SWIPEDIRECTION_HORIZONTAL; // horizontal
else if (s.equals("3")) return SWIPEDIRECTION_HORIZONTALVERTICAL; // both
else if (s.equals("4")) return SWIPEDIRECTION_CIRCULAR; // default - circular
else return SWIPEDIRECTION_CIRCULAR;
}
int clickAttrToInt(String s) {
if (s == null) return 1;
if (s.equals("0")) return 0;
else if (s.equals("1")) return 1; // default - next
else if (s.equals("2")) return 2; // prev
else if (s.equals("3")) return 3; // reset
else if (s.equals("4")) return 4; // menu
else return 1;
if (s == null) return ONCLICK_NEXT;
if (s.equals("0")) return ONCLICK_NONE;
else if (s.equals("1")) return ONCLICK_NEXT; // default - next
else if (s.equals("2")) return ONCLICK_PREV; // prev
else if (s.equals("3")) return ONCLICK_RESET; // reset
else if (s.equals("4")) return ONCLICK_MENU; // menu
else if (s.equals("5")) return ONCLICK_USER; // menu
else return ONCLICK_NEXT;
}
int balloonAnimationAttrToInt(String s) {
if (s == null) return 0;
if (s.equals("0")) return 0; // pop
else if (s.equals("1")) return 1; // scale
else if (s.equals("2")) return 2; // fade
else return 0;
if (s == null) return BALLONANIMATION_POP;
if (s.equals("0")) return BALLONANIMATION_POP; // pop
else if (s.equals("1")) return BALLONANIMATION_SCALE; // scale
else if (s.equals("2")) return BALLONANIMATION_FADE; // fade
else return BALLONANIMATION_POP;
}

private void disallowParentToHandleTouchEvents() {
Expand All @@ -468,11 +474,12 @@ private void disallowParentToHandleTouchEvents() {

void clickMe(View view) {
switch (clickBehaviour) {
case 0: break;
case 1: toggle(animation); break;
case 2: inverseToggle(animation); break;
case 3: revertToDefault(animation); break;
case 4: createPopupMenu(view); break;
case ONCLICK_NONE: break;
case ONCLICK_NEXT: toggle(animation); break;
case ONCLICK_PREV: inverseToggle(animation); break;
case ONCLICK_RESET: revertToDefault(animation); break;
case ONCLICK_MENU: createPopupMenu(view); break;
case ONCLICK_USER: runUserBehaviour(); break;
}
}

Expand All @@ -489,9 +496,9 @@ public void onClick(View view) {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (!enabled) return false;
if (swipeDirection == 0) { toggle(animation); return false; }
if (swipeDirection == SWIPEDIRECTION_NONE) { toggle(animation); return false; }
int action = motionEvent.getAction();
if (swipeDirection == 1) { // vertical
if (swipeDirection == SWIPEDIRECTION_VERTICAL) { // vertical
int y = (int) motionEvent.getY();
if (action == MotionEvent.ACTION_DOWN) {
swipeY = y;
Expand All @@ -518,7 +525,7 @@ else if (action == MotionEvent.ACTION_UP) {
}
return false;
}
else if (swipeDirection == 2) { // horizontal
else if (swipeDirection == SWIPEDIRECTION_HORIZONTAL) { // horizontal
int x = (int) motionEvent.getX();
if (action == MotionEvent.ACTION_DOWN) {
swipeX = x;
Expand All @@ -545,7 +552,7 @@ else if (action == MotionEvent.ACTION_UP) {
}
return false;
}
else if (swipeDirection == 3) { // both
else if (swipeDirection == SWIPEDIRECTION_HORIZONTALVERTICAL) { // both
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
if (action == MotionEvent.ACTION_DOWN) {
Expand Down Expand Up @@ -576,7 +583,7 @@ else if (action == MotionEvent.ACTION_UP) {
}
return false;
}
else if (swipeDirection == 4) { // circular
else if (swipeDirection == SWIPEDIRECTION_CIRCULAR) { // circular
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
if (action == MotionEvent.ACTION_DOWN) {
Expand Down Expand Up @@ -1142,4 +1149,14 @@ public int getClickBehaviour() {
public void setClickBehaviour(int clickBehaviour) {
this.clickBehaviour = clickBehaviour;
}

public void setUserBehaviour(Runnable userRunnable) {
// when "user" click behaviour is selected
this.userRunnable = userRunnable;
}

public void runUserBehaviour() { // to be initialized with setUserBehaviour()
if (userRunnable == null) return;
userRunnable.run();
}
}
1 change: 1 addition & 0 deletions knoblibrary/src/main/res/values/attrs.xml
Expand Up @@ -69,6 +69,7 @@
<enum name="prev" value = "2"/>
<enum name="revert" value = "3"/>
<enum name="menu" value = "4" />
<enum name="user" value = "5" />
</attr>

<attr name="kEnabled" format="boolean"/>
Expand Down
Expand Up @@ -5,6 +5,7 @@
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Random;
Expand Down Expand Up @@ -72,6 +73,13 @@ public void onState(int state) {
}
});


knob2.setUserBehaviour(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "This is a test click listener\nfor a knob with behaviour 'user'", Toast.LENGTH_LONG).show();
}
});
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion knobsample/src/main/res/layout/activity_sample.xml
Expand Up @@ -294,7 +294,9 @@
app:kSelectedStateMarkerColor="@color/red_500"
app:kStateMarkersColor="@color/light_blue_100"
app:kStateMarkersRelativeLength="0.5"
app:kStateMarkersWidth="0.5dp" />
app:kStateMarkersWidth="0.5dp"
app:kClickBehaviour="user"
/>

<it.beppi.knoblibrary.Knob
android:id="@+id/knob3"
Expand Down

0 comments on commit e20cc48

Please sign in to comment.