From b7ee9845c2fa0eed41a96d62589fd8f533d3be3f Mon Sep 17 00:00:00 2001 From: Artur Termenji Date: Mon, 14 Jan 2013 19:35:16 +0200 Subject: [PATCH 1/2] introduced IconicFontDrawable --- .../iconictextview/IconicFontDrawable.java | 73 +++++++++++++++++++ sample/res/layout/activity_simple_sample.xml | 10 +-- .../sample/SimpleSampleActivity.java | 23 +++--- 3 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java diff --git a/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java b/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java new file mode 100644 index 0000000..05f382a --- /dev/null +++ b/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java @@ -0,0 +1,73 @@ +package com.atermenji.android.iconictextview; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.ColorFilter; +import android.graphics.Paint; +import android.graphics.PixelFormat; +import android.graphics.drawable.Drawable; + +import com.atermenji.android.iconictextview.icon.Icon; + +public class IconicFontDrawable extends Drawable { + + private Context mContext; + + private Paint mPaint; + private Icon mIcon; + private char[] mIconUtfChars; + + public IconicFontDrawable(Context context) { + mContext = context; + mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + } + + public IconicFontDrawable(Context context, Icon icon) { + this(context); + init(icon); + } + + public void setIcon(final Icon icon) { + init(icon); + } + + public void setIconSize(final float iconSize) { + mPaint.setTextSize(iconSize); + invalidateSelf(); + } + + public void setIconColor(final int color) { + mPaint.setColor(color); + invalidateSelf(); + } + + @Override + public void draw(Canvas canvas) { + if (mIcon != null) { + canvas.drawText(mIconUtfChars, 0, mIconUtfChars.length, canvas.getWidth() / 2, canvas.getHeight() / 2, mPaint); + } + } + + @Override + public int getOpacity() { + return PixelFormat.OPAQUE; + } + + @Override + public void setAlpha(int alpha) { + mPaint.setAlpha(alpha); + } + + @Override + public void setColorFilter(ColorFilter cf) { + mPaint.setColorFilter(cf); + } + + private void init(final Icon icon) { + mIcon = icon; + mIconUtfChars = Character.toChars(icon.getIconUtfValue()); + mPaint.setTypeface(mIcon.getIconicTypeface().getTypeface(mContext)); + mPaint.setTextAlign(Paint.Align.CENTER); + } +} diff --git a/sample/res/layout/activity_simple_sample.xml b/sample/res/layout/activity_simple_sample.xml index 47136e6..2a1fa77 100644 --- a/sample/res/layout/activity_simple_sample.xml +++ b/sample/res/layout/activity_simple_sample.xml @@ -26,14 +26,14 @@ android:layout_height="0dp" android:layout_weight="0.1" /> - - + android:gravity="center" + android:scaleType="fitCenter" /> + icons = new ArrayList(); icons.addAll(Arrays.asList(EntypoIcon.values())); icons.addAll(Arrays.asList(EntypoSocialIcon.values())); @@ -83,15 +88,15 @@ private void initSimpleSample() { @Override public void onItemSelected(AdapterView parent, View view, int pos, long id) { Icon icon = adapter.getItem(pos); - mIconicTextView.setIcon(icon); + mIconicFontDrawable.setIcon(icon); } @Override public void onNothingSelected(AdapterView parent) { } }); - mIconicTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, ICON_SIZE_DEFAULT); - mIconicTextView.setTextColor(Utils.randomColor()); + mIconicFontDrawable.setIconSize(ICON_SIZE_DEFAULT); + mIconicFontDrawable.setIconColor(Utils.randomColor()); mCurSizeTextView.setText("Size: " + ICON_SIZE_DEFAULT); @@ -106,7 +111,7 @@ public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - mIconicTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, progress); + mIconicFontDrawable.setIconSize(progress); mCurSizeTextView.setText("Size: " + progress); } }); @@ -114,7 +119,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mChangeColorButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - mIconicTextView.setTextColor(Utils.randomColor()); + mIconicFontDrawable.setIconColor(Utils.randomColor()); } }); } From ae822cf833c34b2ff2e6a65534519dabc1636ad0 Mon Sep 17 00:00:00 2001 From: Artur Termenji Date: Sat, 23 Mar 2013 18:48:46 +0200 Subject: [PATCH 2/2] implementation of IconicFontDrawable. Samples updated --- .../iconictextview/IconicFontDrawable.java | 154 +++++++++++++++--- sample/res/layout/activity_simple_sample.xml | 37 +++-- sample/res/layout/list_item_iconic.xml | 15 +- .../sample/SimpleSampleActivity.java | 57 ++++--- .../sample/TypefaceSampleActivity.java | 14 +- 5 files changed, 197 insertions(+), 80 deletions(-) diff --git a/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java b/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java index 05f382a..f2f50fd 100644 --- a/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java +++ b/library/src/com/atermenji/android/iconictextview/IconicFontDrawable.java @@ -1,11 +1,7 @@ package com.atermenji.android.iconictextview; import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.ColorFilter; -import android.graphics.Paint; -import android.graphics.PixelFormat; +import android.graphics.*; import android.graphics.drawable.Drawable; import com.atermenji.android.iconictextview.icon.Icon; @@ -13,39 +9,102 @@ public class IconicFontDrawable extends Drawable { private Context mContext; - - private Paint mPaint; + + private Paint mIconPaint; + private Paint mContourPaint; + + private Rect mPaddingBounds; + private RectF mPathBounds; + + private Path mPath; + + private int mIconPadding; + private int mContourWidth; + + private boolean mDrawContour; + private Icon mIcon; private char[] mIconUtfChars; - + public IconicFontDrawable(Context context) { - mContext = context; - mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mContext = context.getApplicationContext(); + + mIconPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + + mContourPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mContourPaint.setStyle(Paint.Style.STROKE); + + mPath = new Path(); + + mPathBounds = new RectF(); + mPaddingBounds = new Rect(); } - public IconicFontDrawable(Context context, Icon icon) { + public IconicFontDrawable(Context context, final Icon icon) { this(context); - init(icon); + updateIcon(icon); } public void setIcon(final Icon icon) { - init(icon); + updateIcon(icon); + invalidateSelf(); } - - public void setIconSize(final float iconSize) { - mPaint.setTextSize(iconSize); + + public void setIconColor(int color) { + mIconPaint.setColor(color); invalidateSelf(); } - - public void setIconColor(final int color) { - mPaint.setColor(color); + + public void setIconPadding(int iconPadding) { + mIconPadding = iconPadding + mContourWidth; invalidateSelf(); } - + + public void setContour(int contourColor, int contourWidth) { + setContourColor(contourColor); + setContourWidth(contourWidth); + invalidateSelf(); + } + + public void setContourColor(int contourColor) { + mContourPaint.setColor(contourColor); + invalidateSelf(); + } + + public void setContourWidth(int contourWidth) { + mContourWidth = contourWidth; + mContourPaint.setStrokeWidth(mContourWidth); + invalidateSelf(); + } + + public void drawContour(boolean drawStroke) { + mDrawContour = drawStroke; + + if (mDrawContour) { + mIconPadding += mContourWidth; + } else { + mIconPadding -= mContourWidth; + } + + invalidateSelf(); + } + @Override public void draw(Canvas canvas) { if (mIcon != null) { - canvas.drawText(mIconUtfChars, 0, mIconUtfChars.length, canvas.getWidth() / 2, canvas.getHeight() / 2, mPaint); + final Rect viewBounds = getBounds(); + + updatePaddingBounds(viewBounds); + updateTextSize(viewBounds); + offsetIcon(viewBounds); + + mPath.close(); + + if (mDrawContour) { + canvas.drawPath(mPath, mContourPaint); + } + + canvas.drawPath(mPath, mIconPaint); } } @@ -56,18 +115,59 @@ public int getOpacity() { @Override public void setAlpha(int alpha) { - mPaint.setAlpha(alpha); + mIconPaint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter cf) { - mPaint.setColorFilter(cf); + mIconPaint.setColorFilter(cf); } - - private void init(final Icon icon) { + + private void updateIcon(Icon icon) { mIcon = icon; mIconUtfChars = Character.toChars(icon.getIconUtfValue()); - mPaint.setTypeface(mIcon.getIconicTypeface().getTypeface(mContext)); - mPaint.setTextAlign(Paint.Align.CENTER); + mIconPaint.setTypeface(mIcon.getIconicTypeface().getTypeface(mContext)); + } + + private void updatePaddingBounds(Rect viewBounds) { + if (mIconPadding >= 0 + && !(mIconPadding * 2 > viewBounds.width()) + && !(mIconPadding * 2 > viewBounds.height())) { + mPaddingBounds.set( + viewBounds.left + mIconPadding, + viewBounds.top + mIconPadding, + viewBounds.right - mIconPadding, + viewBounds.bottom - mIconPadding); + } + } + + private void updateTextSize(Rect viewBounds) { + float textSize = (float) viewBounds.height() * 2; + mIconPaint.setTextSize(textSize); + + mIconPaint.getTextPath(mIconUtfChars, 0, mIconUtfChars.length, + 0, viewBounds.height(), mPath); + mPath.computeBounds(mPathBounds, true); + + float deltaWidth = ((float) mPaddingBounds.width() / mPathBounds.width()); + float deltaHeight = ((float) mPaddingBounds.height() / mPathBounds.height()); + float delta = (deltaWidth < deltaHeight) ? deltaWidth : deltaHeight; + textSize *= delta; + + mIconPaint.setTextSize(textSize); + + mIconPaint.getTextPath(mIconUtfChars, 0, mIconUtfChars.length, + 0, viewBounds.height(), mPath); + mPath.computeBounds(mPathBounds, true); + } + + private void offsetIcon(Rect viewBounds) { + float startX = viewBounds.centerX() - (mPathBounds.width() / 2); + float offsetX = startX -mPathBounds.left; + + float startY = viewBounds.centerY() - (mPathBounds.height() / 2); + float offsetY = startY - (mPathBounds.top); + + mPath.offset(offsetX, offsetY); } } diff --git a/sample/res/layout/activity_simple_sample.xml b/sample/res/layout/activity_simple_sample.xml index 2a1fa77..a6e3e0b 100644 --- a/sample/res/layout/activity_simple_sample.xml +++ b/sample/res/layout/activity_simple_sample.xml @@ -23,36 +23,41 @@ - - - - + + + + + + + android:textStyle="bold" + android:visibility="visible"/> + android:layout_height="wrap_content" + android:visibility="visible"/>