【译】图像的旋转与转换

  • 原文链接: Image Rotation and Transformation
  • 原文作者: Future Studio
  • 译文出自: 小鄧子的
  • 译者: 小鄧子
  • 状态: 完成

图像旋转

在讲图像转换之前,有一个功能你可能经常用到:图片旋转。Picasso内置了图片旋转的功能。这里有两个选项:简单旋转和复杂旋转。

简单旋转

简单旋转可以通过调用rotate(float degrees)来实现。它能够根据传入的角度进行简单的旋转。0~360度之间的值都是有意义的(0或360度的旋转,图片无任何变化)。让我们看一下代码示例:

Picasso
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .rotate(90f)
    .into(imageViewSimpleRotate);

这张图片将被旋转90度。

复杂旋转

默认情况下,旋转的中心(“pivot point”)是0,0。某些情况下,你可能要围绕一个特殊的轴点进行旋转,这个轴点可能不是中心点。那么你可以使用rotate(float degrees, float pivotX, float pivotY)。代码示例如下:

Picasso
    .with(context)
    .load(R.drawable.floorplan)
    .rotate(45f, 200f, 100f)
    .into(imageViewComplexRotate);

图像转换

旋转只不过是图像处理技术中的一小部分。Picasso允许通过实现Transformation接口的方式,来对图像进行各种处理。你可以实现一个Transformation,并重写主要函数:transform(android.graphics.Bitmap source),这个方法会得到一个Bitmap,然后返回一个转换后的Bitmap

当你实现了自定义转换器后,只需要通过transform(Transformation transformation)设置给Picasso即可。图像展示之前就已经被转换好了。

示例#1:对图片进行模糊处理

我们在之前的博客中介绍了如何在不依赖Picasso的情况下对单张图片添加模糊效果。我们从两个类似的解决方案(1, 2)中获得了启发,并优化了代码。下面这个类实现了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";
    }
}

把转换器添加到Picasso请求上也是极其的简单:

Picasso
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .transform(new BlurTransformation(context))
    .into(imageViewTransformationBlur);

图片在展示到目标ImageView上之前,就已经添加了模糊效果。

示例#2:为图片同时添加模糊和灰度效果

Picasso也允许Transformation集合作为参数:transform(List transformations),这就意味着,你可以对图像使用一些列的转换操作。

基于上一节所提到的模糊效果的基础上,我们再从Picasso官方示例中,添加灰度转换。这个灰度的实现示例如下:

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()";
    }
}

如果需要添加多种图像转换技术,可以构建一个List集合,然后作为参数传入:

List 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的存在,足够让你改变图片以适应需求。但是在创建Transformation实现之前,有两点需要牢记:

  • 如果不需要转换,就返回原来的图像。
  • 当创建一个新的Bitmap之后,请在旧的Bitmap上调用.recycle()

你可能感兴趣的:(【译】图像的旋转与转换)