2017-07-08 9 views
2

ユーザが設定した選択されたプライマリカラーに応じてドロアブルにカラーフィルタを適用しようとしています。これは私が使用しているコードです。setColorFilterがアンドロイドのドロウアブルで動作しない場合があります

getResources().getDrawable(R.drawable.ic_batman_1) 
      .setColorFilter(ColorHelper.getPrimaryColor(), PorterDuff.Mode.OVERLAY); 

問題は、時には、このコードがdrawableのカラーフィルタを変更しないことです。このコードを自分のアクティビティ(主なアクティビティ)onCreateメソッドとonResumeメソッドに配置しました。

アプリが起動するとすぐに、このカラーフィルタをそのドロワブルに適用することができますが、時にはそれが起こっていないことがあります。私はまた、この問題はハイエンドのモバイル(高速プロセッサ、より多くのRAM)ではなくローエンドのモバイルでのみ起こっていることに気付きました。

他のアクティビティを参照してメインアクティビティに戻った場合、カラーフィルタが適用されます。コードをデバッグし、適切なカラーパラメータで起動中にsetColorFilterが呼び出されていますが、何らかの理由でそれが適用されません。どんな種類の助けにも感謝します。

これは愚かな質問だと思うなら、この質問をd​​ownvoteしないでください、ちょうどコメントと私は質問を下げます。私は質問をするためにSOで禁止される寸前です。

答えて

1

あなたはDrawable.mutate()を試します。このような性質、

Drawable drawable = ContextCompat.getDrawable(context, resource).mutate(); 

drawable.setColorFilter(ColorHelper.getPrimaryColor(), PorterDuff.Mode. OVERLAY); 
/** 
* Make this drawable mutable. This operation cannot be reversed. A mutable 
* drawable is guaranteed to not share its state with any other drawable. 
* This is especially useful when you need to modify properties of drawables 
* loaded from resources. By default, all drawables instances loaded from 
* the same resource share a common state; if you modify the state of one 
* instance, all the other instances will receive the same modification. 
* 
* Calling this method on a mutable Drawable will have no effect. 
* 
* @return This drawable. 
* @see ConstantState 
* @see #getConstantState() 
*/ 
public @NonNull Drawable mutate() { 
    return this; 
} 
+0

はあなたにMuthukrishnanをありがとう、それが働きました!私は先週からこれ以上頭を悩ませてきた。このコードを可能な限りの場所に配置しようとしました。あなたは人生の節約者です。 –

関連する問題