我有一个从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);
}
}
截图: