Skip to content

Commit

Permalink
Add Drawable parameter support to setIconLeft/Right().
Browse files Browse the repository at this point in the history
  • Loading branch information
rengwuxian committed Apr 28, 2015
1 parent 58e3c02 commit 83d6abd
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
Expand Down Expand Up @@ -412,11 +413,11 @@ private void init(Context context, AttributeSet attrs) {
typedArray.recycle();

int[] paddings = new int[]{
android.R.attr.padding, // 0
android.R.attr.paddingLeft, // 1
android.R.attr.paddingTop, // 2
android.R.attr.paddingRight, // 3
android.R.attr.paddingBottom // 4
android.R.attr.padding, // 0
android.R.attr.paddingLeft, // 1
android.R.attr.paddingTop, // 2
android.R.attr.paddingRight, // 3
android.R.attr.paddingBottom // 4
};
TypedArray paddingsTypedArray = context.obtainStyledAttributes(attrs, paddings);
int padding = paddingsTypedArray.getDimensionPixelSize(0, 0);
Expand Down Expand Up @@ -491,6 +492,11 @@ public void setIconLeft(@DrawableRes int res) {
initPadding();
}

public void setIconLeft(Drawable drawable) {
iconLeftBitmaps = generateIconBitmaps(drawable);
initPadding();
}

public void setIconLeft(Bitmap bitmap) {
iconLeftBitmaps = generateIconBitmaps(bitmap);
initPadding();
Expand All @@ -501,6 +507,11 @@ public void setIconRight(@DrawableRes int res) {
initPadding();
}

public void setIconRight(Drawable drawable) {
iconRightBitmaps = generateIconBitmaps(drawable);
initPadding();
}

public void setIconRight(Bitmap bitmap) {
iconRightBitmaps = generateIconBitmaps(bitmap);
initPadding();
Expand Down Expand Up @@ -528,6 +539,16 @@ private Bitmap[] generateIconBitmaps(@DrawableRes int origin) {
return generateIconBitmaps(BitmapFactory.decodeResource(getResources(), origin, options));
}

private Bitmap[] generateIconBitmaps(Drawable drawable) {
if (drawable == null)
return null;
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return generateIconBitmaps(Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, false));
}

private Bitmap[] generateIconBitmaps(Bitmap origin) {
if (origin == null) {
return null;
Expand Down Expand Up @@ -786,8 +807,8 @@ private boolean adjustBottomLines() {
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
Expand Down Expand Up @@ -491,6 +492,11 @@ public void setIconLeft(@DrawableRes int res) {
initPadding();
}

public void setIconLeft(Drawable drawable) {
iconLeftBitmaps = generateIconBitmaps(drawable);
initPadding();
}

public void setIconLeft(Bitmap bitmap) {
iconLeftBitmaps = generateIconBitmaps(bitmap);
initPadding();
Expand All @@ -501,6 +507,11 @@ public void setIconRight(@DrawableRes int res) {
initPadding();
}

public void setIconRight(Drawable drawable) {
iconRightBitmaps = generateIconBitmaps(drawable);
initPadding();
}

public void setIconRight(Bitmap bitmap) {
iconRightBitmaps = generateIconBitmaps(bitmap);
initPadding();
Expand Down Expand Up @@ -528,6 +539,16 @@ private Bitmap[] generateIconBitmaps(@DrawableRes int origin) {
return generateIconBitmaps(BitmapFactory.decodeResource(getResources(), origin, options));
}

private Bitmap[] generateIconBitmaps(Drawable drawable) {
if (drawable == null)
return null;
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return generateIconBitmaps(Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, false));
}

private Bitmap[] generateIconBitmaps(Bitmap origin) {
if (origin == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
Expand Down Expand Up @@ -409,11 +410,11 @@ private void init(Context context, AttributeSet attrs) {
typedArray.recycle();

int[] paddings = new int[]{
android.R.attr.padding, // 0
android.R.attr.paddingLeft, // 1
android.R.attr.paddingTop, // 2
android.R.attr.paddingRight, // 3
android.R.attr.paddingBottom // 4
android.R.attr.padding, // 0
android.R.attr.paddingLeft, // 1
android.R.attr.paddingTop, // 2
android.R.attr.paddingRight, // 3
android.R.attr.paddingBottom // 4
};
TypedArray paddingsTypedArray = context.obtainStyledAttributes(attrs, paddings);
int padding = paddingsTypedArray.getDimensionPixelSize(0, 0);
Expand Down Expand Up @@ -458,24 +459,24 @@ private void initText() {

private void initTextWatcher() {
addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
checkCharactersCount();
if (autoValidate) {
validate();
} else {
setError(null);
}
postInvalidate();
@Override
public void afterTextChanged(Editable s) {
checkCharactersCount();
if (autoValidate) {
validate();
} else {
setError(null);
}
postInvalidate();
}
});
}

Expand All @@ -488,6 +489,11 @@ public void setIconLeft(@DrawableRes int res) {
initPadding();
}

public void setIconLeft(Drawable drawable) {
iconLeftBitmaps = generateIconBitmaps(drawable);
initPadding();
}

public void setIconLeft(Bitmap bitmap) {
iconLeftBitmaps = generateIconBitmaps(bitmap);
initPadding();
Expand All @@ -498,6 +504,11 @@ public void setIconRight(@DrawableRes int res) {
initPadding();
}

public void setIconRight(Drawable drawable) {
iconRightBitmaps = generateIconBitmaps(drawable);
initPadding();
}

public void setIconRight(Bitmap bitmap) {
iconRightBitmaps = generateIconBitmaps(bitmap);
initPadding();
Expand Down Expand Up @@ -525,6 +536,16 @@ private Bitmap[] generateIconBitmaps(@DrawableRes int origin) {
return generateIconBitmaps(BitmapFactory.decodeResource(getResources(), origin, options));
}

private Bitmap[] generateIconBitmaps(Drawable drawable) {
if (drawable == null)
return null;
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return generateIconBitmaps(Bitmap.createScaledBitmap(bitmap, iconSize, iconSize, false));
}

private Bitmap[] generateIconBitmaps(Bitmap origin) {
if (origin == null) {
return null;
Expand Down Expand Up @@ -783,8 +804,8 @@ private boolean adjustBottomLines() {
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ?
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ?
Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
Expand Down

0 comments on commit 83d6abd

Please sign in to comment.