- 原文鏈接: Additional Transformations with Transformation Library
- 原文作者: Future Studio
- 譯文出自: 小鄧子的簡書
- 譯者: 小鄧子
- 狀態(tài): 完成
Picasso圖像轉(zhuǎn)換類庫
如果你已經(jīng)有了一個圖像轉(zhuǎn)換的想法,希望在應(yīng)用中使用,可以花上幾分鐘的時間,了解一下picasso-transformations這個三方類庫。它是一個提供了各種Picasso轉(zhuǎn)換的方法集合。對于你的實現(xiàn)來說,它非常值得學(xué)習(xí)。
這個類庫有兩個不同的版本。其中擴(kuò)展版本包含更豐富的圖像轉(zhuǎn)換,使用設(shè)備的GPU進(jìn)行計算與渲染。需要一個額外的依賴,所以添加這兩個版本的方式有些不同。你應(yīng)該通過轉(zhuǎn)換類型列表,來決定哪個版本是真正需要的。
設(shè)置Picasso圖像轉(zhuǎn)換
設(shè)置方式非常的簡單!對于基礎(chǔ)版本的轉(zhuǎn)換,你只需在build.gradle
中添加一行命令:
dependencies {
compile 'jp.wasabeef:picasso-transformations:2.1.0'
}
如果你需要用到GPU方面的圖形轉(zhuǎn)換:
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile 'jp.wasabeef:picasso-transformations:2.1.0'
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.4.1'
}
圖形轉(zhuǎn)換的運用
當(dāng)build.gradle
文件完成同步操作之后,你就可以使用轉(zhuǎn)換集合中的任何一種了。使用方式與你自定義圖像轉(zhuǎn)換器無異。假設(shè),我們希望將圖像裁剪成圓形:
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.transform(new CropCircleTransformation())
.into(imageViewTransformationBlur);
你也可以通過鏈?zhǔn)秸{(diào)用,為Picasso請求同時添加多個圖像轉(zhuǎn)換器:
int color = Color.parseColor("#339b59b6");
Picasso
.with(context)
.load(UsageExampleListView.eatFoodyImages[0])
.transform(new ColorFilterTransformation(color))
.transform(new CropCircleTransformation())
.into(imageViewTransformationLibrary);