Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Vector Drawables #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 5 additions & 101 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:

<a href="https://play.google.com/store/apps/details?id=com.github.glomadrian.materialanimatedswitch.sample">
<img alt="Get it on Google Play"
src="https://developer.android.com/images/brand/en_generic_rgb_wo_60.png" />
</a>

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
<com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch
android:id="@+id/pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
```


With custom colors and icons attributes
```xml
<com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch
android:id="@+id/pin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:base_release_color="@color/md_deep_orange_900"
app:base_press_color="@color/md_deep_orange_50"
app:ball_release_color="@color/md_deep_orange_500"
app:ball_press_color="@color/md_white_1000"
app:icon_release="@drawable/ic_play_arrow_white_36dp"
app:icon_press="@drawable/ic_stop_deep_orange_500_36dp"
/>
```

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 - <[email protected]>
* [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
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_cloud_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/md_indigo_500"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_cloud_off_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/md_white_1000"
android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4c-1.48,0 -2.85,0.43 -4.01,1.17l1.46,1.46C10.21,6.23 11.08,6 12,6c3.04,0 5.5,2.46 5.5,5.5v0.5H19c1.66,0 3,1.34 3,3 0,1.13 -0.64,2.11 -1.56,2.62l1.45,1.45C23.16,18.16 24,16.68 24,15c0,-2.64 -2.05,-4.78 -4.65,-4.96zM3,5.27l2.75,2.74C2.56,8.15 0,10.77 0,14c0,3.31 2.69,6 6,6h11.73l2,2L21,20.73 4.27,4 3,5.27zM7.73,10l8,8H6c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4h1.73z"/>
</vector>
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@
app:icon_release="@drawable/ic_play_arrow_white_36dp"
app:icon_press="@drawable/ic_stop_deep_orange_500_36dp"
/>

<com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch
android:id="@+id/pin5"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
app:base_release_color="@color/md_indigo_900"
app:base_press_color="@color/md_indigo_50"
app:ball_release_color="@color/md_indigo_500"
app:ball_press_color="@color/md_white_1000"
app:icon_release="@drawable/ic_cloud_off_black_24dp"
app:icon_press="@drawable/ic_cloud_black_24dp"
/>

</LinearLayout>

</RelativeLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions materialanimatedswitch/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,96 @@
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;

/**
* @author Adrián García Lomas
*/
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");
}
}
}
Loading