我有一個從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);
}
}
截圖: