先看一下喜马拉雅banner效果
接下来看一下demo的效果
接下来先看看如何实现:
1、取图片的颜色,用到系统提供的调色板Paletee,引入方式
implementation 'com.android.support:palette-v7:28.0.0'
Palette是什么?
它能让你从图像中提取突出的颜色。这个类能提取以下几种颜色:
Vibrant(充满活力的)
Vibrant dark(充满活力的黑)
Vibrant light(充满活力的亮)
Muted(柔和的)
Muted dark(柔和的黑)
Muted lighr(柔和的亮)
主要代码如下:
@Override
public void displayImage(Context context, final Object imgObj, ImageView imageView) {
this.context = context;
if (imgObj != null) {
imageView.setPadding(30, 0, 30, 0);
Glide.with(context).asBitmap().load(imgObj.toString()).listener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
setColorList(resource, imgObj.toString());
return false;
}
}).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(imageView);
}
}
private void setColorList(Bitmap bitmap, String imgUrl) {
if (colorList == null) {
return;
}
palette = Palette.from(bitmap).generate();
for (int i = 0; i < colorList.size(); i++) {
if (colorList.get(i).getImgUrl().equals(imgUrl)) {// imgUrl作为识别标志
if (palette.getVibrantSwatch() != null) {
colorList.get(i).setVibrantColor(palette.getVibrantSwatch().getRgb());
}
if (palette.getDarkVibrantSwatch() != null) {
colorList.get(i).setVibrantDarkColor(palette.getDarkVibrantSwatch().getRgb());
}
if (palette.getLightVibrantSwatch() != null) {
colorList.get(i).setVibrantLightColor(palette.getLightVibrantSwatch().getRgb());
}
if (palette.getMutedSwatch() != null) {
colorList.get(i).setMutedColor(palette.getMutedSwatch().getRgb());
}
if (palette.getDarkMutedSwatch() != null) {
colorList.get(i).setMutedDarkColor(palette.getDarkMutedSwatch().getRgb());
}
if (palette.getLightVibrantSwatch() != null) {
colorList.get(i).setMutedLightColor(palette.getLightVibrantSwatch().getRgb());
}
}
}
}
2、使用第三方banner库 ,感谢其作者,非常好用。github地址banner
主要代码如下:
banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (positionOffset > 1) {//会出现极个别大于1的数据
return;
}
//修正position,解决两头颜色错乱,来自Banner控件源码
if (position == 0) {
position = count;
}
if (position > count) {
position = 1;
}
int pos = (position + 1) % count;//很关键
int vibrantColor = ColorUtils.blendARGB(imageLoader.getVibrantColor(pos), imageLoader.getVibrantColor(pos + 1), positionOffset);
ivBannerHeadBg.setBackgroundColor(vibrantColor);
setStatusBarColor(MainActivity.this, vibrantColor);
}
@Override
public void onPageSelected(final int position) {
if(isInit){// 第一次,延时加载才能拿到颜色
isInit = false;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int vibrantColor = imageLoader.getVibrantColor(1);
ivBannerHeadBg.setBackgroundColor(vibrantColor);
setStatusBarColor(MainActivity.this, vibrantColor);
}
}, 200);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
demo下载地址:demo下载,
csdn积分没办法自己控制,积分越来越多了,2019-12-05开源了,传到github。github传送门