diff --git a/README.md b/README.md index 431c4df..1427d11 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ The library is a custom Switch widget inspired by this [dribbble shot](https://d ## Gradle Add this into your dependencies block. ``` -compile 'com.polyak:icon-switch:1.0.0' +implementation 'com.github.parminder93:IconSwitch:v1.0.5' ``` ## Sample Please see the [sample app](sample/src/main) for a library usage example. @@ -41,6 +41,7 @@ To control the current state or get information about it, use: iconSwitch.setChecked(); iconSwitch.getChecked(); iconSwitch.toggle(); +iconSwitch.setEnabled(); ``` #### Color diff --git a/build.gradle b/build.gradle index 4900c3a..280d2f9 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,6 @@ ext { groupId = 'com.polyak' uploadName = 'IconSwitch' description = 'Custom switch-like widget.' - publishVersion = '1.0.0' + publishVersion = '1.0.1' licences = ['Apache-2.0'] } diff --git a/iconswitch/build.gradle b/iconswitch/build.gradle index a88ae86..78c1dae 100644 --- a/iconswitch/build.gradle +++ b/iconswitch/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 25 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" } } diff --git a/iconswitch/src/main/java/com/polyak/iconswitch/IconSwitch.java b/iconswitch/src/main/java/com/polyak/iconswitch/IconSwitch.java index 2716e60..fff450c 100644 --- a/iconswitch/src/main/java/com/polyak/iconswitch/IconSwitch.java +++ b/iconswitch/src/main/java/com/polyak/iconswitch/IconSwitch.java @@ -60,6 +60,7 @@ public class IconSwitch extends ViewGroup { private int inactiveTintIconLeft, activeTintIconLeft; private int inactiveTintIconRight, activeTintIconRight; private int thumbColorLeft, thumbColorRight; + private int backgroundColor; private PointF downPoint; private boolean isClick; @@ -123,7 +124,7 @@ private void init(AttributeSet attr) { activeTintIconLeft = ta.getColor(R.styleable.IconSwitch_isw_active_tint_icon_left, colorDefActive); inactiveTintIconRight = ta.getColor(R.styleable.IconSwitch_isw_inactive_tint_icon_right, colorDefInactive); activeTintIconRight = ta.getColor(R.styleable.IconSwitch_isw_active_tint_icon_right, colorDefActive); - background.setColor(ta.getColor(R.styleable.IconSwitch_isw_background_color, colorDefBackground)); + backgroundColor = ta.getColor(R.styleable.IconSwitch_isw_background_color, colorDefBackground); thumbColorLeft = ta.getColor(R.styleable.IconSwitch_isw_thumb_color_left, colorDefThumb); thumbColorRight = ta.getColor(R.styleable.IconSwitch_isw_thumb_color_right, colorDefThumb); currentChecked = Checked.values()[ta.getInt(R.styleable.IconSwitch_isw_default_selection, 0)]; @@ -210,6 +211,9 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) { @Override public boolean onTouchEvent(MotionEvent e) { + if(!this.isEnabled()) + return false; + MotionEvent event = MotionEvent.obtain(e); event.setLocation(e.getX() - translationX, e.getY() - translationY); switch (event.getAction()) { @@ -224,6 +228,7 @@ public boolean onTouchEvent(MotionEvent e) { clearTouchInfo(); break; case MotionEvent.ACTION_CANCEL: + cancelAction(); clearTouchInfo(); break; } @@ -232,6 +237,27 @@ public boolean onTouchEvent(MotionEvent e) { return true; } + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + + int left = currentChecked == Checked.LEFT ? thumbStartLeft : thumbEndLeft; + if (thumbDragHelper.smoothSlideViewTo(thumb, left, thumb.getTop())) { + ViewCompat.postInvalidateOnAnimation(this); + } + + ensureCorrectColors(); + } + + @ColorInt + private int getDisableColor(@ColorInt int color) { + return Color.argb(30, Color.red(color), Color.green(color), Color.blue(color)); + } + + @ColorInt + private int getEnableColor(@ColorInt int color) { + return Color.rgb(Color.red(color), Color.green(color), Color.blue(color)); + } + private void onDown(MotionEvent e) { velocityTracker = VelocityTracker.obtain(); velocityTracker.addMovement(e); @@ -267,6 +293,12 @@ private void clearTouchInfo() { } } + private void cancelAction() { + if (thumbDragHelper.smoothSlideViewTo(thumb, currentChecked == Checked.LEFT ? thumbStartLeft : thumbEndLeft, thumb.getTop())) { + ViewCompat.postInvalidateOnAnimation(this); + } + } + private void toggleSwitch() { currentChecked = currentChecked.toggle(); int newLeft = currentChecked == Checked.LEFT ? thumbStartLeft : thumbEndLeft; @@ -315,9 +347,28 @@ public void setCheckedChangeListener(CheckedChangeListener listener) { } private void ensureCorrectColors() { + if(isEnabled()) { + thumbColorLeft = getEnableColor(thumbColorLeft); + thumbColorRight = getEnableColor(thumbColorRight); + backgroundColor = getEnableColor(backgroundColor); + + leftIcon.setAlpha(1f); + rightIcon.setAlpha(1f); + } else { + thumbColorLeft = getDisableColor(thumbColorLeft); + thumbColorRight = getDisableColor(thumbColorRight); + backgroundColor = getDisableColor(backgroundColor); + + leftIcon.setAlpha(0.3f); + rightIcon.setAlpha(0.3f); + } + leftIcon.setColorFilter(isLeftChecked() ? activeTintIconLeft : inactiveTintIconLeft); rightIcon.setColorFilter(isLeftChecked() ? inactiveTintIconRight : activeTintIconRight); thumb.setColor(isLeftChecked() ? thumbColorLeft : thumbColorRight); + background.setColor(backgroundColor); + + } private boolean isLeftChecked() { @@ -331,15 +382,24 @@ private void notifyCheckedChanged() { } public void setChecked(Checked newChecked) { - if (currentChecked != newChecked) { + if (isEnabled() && currentChecked != newChecked) { + toggleSwitch(); + notifyCheckedChanged(); + } + } + + public void setChecked(Checked newChecked, boolean ignoreEnable) { + if ((ignoreEnable || isEnabled()) && currentChecked != newChecked) { toggleSwitch(); notifyCheckedChanged(); } } public void toggle() { - toggleSwitch(); - notifyCheckedChanged(); + if(isEnabled()) { + toggleSwitch(); + notifyCheckedChanged(); + } } public Checked getChecked() { @@ -376,8 +436,9 @@ public void setActiveTintIconRight(@ColorInt int activeTintIconRight) { ensureCorrectColors(); } - public void setBackgroundColor(@ColorInt int color) { - background.setColor(color); + public void setBackgroundColor(@ColorInt int backgroundColor) { + this.backgroundColor = backgroundColor; + ensureCorrectColors(); } public ImageView getLeftIcon() { diff --git a/iconswitch/src/main/java/com/polyak/iconswitch/ViewDragHelper.java b/iconswitch/src/main/java/com/polyak/iconswitch/ViewDragHelper.java index 3e4b4dc..08e8322 100644 --- a/iconswitch/src/main/java/com/polyak/iconswitch/ViewDragHelper.java +++ b/iconswitch/src/main/java/com/polyak/iconswitch/ViewDragHelper.java @@ -237,6 +237,9 @@ public boolean settleCapturedViewAt(int finalLeft, int finalTop) { } private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) { + if(mCapturedView == null) + return false; + final int startLeft = mCapturedView.getLeft(); final int startTop = mCapturedView.getTop(); final int dx = finalLeft - startLeft; diff --git a/sample/build.gradle b/sample/build.gradle index e5c5236..5d5dc18 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.polyak.iconswitch.sample" minSdkVersion 21 targetSdkVersion 25 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" } buildTypes { diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index ce1f447..1f9a2a4 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ + android:screenOrientation="fullSensor"> diff --git a/sample/src/main/java/com/polyak/iconswitch/sample/MainActivity.java b/sample/src/main/java/com/polyak/iconswitch/sample/MainActivity.java index e1919a8..08676dc 100644 --- a/sample/src/main/java/com/polyak/iconswitch/sample/MainActivity.java +++ b/sample/src/main/java/com/polyak/iconswitch/sample/MainActivity.java @@ -3,6 +3,7 @@ import android.animation.ValueAnimator; import android.content.Intent; import android.graphics.Point; +import android.graphics.drawable.Icon; import android.net.Uri; import android.os.Bundle; import android.support.annotation.ColorInt; @@ -34,6 +35,7 @@ public class MainActivity extends AppCompatActivity implements OnMapReadyCallbac private static final Uri URL_GITHUB_POLYAK = Uri.parse("https://github.com/polyak01"); private static final Uri URL_GITHUB_YAROLEGOVICH = Uri.parse("https://github.com/yarolegovich"); private static final Uri URL_DRIBBBLE_PROKHODA = Uri.parse("https://dribbble.com/prokhoda"); + private static final Uri URL_GITHUB_PARMINDER93 = Uri.parse("https://github.com/parminder93"); private int[] toolbarColors; private int[] statusBarColors; @@ -77,6 +79,14 @@ protected void onCreate(Bundle savedInstanceState) { findViewById(R.id.credit_polyak).setOnClickListener(this); findViewById(R.id.credit_yarolegovich).setOnClickListener(this); findViewById(R.id.credit_prokhoda).setOnClickListener(this); + findViewById(R.id.credit_parminder93).setOnClickListener(this); + + ((IconSwitch)findViewById(R.id.ics_enable)).setCheckedChangeListener(new IconSwitch.CheckedChangeListener() { + @Override + public void onCheckChanged(Checked current) { + iconSwitch.setEnabled(current == Checked.LEFT); + } + }); } private void updateColors(boolean animated) { @@ -159,6 +169,9 @@ public void onClick(View v) { case R.id.credit_prokhoda: open(URL_DRIBBBLE_PROKHODA); break; + case R.id.credit_parminder93: + open(URL_GITHUB_PARMINDER93); + break; } } diff --git a/sample/src/main/res/drawable-hdpi/ic_disable_white_18dp.png b/sample/src/main/res/drawable-hdpi/ic_disable_white_18dp.png new file mode 100644 index 0000000..aa84491 Binary files /dev/null and b/sample/src/main/res/drawable-hdpi/ic_disable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-hdpi/ic_enable_white_18dp.png b/sample/src/main/res/drawable-hdpi/ic_enable_white_18dp.png new file mode 100644 index 0000000..92c9c3f Binary files /dev/null and b/sample/src/main/res/drawable-hdpi/ic_enable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-mdpi/ic_disable_white_18dp.png b/sample/src/main/res/drawable-mdpi/ic_disable_white_18dp.png new file mode 100644 index 0000000..3b695f9 Binary files /dev/null and b/sample/src/main/res/drawable-mdpi/ic_disable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-mdpi/ic_enable_white_18dp.png b/sample/src/main/res/drawable-mdpi/ic_enable_white_18dp.png new file mode 100644 index 0000000..dcc9724 Binary files /dev/null and b/sample/src/main/res/drawable-mdpi/ic_enable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-xhdpi/ic_disable_white_18dp.png b/sample/src/main/res/drawable-xhdpi/ic_disable_white_18dp.png new file mode 100644 index 0000000..de395f4 Binary files /dev/null and b/sample/src/main/res/drawable-xhdpi/ic_disable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-xhdpi/ic_enable_white_18dp.png b/sample/src/main/res/drawable-xhdpi/ic_enable_white_18dp.png new file mode 100644 index 0000000..39eacc6 Binary files /dev/null and b/sample/src/main/res/drawable-xhdpi/ic_enable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-xxhdpi/ic_disable_white_18dp.png b/sample/src/main/res/drawable-xxhdpi/ic_disable_white_18dp.png new file mode 100644 index 0000000..5e212f3 Binary files /dev/null and b/sample/src/main/res/drawable-xxhdpi/ic_disable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-xxhdpi/ic_enable_white_18dp.png b/sample/src/main/res/drawable-xxhdpi/ic_enable_white_18dp.png new file mode 100644 index 0000000..8a3d0a2 Binary files /dev/null and b/sample/src/main/res/drawable-xxhdpi/ic_enable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-xxxhdpi/ic_disable_white_18dp.png b/sample/src/main/res/drawable-xxxhdpi/ic_disable_white_18dp.png new file mode 100644 index 0000000..63a7bc0 Binary files /dev/null and b/sample/src/main/res/drawable-xxxhdpi/ic_disable_white_18dp.png differ diff --git a/sample/src/main/res/drawable-xxxhdpi/ic_enable_white_18dp.png b/sample/src/main/res/drawable-xxxhdpi/ic_enable_white_18dp.png new file mode 100644 index 0000000..73dcdac Binary files /dev/null and b/sample/src/main/res/drawable-xxxhdpi/ic_enable_white_18dp.png differ diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index ccd43f7..31d921e 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -85,7 +85,9 @@ android:layout_height="wrap_content" app:mp_icon="@drawable/ic_github_circle_grey600_24dp" app:mp_summary="@string/credit_summary_yarolegovich" - app:mp_title="@string/credit_title_yarolegovich" /> + app:mp_title="@string/credit_title_yarolegovich" > + + + app:mp_title="@string/credit_title_prokhoda" > + + + + + + + diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index 4e0a9cc..a9d52af 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -9,4 +9,6 @@ Contributor Yaroslav Polyakov Developer + Parminder Singh + Contributor