使用Palette类提取图片的颜色信息
原文出處:http://qichaochen.github.io/2014/11/16/105-Android-5.0-Palette-01/
在Material Design設計中很重要的一部分內(nèi)容是應用中圖片顏色和文字顏色需要和主題相匹配,比如下面在這個應用:
文本的顏色根據(jù)不同圖片動態(tài)進行對應適配(也許你會說,如果全部用白色文本多省事,何必這么麻煩呢?額…可以腦補一下高富帥和矮矬窮的區(qū)別)
那么在應用程序中如何提取圖片的顏色信息呢?可以提取多少種顏色信息呢? 在最新的Support Library v21提供了Palette類可以很方便的實現(xiàn)這個需求。
下面看一下提取圖片顏色信息的步驟和注意事項:
1、首先需要添加Palette的依賴
在build.gralde的dependencies添加appcomat v7和palette-v7依賴
dependencies {//...其他依賴compile 'com.android.support:appcompat-v7:21.0.0'compile 'com.android.support:palette-v7:21.+' }添加完成后需要同步一下Gradle,同步成功后就可以使用Palette類了。
2、創(chuàng)建Palette對象
官方提供了四種根據(jù)Bitmap對象來創(chuàng)建Palette對象的方法,具體分別如下:
兩種同步方法:
// 最好在加載圖片線程中使用 // 默認調(diào)色板大小(16). Palette p = Palette.generate(bitmap); //設置調(diào)色板大小(24) Palette p = Palette.generate(bitmap, 24);兩種異步方法:
// 簡單快速的實現(xiàn)方法,內(nèi)部使用AsyncTask // 但是可能不是最優(yōu)的方法(因為有線程的切換) // 默認調(diào)色板大小(16). Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {// palette為生成的調(diào)色板} }); // 設置調(diào)色板大小(24) Palette.generateAsync(bitmap, 24, new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {// palette為生成的調(diào)色板} });調(diào)色板的大小值越大,生成的調(diào)色板的時間越長,數(shù)值越小,得到的顏色信息也越少,調(diào)色板的大小最好根據(jù)圖片類型來決定,比如:
- 聯(lián)系人頭像:最優(yōu)值24~32
- 風景圖:8-16
- 當然默認是16,大多數(shù)情況下都可以取得很好的效果。
3、使用Palette對象
生成了Palette對象后就可以嘗試獲取六種不同的色板,具體如下:
Palette.Swatch s1 = Palette.getVibrantSwatch(); //充滿活力的色板 Palette.Swatch s2 = Palette.getDarkVibrantSwatch(); //充滿活力的暗色類型色板 Palette.Swatch s3 = Palette.getLightVibrantSwatch(); //充滿活力的亮色類型色板 Palette.Swatch s4 = Palette.getMutedSwatch(); //黯淡的色板 Palette.Swatch s5 = Palette.getDarkMutedSwatch(); //黯淡的暗色類型色板 Palette.Swatch s6 = Palette.getLightMutedSwatch(); //黯淡的亮色類型色板可以根據(jù)需求自由選擇色板類型(充滿活力和或充滿活力暗色類型色板應該是大部分開發(fā)經(jīng)常使用的),有了色板就可以根據(jù)色板來獲取具體顏色信息,在Swatch色板中提供了五種顏色信息,分別如下:
| getPopulation() | the number of pixels represented by this swatch |
| getRgb() | the RGB value of this color |
| getHsl() | the HSL value of this color |
| getBodyTextColor() | the RGB value of a text color which can be displayed on top of this color |
| getTitleTextColor() | the RGB value of a text color which can be displayed on top of this color |
示例代碼如下:
請注意必須對swatch進行是否為null判斷,因為如果面板無法找到相匹配的標準色,那么然后將返回null,不進行判斷將會出現(xiàn)空指針異常。
最后附上三個測試效果圖和完整代碼。
完整代碼
public class MainActivity extends ActionBarActivity {private ImageView image, iv1, iv2, iv3, iv4, iv5, iv6;private Palette palette;private Palette.Swatch s1,s2,s3,s4,s5,s6;private int index = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv1 = (ImageView) findViewById(R.id.iv1);iv2 = (ImageView) findViewById(R.id.iv2);iv3 = (ImageView) findViewById(R.id.iv3);iv4 = (ImageView) findViewById(R.id.iv4);iv5 = (ImageView) findViewById(R.id.iv5);iv6 = (ImageView) findViewById(R.id.iv6);image = (ImageView) findViewById(R.id.image);GetPalette(R.drawable.a11);}private void GetPalette(int imageId) {image.setImageResource(imageId);//使用默認的調(diào)色板大小(16)Palette.generateAsync(BitmapFactory.decodeResource(getResources(), imageId), new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {s1 = palette.getVibrantSwatch();s2 = palette.getDarkVibrantSwatch();s3 = palette.getLightVibrantSwatch();s4 = palette.getMutedSwatch();s5 = palette.getDarkMutedSwatch();s6 = palette.getLightMutedSwatch();if (s1 != null) {iv1.setBackgroundColor(s1.getRgb());s1.getPopulation();}if (s2 != null) {iv2.setBackgroundColor(s2.getRgb());}if (s3 != null) {iv3.setBackgroundColor(s3.getRgb());}if (s4 != null) {iv4.setBackgroundColor(s4.getRgb());}if (s5 != null) {iv5.setBackgroundColor(s5.getRgb());}if (s6 != null) {iv6.setBackgroundColor(s6.getRgb());}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main,menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == R.id.action_settings){index++;if (index%3 == 0){GetPalette(R.drawable.a11);} else if (index % 3 == 1){GetPalette(R.drawable.a12);} else if (index % 3 == 2){GetPalette(R.drawable.a13);}}return true;} }參考
Chris Banes:《Palette v21》
Chris Banes:《Palette preview》
Palette Reference
總結(jié)
以上是生活随笔為你收集整理的使用Palette类提取图片的颜色信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android WebView 性能优化
- 下一篇: 使用RoundedBitmapDrawa