如何在Android中使用字体文件中的图标作为绘图 [英] How to use icon from font file as a drawable in Android
本文介绍了如何在Android中使用字体文件中的图标作为绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
类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 作为项目: < itemandroid:homeAsUpIndicator> @drawable / ... < /项目>
如何在style.xml中的font文件中使用图标图像。 / strong>
解决方案您可以创建自己的可绘制类来执行此操作,像这样 / **将图标嵌入可用作TextView图标或ActionBar图标的Drawable。
*
* new IconDrawable(context,IconValue.icon_star)
* .colorRes(R.color.white)
* .actionBarSize();
*
*如果您不设置drawable的大小,它将使用赋给他的大小
*。请注意,在ActionBar中,如果您不显式设置大小
*,则使用0,因此请使用actionBarSize()。
* /
public class FontIconDrawable extends Drawable {
public static int ANDROID_ACTIONBAR_ICON_SIZE_DP = 24;
private final Context context;
private final String icon;
私人TextPaint油漆;
private int size = -1;
private int alpha = 255;
/ **
*创建一个IconDrawable。
*
* @param上下文您的活动或应用程序上下文。
* @param icon你想要这个可绘制图标显示的图标。
* /
public FontIconDrawable(Context context,String icon,Typeface typeface){
this.context = context;
this.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);
}
/ **
*将这个图标的大小设置为标准的Android ActionBar。
*
* @return当前用于链接的IconDrawable。
* /
public FontIconDrawable actionBarSize(){
return sizeDp(ANDROID_ACTIONBAR_ICON_SIZE_DP);
}
/ **
*设置drawable的大小。
*
* @param维度资源。
* @return用于链接的当前IconDrawable。
* /
public FontIconDrawable sizeRes(int dimenRes){
return sizePx(context.getResources()。getDimensionPixelSize(dimenRes));
}
/ **
*设置drawable的大小。
*
* @param size密度无关像素(dp)中的大小。
* @return用于链接的当前IconDrawable。
* /
public FontIconDrawable sizeDp(int size){
return sizePx(dpToPx(context.getResources(),size));
}
/ **
* Dp到px。
*
* @param res res
* @param dp the dp
* @return the int
* /
public static int dpToPx res,int dp){
return(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,
res.getDisplayMetrics());
}
/ **
*设置drawable的大小。
*
* @param size像素(px)的大小。
* @return用于链接的当前IconDrawable。
* /
public FontIconDrawable sizePx(int size){
this.size = size;
setBounds(0,0,size,size);
invalidateSelf();
返回这个;
}
/ **
*设置绘图的颜色。
*
* @param color颜色,通常来自android.graphics.Color或0xFF012345。
* @return用于链接的当前IconDrawable。
* /
public FontIconDrawable color(int color){
paint.setColor(color);
invalidateSelf();
返回这个;
}
/ **
*设置绘图的颜色。
*
* @param colorRes您的R文件中的颜色资源。
* @return用于链接的当前IconDrawable。
* /
public FontIconDrawable colorRes(int colorRes){
paint.setColor(context.getResources()。getColor(colorRes));
invalidateSelf();
返回这个;
}
/ **
*设置这个drawable的alpha值。
*
* @param alpha在0(透明)和255(不透明)之间的alpha值。
* @return用于链接的当前IconDrawable。
* /
public FontIconDrawable alpha(int alpha){
setAlpha(alpha);
invalidateSelf();
返回这个;
}
@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 =图标;
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);
返回oldValue!= newValue;
}
/ **
*检查是否启用。
*
* @param state设置状态集
* @return true,如果启用
* /
public static boolean isEnabled(int [] stateSet){
for(int state:stateSet)
if(state == android.R.attr.state_enabled)
return true;
返回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;
}
/ **
*设置绘画风格。
*
* @param样式应用
* /
public void setStyle(Paint.Style style){
paint.setStyle(style);
}
}
I have a font file whose icons I am using in layout files via a Custom TextView.
I created a Custom Class :
class CustomFontTextView extends TextView
say, icon is there in Font file Sample.ttf as string resource :
<string name="icon"></string>
In layouts, I can use it as :
<com.sec.mywash.views.CustomFontTextView
custom:custom_typeface="sample_font"
android:text="@string/icon"/>.
However , my requirement is to change the Home up button in action bar that we set in style.xml as item:
<item "android:homeAsUpIndicator">@drawable/....</item>
How to use the icon image from font file in style.xml which takes drawable.
解决方案 You can create your own drawable class for doing this, something like this
/** 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中使用字体文件中的图标作为绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文