質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1263閲覧

IconFontを変換した画像がBottomNavigationViewのColorStateListの影響を受けない

okashi123

総合スコア43

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

1グッド

0クリップ

投稿2019/03/05 09:48

IconFontを画像に変換したものをBottomNavigationViewのアイコンに利用しようとしているのですが、未選択のタブの普通のアイコンは暗くなっているのに対してIconFontの画像は一切変化がありませんでした。
FontIconDrawable.javaのoverrideの関数が悪さしてるのかと思ってoverrideの関数を消したりなど試してみたのですが解決できませんでした。

IconFontをタブの選択に応じて変化させるにはどうしたらよろしいでしょうか?
よろしくお願いいたします。

環境 Android Stadio 3.3.1

ソースはnew projectのBottom Navigation Activityから生成したデータに少し手を加えたものです。
フォントはfont awesomeのフリーのものを利用しています。
FontIconDrawable.javaはこちらの内容を使用させていただいています。

dependencies { compile 'com.android.support:appcompat-v7:28.+' compile 'com.android.support:design:28.+' }

java

1public class MainActivity extends AppCompatActivity { 2 3 4 private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 5 = new BottomNavigationView.OnNavigationItemSelectedListener() { 6 7 @Override 8 public boolean onNavigationItemSelected(@NonNull MenuItem item) { 9 10 return true; 11 } 12 13 }; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 21 navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 22 ColorStateList c = new ColorStateList( 23 new int[][]{ 24 new int[]{android.R.attr.state_checked}, 25 new int[]{-android.R.attr.state_checked}, 26 new int[]{android.R.attr.state_enabled}, 27 new int[]{-android.R.attr.state_enabled}, 28 new int[]{android.R.attr.state_selected}, 29 new int[]{-android.R.attr.state_selected}, 30 31 }, 32 new int[]{ 33 Color.BLUE, 34 Color.RED, 35 Color.BLUE, 36 Color.RED, 37 Color.BLUE, 38 Color.RED, 39 } 40 ); 41 navigation.setItemIconTintList(c); 42 43 Menu menu = navigation.getMenu(); 44 MenuItem menuItem = menu.add(Menu.NONE, 99, 99, "追加"); 45 46 Typeface fontType = Typeface.createFromAsset(getResources().getAssets(), "fa_regular_400.ttf"); 47 48 Drawable drawable = new FontIconDrawable(this, "\uF556", fontType).actionBarSize(); 49 menuItem.setIcon(drawable); 50 51 } 52 53} 54

java

1public class FontIconDrawable extends Drawable { 2 3 public static int ANDROID_ACTIONBAR_ICON_SIZE_DP = 24; 4 5 private final Context context; 6 7 private final String icon; 8 9 private TextPaint paint; 10 11 private int size = -1; 12 13 private int alpha = 255; 14 15 /** 16 * Create an IconDrawable. 17 * 18 * @param context Your activity or application context. 19 * @param icon The icon you want this drawable to display. 20 */ 21 public FontIconDrawable(Context context, String icon, Typeface typeface) { 22 this.context = context; 23 this.icon = icon; 24 paint = new TextPaint(); 25 paint.setTypeface(typeface); 26 paint.setStyle(Paint.Style.STROKE); 27 paint.setTextAlign(Paint.Align.CENTER); 28 paint.setUnderlineText(false); 29 paint.setColor(Color.BLACK); 30 paint.setAntiAlias(true); 31 } 32 33 /** 34 * Set the size of this icon to the standard Android ActionBar. 35 * 36 * @return The current IconDrawable for chaining. 37 */ 38 public FontIconDrawable actionBarSize() { 39 return sizeDp(ANDROID_ACTIONBAR_ICON_SIZE_DP); 40 } 41 42 /** 43 * Set the size of the drawable. 44 * 45 * @param dimenRes The dimension resource. 46 * @return The current IconDrawable for chaining. 47 */ 48 public FontIconDrawable sizeRes(int dimenRes) { 49 return sizePx(context.getResources().getDimensionPixelSize(dimenRes)); 50 } 51 52 /** 53 * Set the size of the drawable. 54 * 55 * @param size The size in density-independent pixels (dp). 56 * @return The current IconDrawable for chaining. 57 */ 58 public FontIconDrawable sizeDp(int size) { 59 return sizePx(dpToPx(context.getResources(), size)); 60 } 61 62 /** 63 * Dp to px. 64 * 65 * @param res the res 66 * @param dp the dp 67 * @return the int 68 */ 69 public static int dpToPx(Resources res, int dp) { 70 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 71 res.getDisplayMetrics()); 72 } 73 74 /** 75 * Set the size of the drawable. 76 * 77 * @param size The size in pixels (px). 78 * @return The current IconDrawable for chaining. 79 */ 80 public FontIconDrawable sizePx(int size) { 81 this.size = size; 82 setBounds(0, 0, size, size); 83 invalidateSelf(); 84 return this; 85 } 86 87 /** 88 * Set the color of the drawable. 89 * 90 * @param color The color, usually from android.graphics.Color or 0xFF012345. 91 * @return The current IconDrawable for chaining. 92 */ 93 public FontIconDrawable color(int color) { 94 paint.setColor(color); 95 invalidateSelf(); 96 return this; 97 } 98 99 /** 100 * Set the color of the drawable. 101 * 102 * @param colorRes The color resource, from your R file. 103 * @return The current IconDrawable for chaining. 104 */ 105 public FontIconDrawable colorRes(int colorRes) { 106 paint.setColor(context.getResources().getColor(colorRes)); 107 invalidateSelf(); 108 return this; 109 } 110 111 /** 112 * Set the alpha of this drawable. 113 * 114 * @param alpha The alpha, between 0 (transparent) and 255 (opaque). 115 * @return The current IconDrawable for chaining. 116 */ 117 public FontIconDrawable alpha(int alpha) { 118 setAlpha(alpha); 119 invalidateSelf(); 120 return this; 121 } 122 123 @Override 124 public int getIntrinsicHeight() { 125 return size; 126 } 127 128 @Override 129 public int getIntrinsicWidth() { 130 return size; 131 } 132 133 @Override 134 public void draw(Canvas canvas) { 135 paint.setTextSize(getBounds().height()); 136 Rect textBounds = new Rect(); 137 String textValue = icon; 138 paint.getTextBounds(textValue, 0, 1, textBounds); 139 float textBottom = (getBounds().height() - textBounds.height()) / 2f + textBounds.height() - textBounds.bottom; 140 canvas.drawText(textValue, getBounds().width() / 2f, textBottom, paint); 141 } 142 143 @Override 144 public boolean isStateful() { 145 return true; 146 } 147 148 @Override 149 public boolean setState(int[] stateSet) { 150 int oldValue = paint.getAlpha(); 151 int newValue = isEnabled(stateSet) ? alpha : alpha / 2; 152 paint.setAlpha(newValue); 153 return oldValue != newValue; 154 } 155 156 /** 157 * Checks if is enabled. 158 * 159 * @param stateSet the state set 160 * @return true, if is enabled 161 */ 162 public static boolean isEnabled(int[] stateSet) { 163 for (int state : stateSet) 164 if (state == android.R.attr.state_enabled) 165 return true; 166 return false; 167 } 168 169 @Override 170 public void setAlpha(int alpha) { 171 this.alpha = alpha; 172 paint.setAlpha(alpha); 173 } 174 175 @Override 176 public void setColorFilter(ColorFilter cf) { 177 paint.setColorFilter(cf); 178 } 179 180 @Override 181 public void clearColorFilter() { 182 paint.setColorFilter(null); 183 } 184 185 @Override 186 public int getOpacity() { 187 return PixelFormat.OPAQUE; 188 } 189 190 /** 191 * Sets paint style. 192 * 193 * @param style to be applied 194 */ 195 public void setStyle(Paint.Style style) { 196 paint.setStyle(style); 197 } 198}
kakajika👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FontIconDrawableをざっと見たところ、ColorStateListに関する処理が実装されていないようなので自分で実装する必要があると思います。

  1. setTintListを実装する
  2. setStateでstate変更時の文字色変更の処理をする

java

1public class FontIconDrawable extends Drawable { 2 @Nullable 3 private ColorStateList tint = null; 4 5 @Override 6 public void setTintList(@Nullable ColorStateList tint) { 7 super.setTintList(tint); 8 this.tint = tint; 9 } 10 11 @Override 12 public boolean setState(int[] stateSet) { 13 int oldValue = paint.getAlpha(); 14 int newValue = isEnabled(stateSet) ? alpha : alpha / 2; 15 paint.setAlpha(newValue); 16 17 // tintが設定されていたら文字色を変更 18 int oldColor = paint.getColor(); 19 int newColor = (tint != null) ? tint.getColorForState(stateSet, defaultColor) : defaultColor; 20 paint.setColor(newColor); 21 22 return oldValue != newValue || oldColor != newColor; 23 } 24}

投稿2019/03/05 12:39

kakajika

総合スコア3131

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

okashi123

2019/03/06 02:20

できました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問