看看这张图片
左上角是安卓汉堡菜单,但它是白色的。另外,右边是一个搜索放大镜,也是白色的。问题是背景是从服务器动态加载的,具体取决于所呈现的内容。
是否可以对菜单图标进行着色或提升它们,以便它们在发生这种情况时出现?该背景图像实际上可以是任何颜色,这实际上取决于服务器的功能。也许也可以在它们上使用 Palette 库?
编辑:到目前为止,我的解决方案基于@Steve 响应:
private void loadBackdrop( String inetref ) {
Log.i( TAG, "loadBackdrop : inetref=" + inetref );
String bannerUrl = MainApplication.getInstance().getMasterBackendUrl() + "/Content/GetRecordingArtwork?Inetref=" + inetref + "&Type=banner&Height=256";
Log.i(TAG, "loadBackdrop : bannerUrl=" + bannerUrl);
final ImageView imageView = (ImageView) findViewById( R.id.backdrop );
final PaletteTransformation paletteTransformation = PaletteTransformation.getInstance();
Picasso.with( this )
.load( bannerUrl )
.fit().centerCrop()
.transform( paletteTransformation )
.into(imageView, new Callback.EmptyCallback() {
@Override
public void onSuccess() {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); // Ew!
Palette palette = PaletteTransformation.getPalette(bitmap);
try {
int inverseColor = getComplementaryColor(palette.getVibrantColor(R.color.white));
Log.i( TAG, "loadBackdrop : inverseColor=" + inverseColor + ", Color.WHITE=" + Color.WHITE + ", Color.BLACK=" + Color.BLACK + ", Color." + ( inverseColor > ( Color.BLACK / 2 ) ? "WHITE" : "BLACK" ) );
int color = ( inverseColor > ( Color.BLACK / 2 ) ? Color.WHITE : Color.BLACK );
collapsingToolbar.setCollapsedTitleTextColor( color );
collapsingToolbar.setExpandedTitleColor( color );
Drawable newSearchMenuItem = mSearchMenuItem.getIcon();
newSearchMenuItem.mutate().setColorFilter( color, PorterDuff.Mode.SRC_IN );
mSearchMenuItem.setIcon( newSearchMenuItem );
Drawable newUpMenuItem = getResources().getDrawable( R.drawable.ic_arrow_back_white_24dp );
newUpMenuItem.mutate().setColorFilter( color, PorterDuff.Mode.SRC_IN );
getSupportActionBar().setHomeAsUpIndicator( newUpMenuItem );
} catch( Exception e ) {
Log.e( TAG, "error decoding palette from imageView", e );
}
}
@Override
public void onError() {
}
});
}
public static int getComplementaryColor( int colorToInvert ) {
float[] hsv = new float[ 3 ];
Color.RGBToHSV( Color.red( colorToInvert ), Color.green( colorToInvert ), Color.blue( colorToInvert ), hsv );
hsv[ 0 ] = ( hsv[ 0 ] + 180 ) % 360;
return Color.HSVToColor( hsv );
}