2017-08-29 9 views
2

URLからビットマップを読み込み、パレットを使用してフローティングアクションボタンの背景を変更するAsyncTaskがあります。ほとんどの画像では正常に動作しますが、一部の場合はボタンが透明になります。スクリーンショット1は、画像の青色で動作するボタンの色を示していますが、スクリーンショット2ではボタンの色が透明です(画像に透明なピクセルが含まれていなくてもjpegであるにもかかわらず)。一部の画像で透明な色を返すAndroidパレット

public class ColoredFabTask extends AsyncTask<String , String , String> { 
    Context mContext; 
    View view; 
    private View rootView; 
    URL myFileUrl; 
    Bitmap imageBitmap = null; 

    public ColoredFabTask(Context context, View view) { 
     this.mContext = context; 
     this.view = view; 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     try { 
      myFileUrl = new URL(args[0]); 
      HttpURLConnection conn = (HttpURLConnection) 
      myFileUrl.openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      InputStream is = conn.getInputStream(); 
      imageBitmap = BitmapFactory.decodeStream(is); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String args) { 
     Palette palette = Palette.from(imageBitmap).generate(); 
     int vibrant = palette.getVibrantColor(0); 
     FloatingActionButton applyButton = (FloatingActionButton) view.findViewById(R.id.applyButton); 
     applyButton.setBackgroundTintList(ColorStateList.valueOf(vibrant)); 
     applyButton.setVisibility(View.VISIBLE); 
    } 
} 

スクリーンショット:

enter image description here

enter image description here

答えて

1

どのようにして知りたいのかが分かりません。スウォッチがnullであるかどうかを確認してください。

 Palette palette = Palette.from(imageBitmap).generate(); 
     int fallbackColor = palette.getDominantColor(0); 
     Palette.Swatch vibrantColorSwatch = palette.getVibrantSwatch(); 
     if (vibrantColorSwatch != null) { 
      int vibrantColor = vibrantColorSwatch.getRgb(); 
      FloatingActionButton applyButton = (FloatingActionButton) view.findViewById(R.id.applyButton); 
      applyButton.setBackgroundTintList(ColorStateList.valueOf(vibrantColor)); 

     } 
     else { 
      FloatingActionButton applyButton = (FloatingActionButton) view.findViewById(R.id.applyButton); 
      applyButton.setBackgroundTintList(ColorStateList.valueOf(fallbackColor)); 
     } 
1

Paletterは、その中のデフォルトでいくつかの色を無視します。パレットソースからの実装です:

static final Palette.Filter DEFAULT_FILTER = new Palette.Filter() { 
    private static final float BLACK_MAX_LIGHTNESS = 0.05F; 
    private static final float WHITE_MIN_LIGHTNESS = 0.95F; 

    public boolean isAllowed(int rgb, float[] hsl) { 
     return !this.isWhite(hsl) && !this.isBlack(hsl) && !this.isNearRedILine(hsl); 
    } 

    private boolean isBlack(float[] hslColor) { 
     return hslColor[2] <= 0.05F; 
    } 

    private boolean isWhite(float[] hslColor) { 
     return hslColor[2] >= 0.95F; 
    } 

    private boolean isNearRedILine(float[] hslColor) { 
     return hslColor[0] >= 10.0F && hslColor[0] <= 37.0F && hslColor[1] <= 0.82F; 
    } 
}; 

ご覧のとおり、パレットはいくつかの色を無視します。 したがって、すべての色を処理できるようにカスタムファイラーを設定する必要があります。

関連する問題