日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android palette组件用法,Palette颜色提取使用详解

發(fā)布時(shí)間:2024/1/23 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android palette组件用法,Palette颜色提取使用详解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

如果你試過(guò)android Lollipop的sdk,你可能注意到了Palette。Palette從圖像中提取突出的顏色,這樣可以把色值賦給ActionBar、或者其他,可以讓界面整個(gè)色調(diào)統(tǒng)一。

創(chuàng)建Palette實(shí)例

有四種創(chuàng)建實(shí)例的方法:// Synchronous methods.

// --------------------------------

// These should be used when you have access to the underlying image loading thread.

// Picasso allows this through a Transformation. For other libraries, YMMV.

// Uses the default palette size (16).

Palette p = Palette.generate(bitmap);

// Allows you to specify the maximum palette size, in this case 24.

Palette p = Palette.generate(bitmap, 24);

// Asynchronous methods

// --------------------------------

// This is the quick and easy integration path. Internally uses an AsyncTask so

// this may not be optimal (since you're dipping in and out of threads)

// Uses the default palette size (16).

Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {

@Override

public void onGenerated(Palette palette) {

// Here's your generated palette

}

});

// Allows you to specify the maximum palette size, in this case 24.

Palette.generateAsync(bitmap, 24, new Palette.PaletteAsyncListener() {

@Override

public void onGenerated(Palette palette) {

// Here's your generated palette

}

});

創(chuàng)建完一個(gè)實(shí)例之后,我們還需要得到一種采集的樣本(swatch),有6中樣本(swatch):Vibrant. Palette.getVibrantSwatch()

Vibrant dark. Palette.getDarkVibrantSwatch()

Vibrant light. Palette.getLightVibrantSwatch()

Muted. Palette.getMutedSwatch()

Muted dark. Palette.getDarkMutedSwatch()

Muted light. Palette.getLightMutedSwatch()

具體選擇哪一種取決于你自己,大多數(shù)情況下我們都使用Vibrant and Dark Vibrant。

使用樣本(swatch)

swatch有以下方法:getPopulation(): the amount of pixels which this swatch represents.

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.

比如如果你的TextView 有個(gè)背景圖片,要想讓字體顏色能夠和背景圖片匹配,則使用getBodyTextColor()比較合適,getTitleTextColor()其實(shí)應(yīng)該和getBodyTextColor()差不多。

下面的代碼則是展示了如何從一張圖片中提取顏色將textView的背景色設(shè)置成圖片的主色調(diào),然后再使用getTitleTextColor()來(lái)設(shè)置一個(gè)匹配的文字顏色。Palette.Swatch swatch = palette.getVibrantSwatch();

TextView titleView = ...;

if (swatch != null) {

titleView.setBackgroundColor(swatch.getRgb());

titleView.setTextColor(swatch.getTitleTextColor());

}

需要注意的是getVibrantSwatch()可能會(huì)返回一個(gè)null值,所以檢查一下是必須的。

size的問(wèn)題

你還可以使用如下方法一次性獲得所有的swatch:List swatches = palette.getSwatches();

在上面的代碼中,你可能注意到了可以設(shè)置palette的size。size越大,花費(fèi)的時(shí)間越長(zhǎng),而越小,可以選擇的色彩也越小。最佳的選擇是根據(jù)image的用途:

頭像之類的,size最好在24-32之間;

風(fēng)景大圖之類的 size差不多在8-16;

默認(rèn)是16.

總結(jié)

以上是生活随笔為你收集整理的android palette组件用法,Palette颜色提取使用详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。