Skip to content

Commit

Permalink
Merge pull request #5 from IvBaranov/methods_initials
Browse files Browse the repository at this point in the history
Methods initials
  • Loading branch information
IvBaranov committed Oct 18, 2015
2 parents 0865867 + e580a50 commit de8b984
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ public SimpleStringRecyclerViewAdapter(Context context, List<String> items, int
@Override public void onBindViewHolder(final ViewHolder holder, int position) {
switch (mType) {
case CONTACTS:
holder.mIcon.setLettersNumber(1);
holder.mIcon.setLetterSize(26);
holder.mIcon.setInitials(true);
holder.mIcon.setInitialsNumber(2);
holder.mIcon.setLetterSize(18);
break;
case COUNTRIES:
holder.mIcon.setLettersNumber(2);
holder.mIcon.setLetterSize(18);
holder.mIcon.setLettersNumber(3);
holder.mIcon.setLetterSize(16);
break;
}
holder.mBoundString = mValues.get(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class MaterialLetterIcon extends View {
private final static int DEFAULT_LETTER_SIZE = 26;
private final static String DEFAULT_FONT_PATH = "fonts/Roboto-Light.ttf";
private final static int DEFAULT_LETTERS_NUMBER = 1;
private final static boolean DEFAULT_INITIALS_STATE = false;
private final static int DEFAULT_INITIALS_NUMBER = 2;

private Context context;
private Paint mShapePaint;
Expand All @@ -34,6 +36,9 @@ public class MaterialLetterIcon extends View {
private int mLetterColor;
private int mLetterSize;
private int mLettersNumber;
private boolean mInitials;
private int mInitialsNumber;
private String mOriginalLetter;

public MaterialLetterIcon(Context context) {
super(context);
Expand Down Expand Up @@ -69,6 +74,8 @@ public MaterialLetterIcon(Context context, AttributeSet attrs, int defStyleAttr,
* <li>letter size = 26 sp</li>
* <li>number of letters = 1</li>
* <li>typeface = Roboto Light</li>
* <li>initials = false</li>
* <li>initials number = 2</li>
* </ul>
*/
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Expand All @@ -79,6 +86,8 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr, int def
mLetterColor = DEFAULT_LETTER_COLOR;
mLetterSize = DEFAULT_LETTER_SIZE;
mLettersNumber = DEFAULT_LETTERS_NUMBER;
mInitials = DEFAULT_INITIALS_STATE;
mInitialsNumber = DEFAULT_INITIALS_NUMBER;

mShapePaint = new Paint();
mShapePaint.setStyle(Paint.Style.FILL);
Expand All @@ -99,9 +108,13 @@ private void initAttributes(Context context, AttributeSet attributeSet) {
try {
mShapeColor =
attr.getColor(R.styleable.MaterialLetterIcon_mli_shape_color, DEFAULT_SHAPE_COLOR);
String attrLetter = attr.getString(R.styleable.MaterialLetterIcon_mli_letter);
if (attrLetter != null) {
setLetter(attrLetter);
mInitials =
attr.getBoolean(R.styleable.MaterialLetterIcon_mli_initials, DEFAULT_INITIALS_STATE);
mInitialsNumber = attr.getInt(R.styleable.MaterialLetterIcon_mli_initials_number,
DEFAULT_INITIALS_NUMBER);
mOriginalLetter = attr.getString(R.styleable.MaterialLetterIcon_mli_letter);
if (mOriginalLetter != null) {
setLetter(mOriginalLetter);
}
mShapeType = attr.getInt(R.styleable.MaterialLetterIcon_mli_shape_type, DEFAULT_SHAPE);
mLetterColor =
Expand Down Expand Up @@ -156,7 +169,13 @@ private void drawRect(Canvas canvas, int width, int height) {
private void drawLetter(Canvas canvas, float cx, float cy) {
mLetterPaint.setColor(mLetterColor);
mLetterPaint.setTextSize(spToPx(mLetterSize, context.getResources()));
mLetterPaint.getTextBounds(mLetter, 0, mLettersNumber, textBounds);
if (mInitials) {
mLetterPaint.getTextBounds(mLetter, 0,
mInitialsNumber > mLetter.length() ? mLetter.length() : mInitialsNumber, textBounds);
} else {
mLetterPaint.getTextBounds(mLetter, 0,
mLettersNumber > mLetter.length() ? mLetter.length() : mLettersNumber, textBounds);
}
canvas.drawText(mLetter, cx - textBounds.exactCenterX(), cy - textBounds.exactCenterY(),
mLetterPaint);
}
Expand Down Expand Up @@ -185,17 +204,37 @@ public void setShapeType(int type) {
/**
* Set letters.
*
* @param string a string to take first significant letter from or specified number of letters
* @param string a string to take letters from
*/
public void setLetter(String string) {
this.mLetter = String.valueOf(string.replaceAll("\\s+", "") //
.substring(0, mLettersNumber)) //
.toUpperCase();
if (string == null || string.isEmpty()) {
return;
}

this.mOriginalLetter = string;

int desireLength;
if (mInitials) {
String initials[] = string.split("\\s+");
StringBuilder initialsPlain = new StringBuilder(mLettersNumber);
for (String initial : initials) {
initialsPlain.append(initial.substring(0, 1));
}
this.mLetter = initialsPlain.toString();
desireLength = mInitialsNumber;
} else {
this.mLetter = String.valueOf(string.replaceAll("\\s+", ""));
desireLength = mLettersNumber;
}

this.mLetter =
mLetter.substring(0, desireLength > mLetter.length() ? mLetter.length() : desireLength)
.toUpperCase();
invalidate();
}

/**
* Set a letter color.
* Set letters color.
*
* @param color a color integer associated with a particular resource id
*/
Expand All @@ -205,7 +244,7 @@ public void setLetterColor(int color) {
}

/**
* Set a letter size.
* Set letters size.
*
* @param size size of letter in SP
*/
Expand All @@ -225,7 +264,7 @@ public void setLettersNumber(int num) {
}

/**
* Set a letter typeface.
* Set letters typeface.
*
* @param typeface a typeface to apply to letter
*/
Expand All @@ -234,6 +273,66 @@ public void setLetterTypeface(Typeface typeface) {
invalidate();
}

/**
* Set initials state.
*
* @param state if true, gets first letter of {@code initialsNumber} words in provided string
*/
public void setInitials(boolean state) {
this.mInitials = state;
setLetter(mOriginalLetter);
}

/**
* Set number of initials to show.
*
* @param initialsNumber number of initials to show
*/
public void setInitialsNumber(int initialsNumber) {
this.mInitialsNumber = initialsNumber;
setLetter(mOriginalLetter);
}

public Paint getShapePaint() {
return mShapePaint;
}

public Paint getLetterPaint() {
return mLetterPaint;
}

public int getShapeColor() {
return mShapeColor;
}

public int getShapeType() {
return mShapeType;
}

public String getLetter() {
return mLetter;
}

public int getLetterColor() {
return mLetterColor;
}

public int getLetterSize() {
return mLetterSize;
}

public int getLettersNumber() {
return mLettersNumber;
}

public boolean isInitials() {
return mInitials;
}

public int getInitialsNumber() {
return mInitialsNumber;
}

/**
* Convert sp to pixel.
*/
Expand All @@ -256,6 +355,8 @@ public static final class Builder {
private int mLetterSize = DEFAULT_LETTER_SIZE;
private int mLettersNumber = DEFAULT_LETTERS_NUMBER;
private Typeface mLetterTypeface;
private boolean mInitials = DEFAULT_INITIALS_STATE;
private int mInitialsNumber = DEFAULT_INITIALS_NUMBER;

public Builder(Context context) {
this.context = context;
Expand Down Expand Up @@ -297,6 +398,16 @@ public Builder letterTypeface(Typeface typeface) {
return this;
}

public Builder initials(boolean state) {
this.mInitials = state;
return this;
}

public Builder initialsNumber(int num) {
this.mInitialsNumber = num;
return this;
}

public MaterialLetterIcon create() {
MaterialLetterIcon icon = new MaterialLetterIcon(context);
icon.setShapeColor(mShapeColor);
Expand All @@ -306,6 +417,8 @@ public MaterialLetterIcon create() {
icon.setLetterSize(mLetterSize);
icon.setLettersNumber(mLettersNumber);
icon.setLetterTypeface(mLetterTypeface);
icon.setInitials(mInitials);
icon.setInitialsNumber(mInitialsNumber);

return icon;
}
Expand Down
16 changes: 9 additions & 7 deletions materiallettericon/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MaterialLetterIcon">
<attr name="mli_shape_color" format="color" />
<attr name="mli_shape_color" format="color"/>
<attr name="mli_shape_type" format="enum">
<enum name="circle" value="0" />
<enum name="rect" value="1" />
<enum name="circle" value="0"/>
<enum name="rect" value="1"/>
</attr>
<attr name="mli_letter" format="string" />
<attr name="mli_letter_color" format="color" />
<attr name="mli_letter_size" format="integer" />
<attr name="mli_letters_number" format="integer" />
<attr name="mli_letter" format="string"/>
<attr name="mli_letter_color" format="color"/>
<attr name="mli_letter_size" format="integer"/>
<attr name="mli_letters_number" format="integer"/>
<attr name="mli_initials" format="boolean"/>
<attr name="mli_initials_number" format="integer"/>
</declare-styleable>
</resources>

0 comments on commit de8b984

Please sign in to comment.