diff --git a/README.md b/README.md index 4a2a493..5492d2e 100644 --- a/README.md +++ b/README.md @@ -1,119 +1,23 @@ Material Animated Switch ----------------- -[![Download](https://api.bintray.com/packages/glomadrian/maven/MaterialAnimatedSwitch/images/download.svg) ](https://bintray.com/glomadrian/maven/MaterialAnimatedSwitch/_latestVersion) -[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Material%20Animated%20Switch-green.svg?style=flat)](https://android-arsenal.com/details/1/1985) - -A material Switch with icon animations and color transitions - - - -![Demo Screenshot][1] - -![Demo Screenshot][2] - - -### Sample video: - -[Youtube Material Animated Switch video](https://youtu.be/MBJpCfXuVuU) - -### Sample app: - - - Get it on Google Play - - -Based on ----------- - -[Inbox Pinned](https://dribbble.com/shots/2098916-Inbox-Pinned) by [Derek Torsani](https://dribbble.com/dmtors) - - -How to use ----------- - -By default show the inbox pinned switch - -```xml - -``` - - -With custom colors and icons attributes -```xml - -``` - -Remember put this for custom attribute usage - -```java - -xmlns:app="http://schemas.android.com/apk/res-auto" - -``` +This fork support Vector Drawables +For any questions feel free to write in Issues For Gradle --------------------- +To get latest version Add repository ```groovy repositories { maven { - url "http://dl.bintray.com/glomadrian/maven" + url 'https://jitpack.io' } } ``` Add dependency ```groovy -compile 'com.github.glomadrian:MaterialAnimatedSwitch:1.1@aar' +compile 'com.github.viliamvolosv:material-animated-switch:-SNAPSHOT' ``` -Changelog ---------------------- - -V 1.1 - -* Fixed bug that cut the ball at the start -* Added isChecked() method -* fix toggle() method - -Developed By ------------- -Adrián García Lomas - -* [Twitter](https://twitter.com/glomadrian) -* [LinkedIn](https://es.linkedin.com/in/glomadrian ) - -License -------- - - Copyright 2015 Adrián García Lomas - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -[1]: ./art/swtich.gif -[2]: ./art/all.png diff --git a/app/build.gradle b/app/build.gradle index ce7aca2..943933a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,15 +1,16 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 22 - buildToolsVersion "22.0.1" + compileSdkVersion 25 + buildToolsVersion "25.0.1" defaultConfig { applicationId "com.github.glomadrian.materialanimatedswitch.sample" minSdkVersion 9 - targetSdkVersion 22 + targetSdkVersion 25 versionCode 1 versionName "1.0" + vectorDrawables.useSupportLibrary = true } buildTypes { release { @@ -21,6 +22,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:22.2.0' + compile 'com.android.support:appcompat-v7:25.0.1' compile project (':materialanimatedswitch') } diff --git a/app/src/main/res/drawable/ic_cloud_black_24dp.xml b/app/src/main/res/drawable/ic_cloud_black_24dp.xml new file mode 100644 index 0000000..efd6695 --- /dev/null +++ b/app/src/main/res/drawable/ic_cloud_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_cloud_off_black_24dp.xml b/app/src/main/res/drawable/ic_cloud_off_black_24dp.xml new file mode 100644 index 0000000..1fa97d4 --- /dev/null +++ b/app/src/main/res/drawable/ic_cloud_off_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index eceef21..118922d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -82,6 +82,21 @@ app:icon_release="@drawable/ic_play_arrow_white_36dp" app:icon_press="@drawable/ic_stop_deep_orange_500_36dp" /> + + + diff --git a/build.gradle b/build.gradle index df1607e..0d3af89 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.2.3' + classpath 'com.android.tools.build:gradle:2.2.3' classpath 'com.github.dcendents:android-maven-plugin:1.2' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0c71e76..836e469 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Apr 10 15:27:10 PDT 2013 +#Mon Nov 21 17:47:55 MSK 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/materialanimatedswitch/build.gradle b/materialanimatedswitch/build.gradle index e2fc85d..a479e3e 100644 --- a/materialanimatedswitch/build.gradle +++ b/materialanimatedswitch/build.gradle @@ -1,14 +1,15 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 22 - buildToolsVersion "22.0.1" + compileSdkVersion 25 + buildToolsVersion "25.0.1" defaultConfig { minSdkVersion 9 - targetSdkVersion 22 + targetSdkVersion 25 versionCode 1 versionName "1.1" + vectorDrawables.useSupportLibrary = true } buildTypes { release { @@ -20,6 +21,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:22.2.0' + compile 'com.android.support:appcompat-v7:25.0.1' compile 'com.nineoldandroids:library:2.4.0' } \ No newline at end of file diff --git a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/MaterialAnimatedSwitch.java b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/MaterialAnimatedSwitch.java index dfd70a4..0cafff0 100644 --- a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/MaterialAnimatedSwitch.java +++ b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/MaterialAnimatedSwitch.java @@ -2,11 +2,10 @@ import android.content.Context; import android.content.res.TypedArray; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.os.Build; +import android.support.annotation.DrawableRes; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.SoundEffectConstants; @@ -40,8 +39,10 @@ public class MaterialAnimatedSwitch extends View { private int ballColorRelease = Color.parseColor("#5992FB"); private int ballColorPress = Color.parseColor("#FFFFFF"); private int ballShadowColor = Color.parseColor("#99000000"); - private Bitmap releaseIcon; - private Bitmap pressIcon; + @DrawableRes + private int releaseIcon; + @DrawableRes + private int pressIcon; private BallFinishObservable ballFinishObservable; private BallMoveObservable ballMoveObservable; private boolean isClickable = true; @@ -107,12 +108,12 @@ private void initAttributes(TypedArray attributes) { ballColorRelease); ballColorPress = attributes.getColor(R.styleable.materialAnimatedSwitch_ball_press_color, ballColorPress); - pressIcon = BitmapFactory.decodeResource(getResources(), + pressIcon = attributes.getResourceId(R.styleable.materialAnimatedSwitch_icon_press, - R.drawable.tack_save_button_32_blue)); - releaseIcon = BitmapFactory.decodeResource(getResources(), + R.drawable.tack_save_button_32_blue); + releaseIcon = attributes.getResourceId(R.styleable.materialAnimatedSwitch_icon_release, - R.drawable.tack_save_button_32_white)); + R.drawable.tack_save_button_32_white); } private void initObservables() { diff --git a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/Utils.java b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/Utils.java index d9fc0bd..3220cca 100644 --- a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/Utils.java +++ b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/Utils.java @@ -1,6 +1,17 @@ package com.github.glomadrian.materialanimatedswitch; +import android.annotation.TargetApi; +import android.content.Context; import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.VectorDrawable; +import android.os.Build; +import android.support.annotation.DrawableRes; +import android.support.graphics.drawable.VectorDrawableCompat; +import android.support.v7.widget.AppCompatDrawableManager; import android.util.TypedValue; /** @@ -8,12 +19,78 @@ */ public class Utils { - /** - * Convert Dp to Pixel - */ - public static int dpToPx(float dp, Resources resources) { - float px = - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics()); - return (int) px; - } + /** + * Convert Dp to Pixel + */ + public static int dpToPx(float dp, Resources resources) { + float px = + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics()); + return (int) px; + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private static Bitmap getBitmap(VectorDrawable vectorDrawable) { + Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), + vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + vectorDrawable.draw(canvas); + return bitmap; + } + + private static Bitmap getBitmap(VectorDrawableCompat vectorDrawable) { + Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), + vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + vectorDrawable.draw(canvas); + return bitmap; + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private static Bitmap getBitmap(VectorDrawable vectorDrawable, int width, int height) { + Bitmap bitmap = Bitmap.createBitmap(width, + height, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + vectorDrawable.setBounds(0, 0, width, height); + vectorDrawable.draw(canvas); + return bitmap; + } + + private static Bitmap getBitmap(VectorDrawableCompat vectorDrawable, int width, int height) { + Bitmap bitmap = Bitmap.createBitmap(width, + height, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + vectorDrawable.setBounds(0, 0, width, height); + vectorDrawable.draw(canvas); + return bitmap; + } + + public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) { + Drawable drawable = + AppCompatDrawableManager.get().getDrawable(context, drawableResId); + if (drawable instanceof BitmapDrawable) { + return ((BitmapDrawable) drawable).getBitmap(); + } else if (drawable instanceof VectorDrawableCompat) { + return getBitmap((VectorDrawableCompat) drawable); + } else if (drawable instanceof VectorDrawable) { + return getBitmap((VectorDrawable) drawable); + } else { + throw new IllegalArgumentException("Unsupported drawable type"); + } + } + + public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId, int width, int height) { + Drawable drawable = + AppCompatDrawableManager.get().getDrawable(context, drawableResId); + if (drawable instanceof BitmapDrawable) { + return Bitmap.createScaledBitmap(((BitmapDrawable) drawable).getBitmap(), width, height, false); + } else if (drawable instanceof VectorDrawableCompat) { + return getBitmap((VectorDrawableCompat) drawable, width, height); + } else if (drawable instanceof VectorDrawable) { + return getBitmap((VectorDrawable) drawable, width, height); + } else { + throw new IllegalArgumentException("Unsupported drawable type"); + } + } } diff --git a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPainter.java b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPainter.java index 1f550cb..1d522da 100644 --- a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPainter.java +++ b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPainter.java @@ -4,13 +4,18 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; +import android.support.annotation.DrawableRes; + import com.github.glomadrian.materialanimatedswitch.R; +import com.github.glomadrian.materialanimatedswitch.Utils; /** * @author Adrián García Lomas */ public abstract class IconPainter implements SwitchInboxPinnedPainter { + @DrawableRes + protected int drawableId; protected Bitmap iconBitmap; protected Context context; protected Paint paint; @@ -23,9 +28,9 @@ public abstract class IconPainter implements SwitchInboxPinnedPainter { protected int iconYPosition; protected int margin; - public IconPainter(Context context, Bitmap bitmap, int margin) { + public IconPainter(Context context, @DrawableRes int drawableId, int margin) { this.context = context; - this.iconBitmap = bitmap; + this.drawableId = drawableId; this.margin = margin; init(); } @@ -38,7 +43,7 @@ private void init() { protected void initBitmap() { int iconSize = (int) context.getResources().getDimension(R.dimen.icon_size); - iconBitmap = Bitmap.createScaledBitmap(iconBitmap, iconSize, iconSize, false); + iconBitmap = Utils.getBitmap(context,drawableId,iconSize,iconSize); imageHeight = iconBitmap.getHeight(); imageWidth = iconBitmap.getWidth(); } diff --git a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPressPainter.java b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPressPainter.java index 594c551..47bfb4b 100644 --- a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPressPainter.java +++ b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconPressPainter.java @@ -1,7 +1,8 @@ package com.github.glomadrian.materialanimatedswitch.painter; import android.content.Context; -import android.graphics.Bitmap; +import android.support.annotation.DrawableRes; + import com.github.glomadrian.materialanimatedswitch.R; import com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitchState; import com.github.glomadrian.materialanimatedswitch.observer.BallFinishObservable; @@ -26,9 +27,9 @@ public class IconPressPainter extends IconPainter { private int ballRadius; private BallMoveObservable ballMoveObservable; - public IconPressPainter(Context context, Bitmap bitmap, BallFinishObservable ballFinishObservable, - BallMoveObservable ballMoveObservable, int margin) { - super(context, bitmap, margin); + public IconPressPainter(Context context, @DrawableRes int drawableId, BallFinishObservable ballFinishObservable, + BallMoveObservable ballMoveObservable, int margin) { + super(context, drawableId, margin); initValueAnimator(); this.ballFinishObservable = ballFinishObservable; this.ballMoveObservable = ballMoveObservable; diff --git a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconReleasePainter.java b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconReleasePainter.java index a41a70a..4f5d564 100644 --- a/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconReleasePainter.java +++ b/materialanimatedswitch/src/main/java/com/github/glomadrian/materialanimatedswitch/painter/IconReleasePainter.java @@ -1,7 +1,8 @@ package com.github.glomadrian.materialanimatedswitch.painter; import android.content.Context; -import android.graphics.Bitmap; +import android.support.annotation.DrawableRes; + import com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitchState; import com.github.glomadrian.materialanimatedswitch.R; import com.github.glomadrian.materialanimatedswitch.observer.BallFinishObservable; @@ -30,9 +31,9 @@ public class IconReleasePainter extends IconPainter { private int middle; private boolean alphaEnterTrigger = false; - public IconReleasePainter(Context context, Bitmap bitmap, + public IconReleasePainter(Context context, @DrawableRes int drawableId, BallFinishObservable ballFinishObservable, int margin) { - super(context, bitmap, margin); + super(context, drawableId, margin); initValueAnimator(); this.ballFinishObservable = ballFinishObservable; initObserver();