FitCenter.java revision ac28599e2b40e0dd6b97f6a91849585531264622
1package com.bumptech.glide.load.resource.bitmap;
2
3import android.graphics.Bitmap;
4import com.bumptech.glide.Resource;
5import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
6import com.bumptech.glide.load.Transformation;
7
8/**
9 * Scale the image uniformly (maintaining the image's aspect ratio) so that one of the dimensions of the image
10 * will be equal to the given dimension and the other will be less than the given dimension
11 */
12public class FitCenter implements Transformation<Bitmap> {
13    private BitmapPool pool;
14
15    public FitCenter(BitmapPool pool) {
16        this.pool = pool;
17    }
18
19    @Override
20    public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
21        if (outWidth <= 0 || outHeight <= 0) {
22            throw new IllegalArgumentException("Cannot fit center image to within width=" + outWidth + " or height="
23                    + outHeight);
24        }
25        Bitmap transformed = TransformationUtils.fitCenter(resource.get(), pool, outWidth, outHeight);
26        if (transformed == resource.get()) {
27            return resource;
28        } else {
29            return new BitmapResource(transformed, pool);
30        }
31    }
32
33    @Override
34    public String getId() {
35        return "FitCenter.com.bumptech.glide.load.Transformation";
36    }
37}
38
39
40