ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何在Android中将字体文件中的图标用作绘图对象

2019-10-11 10:28:15  阅读:235  来源: 互联网

标签:android fonts android-actionbar typeface


我有一个字体文件,正在通过自定义TextView在布局文件中使用其图标.

我创建了一个自定义类:
    CustomFontTextView类扩展了TextView

例如,字体文件Sample.ttf中的图标作为字符串资源存在:

<string name="icon">&#xe038;</string>

在布局中,我可以将其用作:

<com.sec.mywash.views.CustomFontTextView
custom:custom_typeface="sample_font"
 android:text="@string/icon"/>.

但是,我的要求是更改在style.xml中设置为项目的操作栏中的Home up按钮:

<item "android:homeAsUpIndicator">@drawable/....</item> 

如何使用style.xml中的字体文件中的图标图像(需要绘制).

解决方法:

您可以创建自己的drawable类来执行此操作,就像这样

/** Embed an icon into a Drawable that can be used as TextView icons, or ActionBar icons.
 *
 * new IconDrawable(context, IconValue.icon_star)
 *           .colorRes(R.color.white)
 *           .actionBarSize();
 * 
 * If you don't set the size of the drawable, it will use the size
 * that is given to him. Note that in an ActionBar, if you don't
 * set the size explicitly it uses 0, so please use actionBarSize().
 */

public class FontIconDrawable extends Drawable {

public static int ANDROID_ACTIONBAR_ICON_SIZE_DP = 24;

private final Context context;

private final String icon;

private TextPaint paint;

private int size = -1;

private int alpha = 255;

/**
 * Create an IconDrawable.
 *
 * @param context Your activity or application context.
 * @param icon    The icon you want this drawable to display.
 */
public FontIconDrawable(Context context, String icon, Typeface typeface) {
    this.context = context;
    this.icon = icon;
    paint = new TextPaint();
    paint.setTypeface(typeface);
    paint.setStyle(Paint.Style.STROKE);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setUnderlineText(false);
    paint.setColor(Color.WHITE);
    paint.setAntiAlias(true);
}

/**
 * Set the size of this icon to the standard Android ActionBar.
 *
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable actionBarSize() {
    return sizeDp(ANDROID_ACTIONBAR_ICON_SIZE_DP);
}

/**
 * Set the size of the drawable.
 *
 * @param dimenRes The dimension resource.
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable sizeRes(int dimenRes) {
    return sizePx(context.getResources().getDimensionPixelSize(dimenRes));
}

/**
 * Set the size of the drawable.
 *
 * @param size The size in density-independent pixels (dp).
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable sizeDp(int size) {
    return sizePx(dpToPx(context.getResources(), size));
}

/**
 * Dp to px.
 *
 * @param res the res
 * @param dp  the dp
 * @return the int
 */
public static int dpToPx(Resources res, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            res.getDisplayMetrics());
}

/**
 * Set the size of the drawable.
 *
 * @param size The size in pixels (px).
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable sizePx(int size) {
    this.size = size;
    setBounds(0, 0, size, size);
    invalidateSelf();
    return this;
}

/**
 * Set the color of the drawable.
 *
 * @param color The color, usually from android.graphics.Color or 0xFF012345.
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable color(int color) {
    paint.setColor(color);
    invalidateSelf();
    return this;
}

/**
 * Set the color of the drawable.
 *
 * @param colorRes The color resource, from your R file.
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable colorRes(int colorRes) {
    paint.setColor(context.getResources().getColor(colorRes));
    invalidateSelf();
    return this;
}

/**
 * Set the alpha of this drawable.
 *
 * @param alpha The alpha, between 0 (transparent) and 255 (opaque).
 * @return The current IconDrawable for chaining.
 */
public FontIconDrawable alpha(int alpha) {
    setAlpha(alpha);
    invalidateSelf();
    return this;
}

@Override
public int getIntrinsicHeight() {
    return size;
}

@Override
public int getIntrinsicWidth() {
    return size;
}

@Override
public void draw(Canvas canvas) {
    paint.setTextSize(getBounds().height());
    Rect textBounds = new Rect();
    String textValue = icon;
    paint.getTextBounds(textValue, 0, 1, textBounds);
    float textBottom = (getBounds().height() - textBounds.height()) / 2f + textBounds.height() - textBounds.bottom;
    canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint);
}

@Override
public boolean isStateful() {
    return true;
}

@Override
public boolean setState(int[] stateSet) {
    int oldValue = paint.getAlpha();
    int newValue = isEnabled(stateSet) ? alpha : alpha / 2;
    paint.setAlpha(newValue);
    return oldValue != newValue;
}

/**
 * Checks if is enabled.
 *
 * @param stateSet the state set
 * @return true, if is enabled
 */
public static boolean isEnabled(int[] stateSet) {
    for (int state : stateSet)
        if (state == android.R.attr.state_enabled)
            return true;
    return false;
}

@Override
public void setAlpha(int alpha) {
    this.alpha = alpha;
    paint.setAlpha(alpha);
}

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

@Override
public void clearColorFilter() {
    paint.setColorFilter(null);
}

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

/**
 * Sets paint style.
 *
 * @param style to be applied
 */
public void setStyle(Paint.Style style) {
    paint.setStyle(style);
}
}

标签:android,fonts,android-actionbar,typeface
来源: https://codeday.me/bug/20191011/1891950.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有