- 原文鏈接: Image Rotation and Transformation
- 原文作者: Future Studio
- 譯文出自: 小鄧子的簡書
- 譯者: 小鄧子
- 狀態(tài): 完成
圖像旋轉(zhuǎn)
在講圖像轉(zhuǎn)換之前糙捺,有一個功能你可能經(jīng)常用到:圖片旋轉(zhuǎn)舶胀。Picasso內(nèi)置了圖片旋轉(zhuǎn)的功能。這里有兩個選項:簡單旋轉(zhuǎn)和復(fù)雜旋轉(zhuǎn)冬骚。
簡單旋轉(zhuǎn)
簡單旋轉(zhuǎn)可以通過調(diào)用rotate(float degrees)
來實現(xiàn)世囊。它能夠根據(jù)傳入的角度進行簡單的旋轉(zhuǎn)别瞭。0~360度之間的值都是有意義的(0或360度的旋轉(zhuǎn),圖片無任何變化)株憾。讓我們看一下代碼示例:
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.rotate(90f)
.into(imageViewSimpleRotate);
這張圖片將被旋轉(zhuǎn)90度蝙寨。
復(fù)雜旋轉(zhuǎn)
默認(rèn)情況下,旋轉(zhuǎn)的中心(“pivot point”)是0,0
嗤瞎。某些情況下墙歪,你可能要圍繞一個特殊的軸點進行旋轉(zhuǎn),這個軸點可能不是中心點贝奇。那么你可以使用rotate(float degrees, float pivotX, float pivotY)
虹菲。代碼示例如下:
Picasso
.with(context)
.load(R.drawable.floorplan)
.rotate(45f, 200f, 100f)
.into(imageViewComplexRotate);
圖像轉(zhuǎn)換
旋轉(zhuǎn)只不過是圖像處理技術(shù)中的一小部分恩商。Picasso允許通過實現(xiàn)Transformation
接口的方式袁勺,來對圖像進行各種處理。你可以實現(xiàn)一個Transformation,并重寫主要函數(shù):transform(android.graphics.Bitmap source)
撒犀,這個方法會得到一個Bitmap
,然后返回一個轉(zhuǎn)換后的Bitmap
怎炊。
當(dāng)你實現(xiàn)了自定義轉(zhuǎn)換器后混移,只需要通過transform(Transformation transformation)
設(shè)置給Picasso即可。圖像展示之前就已經(jīng)被轉(zhuǎn)換好了瘩欺。
示例#1:對圖片進行模糊處理
我們在之前的博客中介紹了如何在不依賴Picasso的情況下對單張圖片添加模糊效果必盖。我們從兩個類似的解決方案(1, 2)中獲得了啟發(fā),并優(yōu)化了代碼俱饿。下面這個類實現(xiàn)了Transformation
接口以及必要的方法:
public class BlurTransformation implements Transformation {
RenderScript rs;
public BlurTransformation(Context context) {
super();
rs = RenderScript.create(context);
}
@Override
public Bitmap transform(Bitmap bitmap) {
// Create another bitmap that will hold the results of the filter.
Bitmap blurredBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
bitmap.recycle();
return blurredBitmap;
}
@Override
public String key() {
return "blur";
}
}
把轉(zhuǎn)換器添加到Picasso請求上也是極其的簡單:
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.transform(new BlurTransformation(context))
.into(imageViewTransformationBlur);
圖片在展示到目標(biāo)ImageView
上之前歌粥,就已經(jīng)添加了模糊效果。
示例#2:為圖片同時添加模糊和灰度效果
Picasso也允許Transformation
集合作為參數(shù):transform(List<? extends Transformation> transformations)
拍埠,這就意味著失驶,你可以對圖像使用一些列的轉(zhuǎn)換操作。
基于上一節(jié)所提到的模糊效果的基礎(chǔ)上枣购,我們再從Picasso官方示例中嬉探,添加灰度轉(zhuǎn)換。這個灰度的實現(xiàn)示例如下:
public class GrayscaleTransformation implements Transformation {
private final Picasso picasso;
public GrayscaleTransformation(Picasso picasso) {
this.picasso = picasso;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap result = createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
Bitmap noise;
try {
noise = picasso.load(R.drawable.noise).get();
} catch (IOException e) {
throw new RuntimeException("Failed to apply transformation! Missing resource.");
}
BitmapShader shader = new BitmapShader(noise, REPEAT, REPEAT);
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(source, 0, 0, paint);
paint.setColorFilter(null);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
source.recycle();
noise.recycle();
return result;
}
@Override
public String key() {
return "grayscaleTransformation()";
}
}
如果需要添加多種圖像轉(zhuǎn)換技術(shù)棉圈,可以構(gòu)建一個List
集合涩堤,然后作為參數(shù)傳入:
List<Transformation> transformations = new ArrayList<>();
transformations.add(new GrayscaleTransformation(Picasso.with(context)));
transformations.add(new BlurTransformation(context));
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.transform(transformations)
.into(imageViewTransformationsMultiple);
Transformation
的存在,足夠讓你改變圖片以適應(yīng)需求分瘾。但是在創(chuàng)建Transformation
實現(xiàn)之前胎围,有兩點需要牢記:
- 如果不需要轉(zhuǎn)換,就返回原來的圖像德召。
- 當(dāng)創(chuàng)建一個新的
Bitmap
之后白魂,請在舊的Bitmap
上調(diào)用.recycle()
。