什么是IconFont
IconFont是一套图标库方案,它将常用的图标保存到一个字库文件当中
字库中每个字符的字形,就是图标的外形,这样就能将图标作为一个文字字符来显示
这套方案思路有点奇特,它的主要作用,是方便对图标进行统一打包、分发,这比诸多零散图片要方便
当我们需要使用某个图标时,我们根据Unicode找到对应图标在字库文件当中的位置,然后显示出这个文字即可
具体的使用方法,百度有很多,这里我们主要是套路,怎么让安卓支持这套技术
使用方式
先看结果,实现上虽然有点麻烦,但最终提供给大家使用的API是非常简单的
有TextView和Drawable两种方式来显示图标,支持渐变、描边、阴影、大小的设置
一般用TextView就行了,Drawable方式是用于特殊用途的,比如地图控件,经常通过自定义Drawable来显示标记
IconFontTextView view = new IconFontTextView(ctx);
view.fillColor(Colors.YELLOW, Colors.ORANGE).strokeColor(Colors.RED_50, 3).textSize(60).text("001", 0xE624).redraw();
IconFontDrawable drawable = new IconFontDrawable();
drawable.fillColor(Colors.YELLOW, Colors.ORANGE).strokeColor(Colors.RED_50, 3).textSize(120).text("001", 0xE624).redraw();
自定义控件核心源码
package com.easing.android.IconFont;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import com.easing.commons.android.manager.Dimens;
import com.easing.commons.android.manager.Fonts;
import com.easing.commons.android.ui.optimize.CanvasPainter;
import com.easing.commons.android.value.color.Colors;
@SuppressWarnings("all")
public class IconFontTextView extends View {
Paint fillPaint = new Paint();
Paint strokePaint = new Paint();
String iconChar;
int width;
int height;
Typeface typeface;
float textSize;
int startColor;
int endColor;
LinearGradient gradient;
int strokeColor;
float strokeWidth;
public IconFontTextView(Context context) {
this(context, null);
}
public IconFontTextView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init(context, attributeSet);
}
protected void init(Context context, AttributeSet attributeSet) {
fillPaint.setAntiAlias(true);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setShadowLayer(Dimens.toPx(4), Dimens.toPx(2), Dimens.toPx(2), Colors.BLACK);
strokePaint.setAntiAlias(true);
strokePaint.setStyle(Paint.Style.STROKE);
TypedArray attrs = context.obtainStyledAttributes(attributeSet, R.styleable.IconFontTextView);
int unicode = attrs.getInteger(R.styleable.IconFontTextView_IconFontTextView_text, 0);
if (unicode != 0)
iconChar = "" + (char) unicode;
String font = attrs.getString(R.styleable.IconFontTextView_IconFontTextView_textFont);
if (font != null) {
typeface = Fonts.create("iconfont/" + font + ".ttf");
fillPaint.setTypeface(typeface);
strokePaint.setTypeface(typeface);
}
textSize = attrs.getDimension(R.styleable.IconFontTextView_IconFontTextView_textSize, Dimens.toDp(20));
fillPaint.setTextSize(textSize);
strokePaint.setTextSize(textSize);
startColor = attrs.getColor(R.styleable.IconFontTextView_IconFontTextView_startColor, 0xFF000000);
endColor = attrs.getColor(R.styleable.IconFontTextView_IconFontTextView_endColor, 0xFF000000);
strokeColor = attrs.getColor(R.styleable.IconFontTextView_IconFontTextView_strokeColor, 0xFFFFFFFF);
strokeWidth = attrs.getDimension(R.styleable.IconFontTextView_IconFontTextView_strokeWidth, Dimens.toDp(20));
strokePaint.setColor(strokeColor);
strokePaint.setStrokeWidth(strokeWidth);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth();
height = getMeasuredHeight();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
if (iconChar == null)
return;
float textWidth = CanvasPainter.getTextWidth(fillPaint, iconChar);
float textHeight = CanvasPainter.getTextHeight(fillPaint);
float x = width / 2 - textWidth / 2;
float y = height / 2 - textHeight / 2;
gradient = new LinearGradient(x, y, x + textWidth, y + textHeight, new int[]{startColor, endColor}, new float[]{0F, 1F}, Shader.TileMode.CLAMP);
fillPaint.setShader(gradient);
CanvasPainter.drawTextFromCenter(canvas, fillPaint, iconChar, width / 2, height / 2);
CanvasPainter.drawTextFromCenter(canvas, strokePaint, iconChar, width / 2, height / 2);
}
public IconFontTextView redraw() {
invalidate();
return this;
}
public IconFontTextView fillColor(int color) {
this.startColor = 0x66000000 + color & 0x00FFFFFF;
this.endColor = color | 0xFF000000;
return this;
}
public IconFontTextView fillColor(int startColor, int endColor) {
this.startColor = startColor;
this.endColor = endColor;
return this;
}
public IconFontTextView strokeColor(int strokeColor, float strokeWidth) {
this.strokeColor = strokeColor;
this.strokeWidth = Dimens.toPx(strokeWidth);
strokePaint.setColor(strokeColor);
strokePaint.setStrokeWidth(this.strokeWidth);
return this;
}
public IconFontTextView textSize(int textSize) {
this.textSize = Dimens.toPx(textSize);
fillPaint.setTextSize(this.textSize);
strokePaint.setTextSize(this.textSize);
return this;
}
public IconFontTextView text(String font, int unicode) {
typeface = Fonts.create("iconfont/" + font + ".ttf");
fillPaint.setTypeface(typeface);
strokePaint.setTypeface(typeface);
iconChar = "" + (char) unicode;
return this;
}
public IconFontTextView text(String font, String unicodeHex) {
int unicode = Integer.parseInt(unicodeHex, 16);
return text(font, unicode);
}
}
package com.easing.android.IconFont;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import com.easing.commons.android.manager.Dimens;
import com.easing.commons.android.manager.Fonts;
import com.easing.commons.android.ui.optimize.CanvasPainter;
import com.easing.commons.android.value.color.Colors;
@SuppressWarnings("all")
public class IconFontDrawable extends Drawable {
Paint fillPaint = new Paint();
Paint strokePaint = new Paint();
String iconChar;
int width;
int height;
float scale = 1F;
Typeface typeface;
float textSize;
int startColor;
int endColor;
LinearGradient gradient;
int strokeColor;
float strokeWidth;
public IconFontDrawable() {
fillPaint.setAntiAlias(true);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setShadowLayer(Dimens.toPx(4), Dimens.toPx(2), Dimens.toPx(2), Colors.BLACK);
strokePaint.setAntiAlias(true);
strokePaint.setStyle(Paint.Style.STROKE);
}
@Override
public void draw(Canvas canvas) {
if (iconChar == null)
return;
gradient = new LinearGradient(0, 0, width, height, new int[]{startColor, endColor}, new float[]{0F, 1F}, Shader.TileMode.CLAMP);
fillPaint.setShader(gradient);
CanvasPainter.drawTextFromCenter(canvas, fillPaint, iconChar, width / 2, height / 2);
CanvasPainter.drawTextFromCenter(canvas, strokePaint, iconChar, width / 2, height / 2);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public void setAlpha(int alpha) {
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
}
@Override
public int getIntrinsicWidth() {
return width;
}
@Override
public int getIntrinsicHeight() {
return height;
}
public IconFontDrawable redraw() {
width = (int) CanvasPainter.getTextWidth(fillPaint, iconChar);
height = (int) CanvasPainter.getTextHeight(fillPaint);
invalidateSelf();
return this;
}
public IconFontDrawable fillColor(int color) {
this.startColor = 0x66000000 + color & 0x00FFFFFF;
this.endColor = color | 0xFF000000;
return this;
}
public IconFontDrawable fillColor(int startColor, int endColor) {
this.startColor = startColor;
this.endColor = endColor;
return this;
}
public IconFontDrawable strokeColor(int strokeColor, float strokeWidth) {
this.strokeColor = strokeColor;
this.strokeWidth = Dimens.toPx(strokeWidth);
strokePaint.setColor(strokeColor);
strokePaint.setStrokeWidth(this.strokeWidth);
return this;
}
public IconFontDrawable textSize(int textSize) {
this.textSize = Dimens.toPx(textSize);
fillPaint.setTextSize(this.textSize);
strokePaint.setTextSize(this.textSize);
return this;
}
public IconFontDrawable text(String font, int unicode) {
typeface = Fonts.create("iconfont/" + font + ".ttf");
fillPaint.setTypeface(typeface);
strokePaint.setTypeface(typeface);
iconChar = "" + (char) unicode;
return this;
}
public IconFontDrawable text(String font, String unicodeHex) {
int unicode = Integer.parseInt(unicodeHex, 16);
return text(font, unicode);
}
}
源码下载
IconFont for Android.zip
|