使用Palette,首先要导入sdk\extras\android\support\v7\palette
然后在你的工程中引入Palette工程
接下来就可以使用了
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.gg1); Palette palette=Palette.from(bitmap).generate(); //暗鲜艳色 int darkVibrantColor=palette.getDarkVibrantColor(android.R.color.holo_blue_dark); //暗柔和的颜色 int darkMutedColor=palette.getDarkMutedColor(android.R.color.holo_orange_dark); //亮鲜艳色(淡色) int lightVibrantColor=palette.getLightVibrantColor(android.R.color.holo_blue_bright); //亮柔和色(淡色) int lightMutedColor=palette.getLightMutedColor(android.R.color.holo_orange_light); //柔和色 int mutedColor=palette.getMutedColor(android.R.color.holo_red_dark); //鲜艳色 int vibrantColor=palette.getVibrantColor(android.R.color.holo_red_light); final TextView tv_0 = (TextView) findViewById(R.id.tv_0); final TextView tv_1 = (TextView) findViewById(R.id.tv_1); final TextView tv_2 = (TextView) findViewById(R.id.tv_2); tv_0.setBackgroundColor(darkVibrantColor); tv_0.setTextColor(lightVibrantColor); tv_1.setBackgroundColor(darkMutedColor); tv_1.setTextColor(lightMutedColor); tv_2.setBackgroundColor(mutedColor); tv_2.setTextColor(vibrantColor); } }
效果图:
Palette还有个异步的方法:
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() { @Override public void onGenerated(Palette palette) { // TODO Auto-generated method stub } });
PS:不同版本的Palette可能用法不同,我用的是最新的Android Support Library 22.2