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 9, 2017
1 parent 323ae16 commit 4f12ee7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

4 changes: 2 additions & 2 deletions knoblibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 25
versionCode 11
versionName "1.7.0"
versionCode 12
versionName "1.8.0"
}
buildTypes {
release {
Expand Down
84 changes: 78 additions & 6 deletions knoblibrary/src/main/java/it/beppi/knoblibrary/Knob.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.PopupMenu;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
Expand All @@ -36,6 +39,13 @@ public class Knob extends View {
public static final int SWIPEDIRECTION_HORIZONTALVERTICAL = 3;
public static final int SWIPEDIRECTION_CIRCULAR = 4;

public static final int ONCLICK_NONE = 0;
public static final int ONCLICK_NEXT = 1;
public static final int ONCLICK_PREV = 2;
public static final int ONCLICK_RESET = 3;
public static final int ONCLICK_MENU = 4;


// constructors
public Knob(Context context) {
super(context);
Expand Down Expand Up @@ -318,6 +328,7 @@ BalloonPopup.BalloonAnimation getBalloonAnimation() {
private int balloonValuesAnimation = 0;
private CharSequence[] balloonValuesArray = null;
private boolean balloonValuesSlightlyTransparent = true;
private int clickBehaviour = 1; // next


// initialize
Expand Down Expand Up @@ -416,6 +427,8 @@ void loadAttributes(AttributeSet attrs) {
balloonValuesArray = typedArray.getTextArray(R.styleable.Knob_kBalloonValuesArray);
balloonValuesSlightlyTransparent = typedArray.getBoolean(R.styleable.Knob_kBalloonValuesSlightlyTransparent, balloonValuesSlightlyTransparent);

clickBehaviour = clickAttrToInt(typedArray.getString(R.styleable.Knob_kClickBehaviour));

enabled = typedArray.getBoolean(R.styleable.Knob_kEnabled, enabled);

typedArray.recycle();
Expand All @@ -429,6 +442,15 @@ int swipeAttrToInt(String s) {
else if (s.equals("4")) return 4; // default - circular
else return 4;
}
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;
}
int balloonAnimationAttrToInt(String s) {
if (s == null) return 0;
if (s.equals("0")) return 0; // pop
Expand All @@ -444,13 +466,22 @@ private void disallowParentToHandleTouchEvents() {
}
}

void initListeners() {
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;
}
}

void initListeners() {
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (!enabled) return;
toggle(animation);
clickMe(view);
}
});

Expand Down Expand Up @@ -482,7 +513,7 @@ else if (swipeY - y > swipeSensibilityPixels) {
}
}
else if (action == MotionEvent.ACTION_UP) {
if (!swipeing) toggle(animation); // click
if (!swipeing) clickMe(view); // click
return true;
}
return false;
Expand All @@ -509,7 +540,7 @@ else if (swipeX - x > swipeSensibilityPixels) {
}
}
else if (action == MotionEvent.ACTION_UP) {
if (!swipeing) toggle(animation); // click
if (!swipeing) clickMe(view); // click
return true;
}
return false;
Expand Down Expand Up @@ -540,7 +571,7 @@ else if (swipeX - x > swipeSensibilityPixels || y - swipeY > swipeSensibilityPix
}
}
else if (action == MotionEvent.ACTION_UP) {
if (!swipeing) toggle(animation); // click
if (!swipeing) clickMe(view); // click
return true;
}
return false;
Expand All @@ -559,7 +590,7 @@ else if (action == MotionEvent.ACTION_MOVE) {
return true;
}
else if (action == MotionEvent.ACTION_UP) {
if (!swipeing) toggle(animation); // click
if (!swipeing) clickMe(view); // click
return true;
}
return false;
Expand All @@ -569,6 +600,7 @@ else if (action == MotionEvent.ACTION_UP) {
return false;
}
});

spring.addListener(new SimpleSpringListener(){
@Override
public void onSpringUpdate(Spring spring) {
Expand All @@ -577,6 +609,28 @@ public void onSpringUpdate(Spring spring) {
}});
}

void createPopupMenu(View view) {
PopupMenu mPopupMenu = new PopupMenu(getContext(), view);
if (balloonValuesArray == null)
for (int w=0; w<numberOfStates; w++)
mPopupMenu.getMenu().add(Menu.NONE, w+1, w+1, Integer.toString(w));
else
for (int w=0; w<numberOfStates; w++)
mPopupMenu.getMenu().add(Menu.NONE, w+1, w+1, balloonValuesArray[w].toString());

mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId()-1;
setState(i);
return true;
}
});

mPopupMenu.show();

}

void initStatus() {
currentState = defaultState;
previousState = defaultState;
Expand All @@ -598,6 +652,16 @@ public void toggle() {
toggle(animation);
}

public void inverseToggle(boolean animate) {
decreaseValue(animate);
}
public void inverseToggle() { inverseToggle(animation);}

public void revertToDefault(boolean animate) {
setState(defaultState, animate);
}
public void revertToDefault() { revertToDefault(animation); }

private void calcActualState() {
actualState = currentState % numberOfStates;
if (actualState < 0) actualState += numberOfStates;
Expand Down Expand Up @@ -1070,4 +1134,12 @@ public boolean isBalloonValuesSlightlyTransparent() {
public void setBalloonValuesSlightlyTransparent(boolean balloonValuesSlightlyTransparent) {
this.balloonValuesSlightlyTransparent = balloonValuesSlightlyTransparent;
}

public int getClickBehaviour() {
return clickBehaviour;
}

public void setClickBehaviour(int clickBehaviour) {
this.clickBehaviour = clickBehaviour;
}
}
8 changes: 8 additions & 0 deletions knoblibrary/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
<attr name="kBalloonValuesArray" format="reference"/>
<attr name="kBalloonValuesSlightlyTransparent" format="boolean"/>

<attr name="kClickBehaviour">
<enum name="nothing" value = "0"/>
<enum name="next" value = "1"/>
<enum name="prev" value = "2"/>
<enum name="revert" value = "3"/>
<enum name="menu" value = "4" />
</attr>

<attr name="kEnabled" format="boolean"/>
</declare-styleable>
</resources>
5 changes: 4 additions & 1 deletion knobsample/src/main/res/layout/activity_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
<it.beppi.knoblibrary.Knob
android:id="@+id/knob1"
android:layout_width="96dp"
android:layout_height="96dp" />
android:layout_height="96dp"
/>

<TextView
android:layout_width="match_parent"
Expand Down Expand Up @@ -132,6 +133,7 @@
<it.beppi.knoblibrary.Knob
android:layout_width="96dp"
android:layout_height="96dp"
app:kClickBehaviour="menu"
app:kAnimationBounciness="1"
app:kAnimationSpeed="0"
app:kBalloonValuesAnimation="scale"
Expand Down Expand Up @@ -220,6 +222,7 @@
android:id="@+id/multistate_toggle"
android:layout_width="96dp"
android:layout_height="96dp"
app:kClickBehaviour="menu"
app:kAnimationBounciness="5"
app:kAnimationSpeed="20"
app:kBalloonValuesArray="@array/multichoice"
Expand Down

0 comments on commit 4f12ee7

Please sign in to comment.