Skip to content

Commit

Permalink
fixed: added tint & state support
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudcerrato committed Dec 28, 2015
1 parent f1467ff commit 1d73ea3
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions library/src/main/java/com/mypopsy/drawable/ToggleDrawable.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.mypopsy.drawable;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;

import com.mypopsy.drawable.util.Bezier;
Expand All @@ -34,10 +39,14 @@ public class ToggleDrawable extends Drawable {
// The reported intrinsic size of the drawable
protected final int mSize;

private ColorStateList mTint;
private PorterDuff.Mode mTintMode = PorterDuff.Mode.SRC_IN;

private final ArrayList<Bezier> mStart = new ArrayList<>();
private final ArrayList<Bezier> mEnd = new ArrayList<>();
private final ArrayList<Bezier> mCurrent = new ArrayList<>();


public ToggleDrawable(Context context) {
this(context, 0, R.style.ToggleDrawable);
}
Expand All @@ -58,6 +67,8 @@ public ToggleDrawable(Context context, @AttrRes int defStyleAttr, @StyleRes int
mPaint.setStrokeJoin(Paint.Join.MITER);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeWidth(mStrokeWidth);

updateTintFilter();
}

public void reset() {
Expand Down Expand Up @@ -184,6 +195,41 @@ public void setProgress(float progress) {
}
}

@Override
public void setTintList(ColorStateList tint) {
mTint = tint;
updateTintFilter();
}

@Override
public void setTintMode(@NonNull PorterDuff.Mode tintMode) {
if(mTintMode != tintMode) {
mTintMode = tintMode;
updateTintFilter();
}
}

@Override
protected boolean onStateChange(int[] state) {
updateTintFilter();
return true;
}

@Override
public boolean isStateful() {
return mTint != null && mTint.isStateful();
}

private void updateTintFilter() {
if (mTint == null || mTintMode == null) {
setColorFilter(null);
return;
}

final int color = mTint.getColorForState(getState(), Color.TRANSPARENT);
setColorFilter(new PorterDuffColorFilter(color, mTintMode));
}

private static void lerp(Bezier a, Bezier b, Bezier out, float t) {
lerp(a.p1, b.p1, out.p1, t);
lerp(a.p2, b.p2, out.p2, t);
Expand Down

0 comments on commit 1d73ea3

Please sign in to comment.