android Palette調(diào)色板使用詳解
Palette是一個(gè)可以從圖片(Bitmap)中提取顏色的幫助類(lèi),可以使UI更加美觀,根據(jù)圖片動(dòng)態(tài)的顯示相應(yīng)的顏色?,F(xiàn)在最新的api是在版本22.0.0添加的,本篇文章也是使用的22.0.0的api版本(注意版本之間api的不同)。

應(yīng)用項(xiàng)目:https://github.com/DingMouRen/PaletteImageView
應(yīng)用中的效果 Demo 效果

Palette可以提取的顏色:
- Vibrant (有活力的)
- Vibrant dark(有活力的 暗色)
- Vibrant light(有活力的 亮色)
- Muted (柔和的)
- Muted dark(柔和的 暗色)
- Muted light(柔和的 亮色)
使用方法: module的build.gradle中引用
compile 'com.android.support:palette-v7:25.3.1'
使用步驟:
1.獲取Palette對(duì)象,也就是圖像調(diào)色板
2.獲取從圖像調(diào)色板生成的色樣
3.從色樣中提取相應(yīng)顏色
1.獲取Palette對(duì)象,也就是圖像調(diào)色板
獲取Palette對(duì)象有同步和異步兩種方式,建議使用異步獲取Palette對(duì)象
// Synchronous
Palette p = Palette.from(bitmap).generate();
// Asynchronous
Palette.from(bitmap).generate(new PaletteAsyncListener() {
public void onGenerated(Palette p) {
// Use generated instance
}
});
2.獲取從圖像調(diào)色板生成的色樣
可以獲取到六種色樣,但是有的時(shí)候獲取不到對(duì)應(yīng)的色樣對(duì)象,必須注意非空判斷。
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的 Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色 Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色 Palette.Swatch muted = palette.getMutedSwatch();//柔和的 Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色 Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色
3.從色樣中提取相應(yīng)顏色
通過(guò) getRgb() 可以得到最終的顏色值并應(yīng)用到UI中。getBodyTextColor() 和 getTitleTextColor() 可以得到此顏色下文字適合的顏色,這樣很方便我們?cè)O(shè)置文字的顏色,使文字看起來(lái)更加舒服。
swatch.getPopulation(): 樣本中的像素?cái)?shù)量 swatch.getRgb(): 顏色的RBG值 swatch.getHsl(): 顏色的HSL值 swatch.getBodyTextColor(): 主體文字的顏色值 swatch.getTitleTextColor(): 標(biāo)題文字的顏色值
Demo的代碼中沒(méi)有對(duì)獲取到的色樣對(duì)象進(jìn)行非空判斷,注意一定要加上非空判斷
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
private LinearLayout line1,line2,line3,line4,line5,line6;
private TextView tv1_1,tv1_2,tv2_1,tv2_2,tv3_1,tv3_2,tv4_1,tv4_2,tv5_1,tv5_2,tv6_1,tv6_2;
private List<LinearLayout> bgs = new ArrayList<>();
private List<TextView> bodyTexts = new ArrayList<>();
private List<TextView> titleTexts = new ArrayList<>();
private List<Palette.Swatch> swatchs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView) findViewById(R.id.img);
initView();
Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
if (bitmap == null){
return;
}
Palette.from(bitmap).generate(listener);
}
private Palette.PaletteAsyncListener listener = new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
if (palette != null){
Palette.Swatch vibrant = palette.getVibrantSwatch();//有活力的
Palette.Swatch vibrantDark = palette.getDarkVibrantSwatch();//有活力的,暗色
Palette.Swatch vibrantLight = palette.getLightVibrantSwatch();//有活力的,亮色
Palette.Swatch muted = palette.getMutedSwatch();//柔和的
Palette.Swatch mutedDark = palette.getDarkMutedSwatch();//柔和的,暗色
Palette.Swatch mutedLight = palette.getLightMutedSwatch();//柔和的,亮色
swatchs.clear();
swatchs.add(vibrant);swatchs.add(vibrantDark);swatchs.add(vibrantLight);
swatchs.add(muted);swatchs.add(mutedDark);swatchs.add(mutedLight);
show();
}
}
};
private void show() {
for (int i = 0; i < 6; i++) {
bgs.get(i).setBackgroundColor(swatchs.get(i).getRgb());
bodyTexts.get(i).setTextColor(swatchs.get(i).getBodyTextColor());
titleTexts.get(i).setTextColor(swatchs.get(i).getTitleTextColor());
}
}
private void initView() {
line1 = (LinearLayout) findViewById(R.id.line1);
line2 = (LinearLayout) findViewById(R.id.line2);
line3 = (LinearLayout) findViewById(R.id.line3);
line4 = (LinearLayout) findViewById(R.id.line4);
line5 = (LinearLayout) findViewById(R.id.line5);
line6 = (LinearLayout) findViewById(R.id.line6);
bgs.clear();
bgs.add(line1);bgs.add(line2);bgs.add(line3);bgs.add(line4);bgs.add(line5);bgs.add(line6);
tv1_1 = (TextView) findViewById(R.id.tv1_1);
tv2_1 = (TextView) findViewById(R.id.tv2_1);
tv3_1 = (TextView) findViewById(R.id.tv3_1);
tv4_1 = (TextView) findViewById(R.id.tv4_1);
tv5_1 = (TextView) findViewById(R.id.tv5_1);
tv6_1 = (TextView) findViewById(R.id.tv6_1);
tv1_2 = (TextView) findViewById(R.id.tv1_2);
tv2_2 = (TextView) findViewById(R.id.tv2_2);
tv3_2 = (TextView) findViewById(R.id.tv3_2);
tv4_2 = (TextView) findViewById(R.id.tv4_2);
tv5_2 = (TextView) findViewById(R.id.tv5_2);
tv6_2 = (TextView) findViewById(R.id.tv6_2);
bodyTexts.clear();titleTexts.clear();
bodyTexts.add(tv1_1);bodyTexts.add(tv2_1);bodyTexts.add(tv3_1);bodyTexts.add(tv4_1);bodyTexts.add(tv5_1);bodyTexts.add(tv6_1);
titleTexts.add(tv1_2);titleTexts.add(tv2_2);titleTexts.add(tv3_2);titleTexts.add(tv4_2);titleTexts.add(tv5_2);titleTexts.add(tv6_2);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基礎(chǔ)之使用Fragment控制切換多個(gè)頁(yè)面
- android TextView設(shè)置中文字體加粗實(shí)現(xiàn)方法
- Android應(yīng)用開(kāi)發(fā)SharedPreferences存儲(chǔ)數(shù)據(jù)的使用方法
- Android 動(dòng)畫(huà)之TranslateAnimation應(yīng)用詳解
- Android的Activity跳轉(zhuǎn)動(dòng)畫(huà)各種效果整理
- android客戶(hù)端從服務(wù)器端獲取json數(shù)據(jù)并解析的實(shí)現(xiàn)代碼
- android Handler詳細(xì)使用方法實(shí)例
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- Android SQLite數(shù)據(jù)庫(kù)增刪改查操作的使用詳解
相關(guān)文章
Android實(shí)現(xiàn)Tab切換界面功能詳解
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)Tab切換界面的功能,以及對(duì)Tab變化事件進(jìn)行監(jiān)聽(tīng)。文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05
Android開(kāi)發(fā)中判斷手機(jī)是否安裝了QQ或者微信
這篇文章主要介紹了Android開(kāi)發(fā)中判斷手機(jī)是否安裝了QQ或者微信的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android中使用Service實(shí)現(xiàn)后臺(tái)發(fā)送郵件功能實(shí)例
這篇文章主要介紹了Android中使用Service實(shí)現(xiàn)后臺(tái)發(fā)送郵件功能的方法,結(jié)合實(shí)例形式分析了Service實(shí)現(xiàn)郵件的發(fā)送、接收及權(quán)限控制相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android 逆向?qū)W習(xí)詳解及實(shí)例
本文主要介紹Android 逆向?qū)W習(xí),這里整理逆向?qū)W習(xí)的思路及學(xué)習(xí)要點(diǎn),并附示例代碼,幫助大家學(xué)習(xí)理解,有需要的小伙伴可以參考下2016-09-09
Android Studio 多層級(jí) Module 對(duì) aar 引用問(wèn)題解決方法
這篇文章主要介紹了Android Studio 多層級(jí) Module 對(duì) aar 引用問(wèn)題的解決方法,需要的朋友參考下2017-12-12
解決Android Studio突然不顯示logcat日志的問(wèn)題
這篇文章主要介紹了解決Android Studio突然不顯示logcat日志的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
安裝android開(kāi)發(fā)環(huán)境原始版(windows版)
安裝android開(kāi)發(fā)環(huán)境原始版(windows版)的詳細(xì)步驟2013-03-03
Android開(kāi)發(fā)之拖動(dòng)條/滑動(dòng)條控件、星級(jí)評(píng)分控件功能的實(shí)例代碼
這篇文章主要介紹了Android開(kāi)發(fā)之拖動(dòng)條/滑動(dòng)條控件、星級(jí)評(píng)分控件功能的實(shí)例代碼,需要的朋友可以參考下2019-05-05
Mac 下 Android Studio 不打印日志的解決辦法
這篇文章主要介紹了Mac 下 Android Studio 不打印日志的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-10-10

