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

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

概述我有一个字体文件,正在通过自定义TextView在布局文件中使用其图标.我创建了一个自定义类:    CustomFontTextView类扩展了TextView例如,字体文件Sample.ttf中的图标作为字符串资源存在:<stringname="icon"></string>在布局中,我可以将其用作:<com.sec.mywash.vi

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

我创建了一个自定义类:
    CustomFontTextVIEw类扩展了TextVIEw

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

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

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

<com.sec.mywash.vIEws.CustomFontTextVIEwcustom: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;}@OverrIDepublic int getIntrinsicHeight() {    return size;}@OverrIDepublic int getIntrinsicWIDth() {    return size;}@OverrIDepublic 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);}@OverrIDepublic boolean isstateful() {    return true;}@OverrIDepublic 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;}@OverrIDepublic voID setAlpha(int Alpha) {    this.Alpha = Alpha;    paint.setAlpha(Alpha);}@OverrIDepublic voID setcolorFilter(colorFilter cf) {    paint.setcolorFilter(cf);}@OverrIDepublic voID clearcolorFilter() {    paint.setcolorFilter(null);}@OverrIDepublic int getopacity() {    return PixelFormat.OPAQUE;}/** * Sets paint style. * * @param style to be applIEd */public voID setStyle(Paint.Style style) {    paint.setStyle(style);}}
总结

以上是内存溢出为你收集整理的如何在Android中将字体文件中的图标用作绘图对象全部内容,希望文章能够帮你解决如何在Android中将字体文件中的图标用作绘图对象所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存