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

Commit

Permalink
only convert background drawable if mutate_background is true
Browse files Browse the repository at this point in the history
also updated api
#34
  • Loading branch information
vinc3m1 committed Apr 13, 2014
1 parent 470e8d7 commit 6d734ca
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 51 deletions.
6 changes: 1 addition & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ buildscript {

allprojects {
group 'com.makeramen'
version '1.2.5-SNAPSHOT'
version '1.3.0-SNAPSHOT'
repositories {
mavenCentral()
}

tasks.withType(Compile) {
options.encoding = "UTF-8"
}
}
5 changes: 5 additions & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ dependencies {
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
2 changes: 1 addition & 1 deletion example/src/main/res/layout/rounded_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:scaleType="center"
app:corner_radius="30dip"
app:border_width="3dip"
app:is_oval="false"
app:oval="false"
app:border_color="@color/border"
/>

Expand Down
5 changes: 5 additions & 0 deletions roundedimageview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ sonatypePassword = project.hasProperty('sonatypePassword') ? sonatypePassword :
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}

task androidJavadocs(type: Javadoc) {
Expand Down
76 changes: 33 additions & 43 deletions roundedimageview/src/main/java/com/makeramen/RoundedImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class RoundedImageView extends ImageView {
private ColorStateList mBorderColor =
ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
private boolean mOval = false;
private boolean mRoundBackground = false;
private boolean mMutateBackground = false;

private int mResource;
private Drawable mDrawable;
Expand Down Expand Up @@ -79,11 +79,11 @@ public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
mBorderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
}

mRoundBackground = a.getBoolean(R.styleable.RoundedImageView_round_background, false);
mOval = a.getBoolean(R.styleable.RoundedImageView_is_oval, false);
mMutateBackground = a.getBoolean(R.styleable.RoundedImageView_mutate_background, false);
mOval = a.getBoolean(R.styleable.RoundedImageView_oval, false);

updateDrawableAttrs();
updateBackgroundDrawableAttrs();
updateBackgroundDrawableAttrs(true);

a.recycle();
}
Expand Down Expand Up @@ -114,9 +114,7 @@ public ScaleType getScaleType() {
*/
@Override
public void setScaleType(ScaleType scaleType) {
if (scaleType == null) {
throw new NullPointerException();
}
assert scaleType != null;

if (mScaleType != scaleType) {
mScaleType = scaleType;
Expand All @@ -137,7 +135,7 @@ public void setScaleType(ScaleType scaleType) {
}

updateDrawableAttrs();
updateBackgroundDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
}
}
Expand Down Expand Up @@ -175,9 +173,7 @@ public void setImageResource(int resId) {

private Drawable resolveResource() {
Resources rsrc = getResources();
if (rsrc == null) {
return null;
}
if (rsrc == null) { return null; }

Drawable d = null;

Expand All @@ -202,27 +198,29 @@ private void updateDrawableAttrs() {
updateAttrs(mDrawable, false);
}

private void updateBackgroundDrawableAttrs() {
updateAttrs(mBackgroundDrawable, true);
private void updateBackgroundDrawableAttrs(boolean convert) {
if (mMutateBackground) {
if (convert) {
mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);
}
updateAttrs(mBackgroundDrawable, true);
}
}

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

if (drawable instanceof RoundedDrawable) {
((RoundedDrawable) drawable)
.setScaleType(mScaleType)
.setCornerRadius(background && !mRoundBackground ? 0 : mCornerRadius)
.setBorderWidth(background && !mRoundBackground ? 0 : mBorderWidth)
.setCornerRadius(background && !mMutateBackground ? 0 : mCornerRadius)
.setBorderWidth(background && !mMutateBackground ? 0 : mBorderWidth)
.setBorderColors(mBorderColor)
.setOval(mOval);
} else if (drawable instanceof LayerDrawable) {
// loop through layers to and set drawable attrs
LayerDrawable ld = ((LayerDrawable) drawable);
int layers = ld.getNumberOfLayers();
for (int i = 0; i < layers; i++) {
for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
updateAttrs(ld.getDrawable(i), background);
}
}
Expand All @@ -231,8 +229,8 @@ private void updateAttrs(Drawable drawable, boolean background) {
@Override
@Deprecated
public void setBackgroundDrawable(Drawable background) {
mBackgroundDrawable = RoundedDrawable.fromDrawable(background);
updateBackgroundDrawableAttrs();
mBackgroundDrawable = background;
updateBackgroundDrawableAttrs(true);
super.setBackgroundDrawable(mBackgroundDrawable);
}

Expand All @@ -241,27 +239,23 @@ public int getCornerRadius() {
}

public void setCornerRadius(int radius) {
if (mCornerRadius == radius) {
return;
}
if (mCornerRadius == radius) { return; }

mCornerRadius = radius;
updateDrawableAttrs();
updateBackgroundDrawableAttrs();
updateBackgroundDrawableAttrs(false);
}

public int getBorderWidth() {
return mBorderWidth;
}

public void setBorderWidth(int width) {
if (mBorderWidth == width) {
return;
}
if (mBorderWidth == width) { return; }

mBorderWidth = width;
updateDrawableAttrs();
updateBackgroundDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
}

Expand All @@ -278,14 +272,12 @@ public ColorStateList getBorderColors() {
}

public void setBorderColors(ColorStateList colors) {
if (mBorderColor.equals(colors)) {
return;
}
if (mBorderColor.equals(colors)) { return; }

mBorderColor =
(colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
updateDrawableAttrs();
updateBackgroundDrawableAttrs();
updateBackgroundDrawableAttrs(false);
if (mBorderWidth > 0) {
invalidate();
}
Expand All @@ -298,21 +290,19 @@ public boolean isOval() {
public void setOval(boolean oval) {
mOval = oval;
updateDrawableAttrs();
updateBackgroundDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
}

public boolean isRoundBackground() {
return mRoundBackground;
public boolean isMutateBackground() {
return mMutateBackground;
}

public void setRoundBackground(boolean roundBackground) {
if (mRoundBackground == roundBackground) {
return;
}
public void setMutateBackground(boolean mutate) {
if (mMutateBackground == mutate) { return; }

mRoundBackground = roundBackground;
updateBackgroundDrawableAttrs();
mMutateBackground = mutate;
updateBackgroundDrawableAttrs(true);
invalidate();
}
}
4 changes: 2 additions & 2 deletions roundedimageview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<attr name="corner_radius" format="dimension" />
<attr name="border_width" format="dimension" />
<attr name="border_color" format="color" />
<attr name="round_background" format="boolean" />
<attr name="is_oval" format="boolean" />
<attr name="mutate_background" format="boolean" />
<attr name="oval" format="boolean" />
<attr name="android:scaleType" />
</declare-styleable>
</resources>

0 comments on commit 6d734ca

Please sign in to comment.