Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

Commit

Permalink
colorfilter support #73
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc3m1 committed Jan 26, 2015
1 parent f1bccea commit 00bd0c4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.android.tools.build:gradle:1.0.1'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 Vincent Mi
* Copyright (C) 2015 Vincent Mi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -292,12 +292,20 @@ public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

@Override public int getAlpha() {
return mBitmapPaint.getAlpha();
}

@Override
public void setAlpha(int alpha) {
mBitmapPaint.setAlpha(alpha);
invalidateSelf();
}

@Override public ColorFilter getColorFilter() {
return mBitmapPaint.getColorFilter();
}

@Override
public void setColorFilter(ColorFilter cf) {
mBitmapPaint.setColorFilter(cf);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 Vincent Mi
* Copyright (C) 2015 Vincent Mi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.ColorFilter;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
Expand Down Expand Up @@ -62,6 +63,10 @@ public class RoundedImageView extends ImageView {
private Shader.TileMode tileModeX = DEFAULT_TILE_MODE;
private Shader.TileMode tileModeY = DEFAULT_TILE_MODE;

private ColorFilter mColorFilter = null;
private boolean mHasColorFilter = false;
private boolean mColorMod = false;

private int mResource;
private Drawable mDrawable;
private Drawable mBackgroundDrawable;
Expand Down Expand Up @@ -262,6 +267,31 @@ private void updateBackgroundDrawableAttrs(boolean convert) {
}
}

@Override public void setColorFilter(ColorFilter cf) {
if (mColorFilter != cf) {
mColorFilter = cf;
mHasColorFilter = true;
mColorMod = true;
applyColorMod();
invalidate();
}
}

private void applyColorMod() {
// Only mutate and apply when modifications have occurred. This should
// not reset the mColorMod flag, since these filters need to be
// re-applied if the Drawable is changed.
if (mDrawable != null && mColorMod) {
mDrawable = mDrawable.mutate();
if (mHasColorFilter) {
mDrawable.setColorFilter(mColorFilter);
}
// TODO: support, eventually...
//mDrawable.setXfermode(mXfermode);
//mDrawable.setAlpha(mAlpha * mViewAlphaScale >> 8);
}
}

private void updateAttrs(Drawable drawable) {
if (drawable == null) { return; }

Expand All @@ -274,6 +304,7 @@ private void updateAttrs(Drawable drawable) {
.setOval(isOval)
.setTileModeX(tileModeX)
.setTileModeY(tileModeY);
applyColorMod();
} else if (drawable instanceof LayerDrawable) {
// loop through layers to and set drawable attrs
LayerDrawable ld = ((LayerDrawable) drawable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 Vincent Mi
* Copyright (C) 2015 Vincent Mi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 00bd0c4

Please sign in to comment.