Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy addition of other fonts #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.atermenji.android.iconictextview;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;

import com.atermenji.android.iconictextview.icon.Icon;

public class IconicFontDrawable extends Drawable {

private Context mContext;

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.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, final Icon icon) {
this(context);
updateIcon(icon);
}

public void setIcon(final Icon icon) {
updateIcon(icon);
invalidateSelf();
}

public void setIconColor(int color) {
mIconPaint.setColor(color);
invalidateSelf();
}

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) {
final Rect viewBounds = getBounds();

updatePaddingBounds(viewBounds);
updateTextSize(viewBounds);
offsetIcon(viewBounds);

mPath.close();

if (mDrawContour) {
canvas.drawPath(mPath, mContourPaint);
}

canvas.drawPath(mPath, mIconPaint);
}
}

@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}

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

@Override
public void setColorFilter(ColorFilter cf) {
mIconPaint.setColorFilter(cf);
}

private void updateIcon(Icon icon) {
mIcon = icon;
mIconUtfChars = Character.toChars(icon.getIconUtfValue());
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);
}
}
37 changes: 21 additions & 16 deletions sample/res/layout/activity_simple_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,41 @@
<Spinner
android:id="@+id/sp_icons"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" />

<com.atermenji.android.iconictextview.IconicTextView
android:id="@+id/itv_icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:gravity="center_horizontal|top"
android:textSize="600sp" />

<TextView
android:layout_height="wrap_content"/>

<View
android:id="@+id/view_icon"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"/>

<CheckBox
android:id="@+id/cb_draw_stroke"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:text="Draw Stroke"/>

<TextView
android:id="@+id/tv_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="left"
android:textSize="20sp"
android:textStyle="bold" />
android:textStyle="bold"
android:visibility="visible"/>

<SeekBar
android:id="@+id/sb_size"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"
android:visibility="visible"/>

<Button
android:id="@+id/bt_change_color"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:text="Change color" />

</LinearLayout>
15 changes: 7 additions & 8 deletions sample/res/layout/list_item_iconic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
android:layout_marginLeft="5dp"
android:textSize="20sp" />

<com.atermenji.android.iconictextview.IconicTextView
android:id="@+id/itv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:textSize="50sp" />
<View
android:id="@+id/view_icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@

import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

import com.atermenji.android.iconictextview.IconicTextView;
import com.atermenji.android.iconictextview.IconicFontDrawable;
import com.atermenji.android.iconictextview.icon.EntypoIcon;
import com.atermenji.android.iconictextview.icon.EntypoSocialIcon;
import com.atermenji.android.iconictextview.icon.FontAwesomeIcon;
Expand All @@ -42,14 +36,17 @@

public class SimpleSampleActivity extends Activity {

private static final int ICON_SIZE_MAX = 500;
private static final int ICON_SIZE_DEFAULT = 300;
private static final int ICON_PADDING_MAX = 500;
private static final int ICON_CONTOUR_WIDTH = 10;

private IconicTextView mIconicTextView;
private View mIconButton;
private Spinner mIconsSpinner;
private SeekBar mSizeSeekBar;
private SeekBar mPaddingSeekBar;
private Button mChangeColorButton;
private TextView mCurSizeTextView;
private TextView mCurPaddingTextView;
private CheckBox mDrawContourCheckBox;

private IconicFontDrawable mIconicFontDrawable;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -58,16 +55,20 @@ protected void onCreate(Bundle savedInstanceState) {
setTitle("Simple Sample");
setContentView(R.layout.activity_simple_sample);

mIconicTextView = (IconicTextView) findViewById(R.id.itv_icon);
mIconButton = findViewById(R.id.view_icon);
mIconsSpinner = (Spinner) findViewById(R.id.sp_icons);
mSizeSeekBar = (SeekBar) findViewById(R.id.sb_size);
mPaddingSeekBar = (SeekBar) findViewById(R.id.sb_size);
mChangeColorButton = (Button) findViewById(R.id.bt_change_color);
mCurSizeTextView = (TextView) findViewById(R.id.tv_size);
mCurPaddingTextView = (TextView) findViewById(R.id.tv_size);
mDrawContourCheckBox = (CheckBox) findViewById(R.id.cb_draw_stroke);

initSimpleSample();
}

private void initSimpleSample() {
mIconicFontDrawable = new IconicFontDrawable(this);
mIconButton.setBackground(mIconicFontDrawable);

List<Icon> icons = new ArrayList<Icon>();
icons.addAll(Arrays.asList(EntypoIcon.values()));
icons.addAll(Arrays.asList(EntypoSocialIcon.values()));
Expand All @@ -83,38 +84,51 @@ 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.setIconColor(Utils.randomColor());

mCurSizeTextView.setText("Size: " + ICON_SIZE_DEFAULT);
mCurPaddingTextView.setText("Padding: " + 0);

mSizeSeekBar.setMax(ICON_SIZE_MAX);
mSizeSeekBar.setProgress(ICON_SIZE_DEFAULT);
mSizeSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
mPaddingSeekBar.setMax(ICON_PADDING_MAX);
mPaddingSeekBar.setProgress(0);
mPaddingSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mIconicTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, progress);
mCurSizeTextView.setText("Size: " + progress);
mIconicFontDrawable.setIconPadding(progress);
mCurPaddingTextView.setText("Padding: " + progress);
}
});

mChangeColorButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mIconicTextView.setTextColor(Utils.randomColor());
mIconicFontDrawable.setIconColor(Utils.randomColor());
mIconicFontDrawable.setContourColor(Utils.randomColor());
}
});

mDrawContourCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
mIconicFontDrawable.setContour(Utils.randomColor(), ICON_CONTOUR_WIDTH);
}

mIconicFontDrawable.drawContour(isChecked);
}
});
}
Expand Down
Loading