Palette:
引入v7里面的一个单独项目Palette, android.support.graphics.Palette;
Palette 可以在一张图片里分析出一些色彩特性,主色调,鲜艳的颜色,柔和的颜色等等。。
简单 代码实现:
Layout布局
<ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@mipmap/aaa" />MainActivity.java中带吗第一步 获取imageview中的图片
BitmapDrawable drawable = (BitmapDrawable)iv.getDrawable();Bitmap bitmap = drawable.getBitmap();第二步 获取Palette实例 设置监听事件Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { int lightMutedColor = palette.getLightMutedColor(Color.RED); 获取一个图片中比较柔和的颜色,如果获取不到 设为红色 Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch(); text.setTextColor(darkMutedSwatch.getTitleTextColor()); 给text设置字体颜色 text.setBackgroundColor(getTransColor(0.5f,darkMutedSwatch.getRgb())); 给text设置背景颜色 0.5 设置透明度 }});/** * textview 背景 透明 * @param darkMutedSwatch * @return */PRivate int getTransColor(float percent,int darkMutedSwatch) { int i = darkMutedSwatch & 0xfff; int i1 = darkMutedSwatch >> 8 & 0xff; int i2 = darkMutedSwatch >> 16 & 0xff; int i3 = darkMutedSwatch >>> 24; //透明度 percent i3 = Math.round(i3* percent); return Color.argb(i,i1,i2,i3);}
新闻热点
疑难解答