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

support tint for likeDrawable, unlikeDrawable and built-in icons(heart,star,etc.), vector drawable tint included. #106

Open
wants to merge 3 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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
before_install:
- chmod +x gradlew
- yes | sdkmanager "platforms;android-27"
- yes | sdkmanager "platforms;android-28"

language: android
jdk: oraclejdk8

android:
components:
- tools
- build-tools-27.0.1
- android-27
- build-tools-28.0.0
- android-28
- extra-android-support
- extra-android-m2repository
- extra-google-m2repository
Expand Down
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion "27.0.1"
compileSdkVersion 28
buildToolsVersion "28.0.0"
// buildToolsVersion "28.0.0"

lintOptions {
abortOnError false
Expand All @@ -26,8 +27,8 @@ android {

dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:appcompat-v7:27.0.1'
compile 'com.android.support:design:27.0.1'
compile 'com.android.support:appcompat-v7:28.0.0'
compile 'com.android.support:design:28.0.0'
compile project(':likebutton')
//compile 'com.github.jd-alexander:LikeButton:0.1.6'
compile 'com.jakewharton:butterknife:8.8.1'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/like/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.like.example;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -35,6 +36,8 @@ public class MainActivity extends AppCompatActivity implements OnLikeListener,
LikeButton thumbButton;
@BindView(R.id.smile_button)
LikeButton smileButton;
@BindView(R.id.vector_android_button)
LikeButton vectorButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -57,6 +60,8 @@ protected void onCreate(Bundle savedInstanceState) {

thumbButton.setLiked(true);

vectorButton.setLikeDrawableTintColor(Color.GREEN);
vectorButton.setUnLikeDrawableTintColor(Color.BLACK);
usingCustomIcons();

}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
app:icon_size="22dp"
app:like_drawable="@drawable/ic_android_green_500_24dp"
app:unlike_drawable="@drawable/ic_android_grey_500_24dp" />

</LinearLayout>
<Button
android:id="@+id/button"
Expand Down
6 changes: 3 additions & 3 deletions likebutton/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ apply plugin: 'com.github.dcendents.android-maven'
group='com.github.jd-alexander'

android {
compileSdkVersion 27
buildToolsVersion "27.0.1"
compileSdkVersion 28
buildToolsVersion "28.0.0"

lintOptions {
abortOnError false
Expand All @@ -29,5 +29,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:27.0.1'
compile 'com.android.support:appcompat-v7:28.0.0'
}
85 changes: 82 additions & 3 deletions likebutton/src/main/java/com/like/LikeButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.content.res.AppCompatResources;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
Expand Down Expand Up @@ -53,6 +56,12 @@ public class LikeButton extends FrameLayout implements View.OnClickListener {
private Drawable likeDrawable;
private Drawable unLikeDrawable;

private boolean hasLikeDrawableTintColor=false;
private boolean hasUnlikeDrawableTintColor=false;

private @ColorInt int likeDrawableTintColor;
private @ColorInt int unLikeDrawableTintColor;

public LikeButton(Context context) {
this(context, null);
}
Expand Down Expand Up @@ -90,6 +99,16 @@ private void init(Context context, AttributeSet attrs, int defStyle) {

String iconType = array.getString(R.styleable.LikeButton_icon_type);

hasLikeDrawableTintColor=array.hasValue(R.styleable.LikeButton_like_drawable_tint);
if(hasLikeDrawableTintColor){
likeDrawableTintColor=array.getColor(R.styleable.LikeButton_like_drawable_tint,0);
}

hasUnlikeDrawableTintColor=array.hasValue(R.styleable.LikeButton_unlike_drawable_tint);
if(hasUnlikeDrawableTintColor){
unLikeDrawableTintColor=array.getColor(R.styleable.LikeButton_unlike_drawable_tint,0);
}

likeDrawable = getDrawableFromResource(array, R.styleable.LikeButton_like_drawable);

if (likeDrawable != null)
Expand Down Expand Up @@ -142,7 +161,7 @@ private void init(Context context, AttributeSet attrs, int defStyle) {
private Drawable getDrawableFromResource(TypedArray array, int styleableIndexId) {
int id = array.getResourceId(styleableIndexId, -1);

return (-1 != id) ? ContextCompat.getDrawable(getContext(), id) : null;
return (-1 != id) ? AppCompatResources.getDrawable(getContext(), id) : null;
}

/**
Expand Down Expand Up @@ -289,17 +308,67 @@ public boolean onTouchEvent(MotionEvent event) {
* @param resId
*/
public void setLikeDrawableRes(@DrawableRes int resId) {
likeDrawable = ContextCompat.getDrawable(getContext(), resId);
likeDrawable = AppCompatResources.getDrawable(getContext(), resId);

if (iconSize != 0) {
likeDrawable = Utils.resizeDrawable(getContext(), likeDrawable, iconSize, iconSize);
}
tintDrawable(likeDrawable,hasLikeDrawableTintColor,likeDrawableTintColor);

if (isChecked) {
icon.setImageDrawable(likeDrawable);
}
}

public boolean hasLikeDrawableTintColor() {
return hasLikeDrawableTintColor;
}

public void setHasLikeDrawableTintColor(boolean hasLikeDrawableTintColor) {
this.hasLikeDrawableTintColor = hasLikeDrawableTintColor;
}

public boolean hasUnlikeDrawableTintColor() {
return hasUnlikeDrawableTintColor;
}

public void setHasUnlikeDrawableTintColor(boolean hasUnlikeDrawableTintColor) {
this.hasUnlikeDrawableTintColor = hasUnlikeDrawableTintColor;
}

public int getLikeDrawableTintColor() {
return likeDrawableTintColor;
}

public void setLikeDrawableTintColor(int likeDrawableTintColor) {
this.likeDrawableTintColor = likeDrawableTintColor;
hasLikeDrawableTintColor = true;
tintDrawable(likeDrawable,hasLikeDrawableTintColor,likeDrawableTintColor);
}

public int getUnLikeDrawableTintColor() {
return unLikeDrawableTintColor;
}

public void setUnLikeDrawableTintColor(int unLikeDrawableTintColor) {
this.unLikeDrawableTintColor = unLikeDrawableTintColor;
hasUnlikeDrawableTintColor = true;
tintDrawable(unLikeDrawable,hasUnlikeDrawableTintColor,unLikeDrawableTintColor);
}

private void tintDrawable(Drawable drawable, boolean needTint, @ColorInt int tintColor){
if(needTint){
// if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
// drawable.setTint(tintColor);
// drawable.invalidateSelf();
// }else{
drawable = DrawableCompat.wrap(drawable).mutate();
DrawableCompat.setTint(drawable,tintColor);
drawable.invalidateSelf();
// }
}
}

/**
* This drawable is shown when the button is in a liked state.
*
Expand All @@ -311,6 +380,7 @@ public void setLikeDrawable(Drawable likeDrawable) {
if (iconSize != 0) {
this.likeDrawable = Utils.resizeDrawable(getContext(), likeDrawable, iconSize, iconSize);
}
tintDrawable(this.likeDrawable,hasLikeDrawableTintColor,likeDrawableTintColor);

if (isChecked) {
icon.setImageDrawable(this.likeDrawable);
Expand All @@ -323,11 +393,12 @@ public void setLikeDrawable(Drawable likeDrawable) {
* @param resId
*/
public void setUnlikeDrawableRes(@DrawableRes int resId) {
unLikeDrawable = ContextCompat.getDrawable(getContext(), resId);
unLikeDrawable = AppCompatResources.getDrawable(getContext(), resId);

if (iconSize != 0) {
unLikeDrawable = Utils.resizeDrawable(getContext(), unLikeDrawable, iconSize, iconSize);
}
tintDrawable(unLikeDrawable,hasUnlikeDrawableTintColor,unLikeDrawableTintColor);

if (!isChecked) {
icon.setImageDrawable(unLikeDrawable);
Expand All @@ -345,6 +416,7 @@ public void setUnlikeDrawable(Drawable unLikeDrawable) {
if (iconSize != 0) {
this.unLikeDrawable = Utils.resizeDrawable(getContext(), unLikeDrawable, iconSize, iconSize);
}
tintDrawable(this.unLikeDrawable,hasUnlikeDrawableTintColor,unLikeDrawableTintColor);

if (!isChecked) {
icon.setImageDrawable(this.unLikeDrawable);
Expand Down Expand Up @@ -391,6 +463,8 @@ public void setIconSizePx(int iconSize) {
setEffectsViewSize();
this.unLikeDrawable = Utils.resizeDrawable(getContext(), unLikeDrawable, iconSize, iconSize);
this.likeDrawable = Utils.resizeDrawable(getContext(), likeDrawable, iconSize, iconSize);
tintDrawable(likeDrawable,hasLikeDrawableTintColor,likeDrawableTintColor);
tintDrawable(unLikeDrawable,hasUnlikeDrawableTintColor,unLikeDrawableTintColor);
}

/**
Expand Down Expand Up @@ -487,6 +561,11 @@ public void setCircleEndColorRes(@ColorRes int circleEndColor) {
circleView.setEndColor(this.circleEndColor);
}

public void setCircleEndColorInt(@ColorInt int circleEndColor) {
this.circleEndColor = circleEndColor;
circleView.setEndColor(circleEndColor);
}

/**
* This function updates the dots view and the circle
* view with the respective sizes based on the size
Expand Down
2 changes: 1 addition & 1 deletion likebutton/src/main/java/com/like/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static Bitmap getBitmap(Drawable drawable, int width, int height) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof VectorDrawableCompat) {
return getBitmap((VectorDrawableCompat) drawable, width, height);
} else if (drawable instanceof VectorDrawable) {
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) {
return getBitmap((VectorDrawable) drawable, width, height);
} else {
throw new IllegalArgumentException("Unsupported drawable type");
Expand Down
2 changes: 2 additions & 0 deletions likebutton/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<attr name="icon_size" format="dimension|reference"/>
<attr name="like_drawable" format="reference" />
<attr name="unlike_drawable" format="reference" />
<attr name="like_drawable_tint" format="color" />
<attr name="unlike_drawable_tint" format="color" />
<attr name="dots_primary_color" format="color|reference" />
<attr name="dots_secondary_color" format="color|reference" />
<attr name="circle_start_color" format="color|reference" />
Expand Down