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);
}
}
スクリーンショット: