Skip to content

Commit

Permalink
Merge pull request #17 from konifar/vector_drawable_support
Browse files Browse the repository at this point in the history
Vector drawable support
  • Loading branch information
jd-alexander committed Mar 14, 2016
2 parents 111180e + 67ccf90 commit a43402f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 16 deletions.
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ android {
targetSdkVersion 23
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
Expand All @@ -25,10 +26,10 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
//compile project(':likebutton')
compile 'com.github.jd-alexander:LikeButton:0.1.6'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile project(':likebutton')
//compile 'com.github.jd-alexander:LikeButton:0.1.6'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.mikepenz:iconics-core:2.5.2@aar'
compile 'com.mikepenz:community-material-typeface:1.3.41.1@aar'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_android_green_500_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
<path android:fillColor="#4CAF50" android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/ic_android_grey_500_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
<path android:fillColor="#9E9E9E" android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
</vector>
22 changes: 16 additions & 6 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.like.example.MainActivity"
Expand All @@ -23,14 +21,14 @@
android:id="@+id/star_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon_size="25dp"
app:icon_size="22dp"
app:icon_type="star" />

<com.like.LikeButton
android:id="@+id/heart_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon_size="25dp"
app:icon_size="22dp"
app:anim_scale_factor="3"
app:icon_type="heart" />

Expand All @@ -42,7 +40,7 @@
app:circle_start_color="@android:color/holo_blue_bright"
app:dots_primary_color="@android:color/holo_blue_light"
app:dots_secondary_color="@android:color/holo_blue_dark"
app:icon_size="25dp"
app:icon_size="22dp"
app:like_drawable="@drawable/thumb_on"
app:unlike_drawable="@drawable/thumb_off" />

Expand All @@ -54,6 +52,18 @@
app:circle_start_color="@android:color/holo_purple"
app:dots_primary_color="@android:color/holo_blue_dark"
app:dots_secondary_color="@android:color/holo_blue_light"
app:icon_size="25dp" />
app:icon_size="22dp" />

<com.like.LikeButton
android:id="@+id/vector_android_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:circle_end_color="@android:color/holo_green_dark"
app:circle_start_color="@android:color/holo_green_dark"
app:dots_primary_color="@android:color/holo_green_dark"
app:dots_secondary_color="@android:color/holo_green_light"
app:icon_size="22dp"
app:like_drawable="@drawable/ic_android_green_500_24dp"
app:unlike_drawable="@drawable/ic_android_grey_500_24dp" />
</LinearLayout>
</RelativeLayout>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.android.tools.build:gradle:2.0.0-beta6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip
3 changes: 2 additions & 1 deletion likebutton/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ android {
targetSdkVersion 23
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
Expand All @@ -28,5 +29,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:appcompat-v7:23.2.0'
}
38 changes: 35 additions & 3 deletions likebutton/src/main/java/com/like/Utils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.like;

import android.annotation.TargetApi;
import android.content.Context;
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.graphics.drawable.VectorDrawableCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;

Expand Down Expand Up @@ -34,10 +39,37 @@ public static List<Icon> getIcons() {
}

public static Drawable resizeDrawable(Context context, Drawable drawable, int width, int height) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Drawable newDrawable = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, width, height, true));
Bitmap bitmap = getBitmap(drawable, width, height);
return new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, width, height, true));
}

public static Bitmap getBitmap(Drawable drawable, int width, int height) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} 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");
}
}

@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, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}

return newDrawable;
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, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}

public static float dipToPixels(Context context, float dipValue) {
Expand Down

0 comments on commit a43402f

Please sign in to comment.