如何在Floating Action Button上设置Font Awseome图标?

时间:2015-09-28 06:06:57

标签: android font-awesome

我正在尝试使用字体真棒图标来浮动Action Button。我无法实现以下解决方案。 https://github.com/futuresimple/android-floating-action-button/issues/59

3 个答案:

答案 0 :(得分:9)

你应该使用官方的Font Awesome Library,

https://github.com/bperin/FontAwesomeAndroid

FontAwesome Library中有DrawableAwesome,您需要使用

构建它
DrawableAwesome drable = new DrawableAwesome.DrawableAwesomeBuilder(Context context, int icon).build();

并在您的FAB中设置。 int icon是String资源的id你也可以自定义Shadow,Bold等渲染选项,只需检查DrawableAwesome

另一个图书馆可用,如果有人想检查。 https://github.com/JoanZapata/android-iconify

答案 1 :(得分:0)

感谢QAMAR,你帮助我了。

对于使用Xamarin的人,我将DrawableAwesome.java移植到C#:

using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace YourNameSpace
{
  public class DrawableAwesome : Drawable
  {
    private static float padding = 0.88f;
    private Context context;
    private string icon;
    private Paint paint;
    private int width;
    private int height;
    private float size;
    private int color;
    private bool antiAliased;
    private bool fakeBold;
    private float shadowRadius;
    private float shadowDx;
    private float shadowDy;
    private int shadowColor;

    public override int Opacity
    {
      get
      {
        return (int)Format.Translucent;
      }
    }

    public DrawableAwesome(string icon, int sizeDpi, int color, bool antiAliased, bool fakeBold, float shadowRadius, float shadowDx, float shadowDy, int shadowColor, Context context)
    {
      this.context = context;
      this.icon = icon;
      this.size = dpToPx(sizeDpi) * padding;
      this.height = dpToPx(sizeDpi);
      this.width = dpToPx(sizeDpi);
      this.color = color;
      this.antiAliased = antiAliased;
      this.fakeBold = fakeBold;
      this.shadowRadius = shadowRadius;
      this.shadowDx = shadowDx;
      this.shadowDy = shadowDy;
      this.shadowColor = shadowColor;
      this.paint = new Paint();

      paint.SetStyle(Paint.Style.Fill);
      paint.TextAlign = Paint.Align.Center;
      this.paint.Color = new Color(this.color);
      this.paint.TextSize = this.size;
      Typeface font = AppData.FontAwesomeTypeface;
      this.paint.SetTypeface(font);
      this.paint.AntiAlias = this.antiAliased;
      this.paint.FakeBoldText = this.fakeBold;
      this.paint.SetShadowLayer(this.shadowRadius, this.shadowDx, this.shadowDy, new Color(this.shadowColor));
    }

    public override void Draw(Canvas canvas)
    {
      float xDiff = (width / 2.0f);
      canvas.DrawText(icon, xDiff, size, paint);
    }

    public override int IntrinsicHeight
    {
      get
      {
        return height;
      }
    }
    public override int IntrinsicWidth
    {
      get
      {
        return width;
      }
    }

    public override void SetAlpha(int alpha)
    {
      paint.Alpha = alpha;
    }

    public override void SetColorFilter(ColorFilter cf)
    {
      paint.SetColorFilter(cf);
    }

    private int dpToPx(int dp)
    {
      DisplayMetrics displayMetrics = context.Resources.DisplayMetrics;
      int px = (int)Math.Round(dp * (displayMetrics.Xdpi / (int)DisplayMetricsDensity.Default));
      return px;
    }

    public class DrawableAwesomeBuilder
    {
      private Context context;
      private string icon;
      private int sizeDpi = 32;
      private int color = Color.White;
      private bool antiAliased = true;
      private bool fakeBold = true;
      private float shadowRadius = 0;
      private float shadowDx = 0;
      private float shadowDy = 0;
      private int shadowColor = Color.White;

      public DrawableAwesomeBuilder(Context context, string icon)
      {
        this.context = context;
        this.icon = icon;
      }

      public void SetSize(int size)
      {
        this.sizeDpi = size;
      }

      public void SetColor(int color)
      {
        this.color = color;
      }

      public void SetAntiAliased(bool antiAliased)
      {
        this.antiAliased = antiAliased;
      }

      public void SetFakeBold(bool fakeBold)
      {
        this.fakeBold = fakeBold;
      }

      public void SetShadow(float radius, float dx, float dy, int color)
      {
        this.shadowRadius = radius;
        this.shadowDx = dx;
        this.shadowDy = dy;
        this.shadowColor = color;
      }

      public DrawableAwesome build()
      {
        return new DrawableAwesome(icon, sizeDpi, color, antiAliased, fakeBold, shadowRadius, shadowDx, shadowDy, shadowColor, context);
      }
    }
  }
}

将font_awesome.xml添加到Xamarin Android项目的Values文件夹后,您可以执行以下操作:

  DrawableAwesome drable = new DrawableAwesome.DrawableAwesomeBuilder(this.FabButton.Context, Resources.GetString(Resource.String.fa_whatevericonyouwant)).build();
  this.FabButton.SetImageDrawable(drable);

this.FabButton是:

private FloatingActionButton fabButton;
public FloatingActionButton FabButton
{
  get
  {
    if (this.fabButton!= null)
      return fabButton;

    fabButton= FindViewById<FloatingActionButton>(Resource.Id.fabButton);
    return fabButton;
  }
}

答案 2 :(得分:-1)

甚至更好的你可以使用官方谷歌库工厂.. 看到我的回答 Floating Action Button

相关问题