【譯】圖像的旋轉(zhuǎn)與轉(zhuǎn)換

圖像旋轉(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ù)傳入的角度進(jìn)行簡單的旋轉(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。某些情況下,你可能要圍繞一個特殊的軸點進(jìn)行旋轉(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接口的方式,來對圖像進(jìn)行各種處理。你可以實現(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:對圖片進(jìn)行模糊處理

我們在之前的博客中介紹了如何在不依賴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()。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容