mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-10-05 02:13:22 +08:00
Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
68a9097962 | ||
|
6269999abc | ||
|
7e93ccd9af | ||
|
07b7d24e13 | ||
|
ca648cbc7d | ||
|
67ac3e31a4 | ||
|
1534573e8f | ||
|
a5487ade83 | ||
|
62abca0b1e | ||
|
a6bbdb0694 | ||
|
2f75284dd4 | ||
|
5772f60c61 | ||
|
cf5b0bbf1d | ||
|
a0e4705452 | ||
|
d474911010 | ||
|
5775d8211c | ||
|
3c3d2c0123 | ||
|
ccf79de0f0 | ||
|
b7f1b8cd50 | ||
|
6481449de7 | ||
|
dd94a0a16a | ||
|
bf2eaadf2d |
16
CHANGELOG.md
16
CHANGELOG.md
@@ -1,6 +1,22 @@
|
|||||||
Change Log
|
Change Log
|
||||||
==========
|
==========
|
||||||
|
|
||||||
|
Version 1.0.3 *(2015-02-05)*
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
Bug fix
|
||||||
|
|
||||||
|
Version 1.0.2 *(2015-02-04)*
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
Refactor: use BimapPool
|
||||||
|
|
||||||
|
Version 1.0.1 *(2015-01-21)*
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
fix: Blur Transformation now woking at Android 4.3
|
||||||
|
add: GPUImage to Gradle dependency
|
||||||
|
|
||||||
Version 1.0.0 *(2015-01-12)*
|
Version 1.0.0 *(2015-01-12)*
|
||||||
----------------------------
|
----------------------------
|
||||||
|
|
||||||
|
25
README.md
25
README.md
@@ -8,6 +8,10 @@ An Android transformation library providing a variety of image transformations f
|
|||||||
|
|
||||||
Please feel free to use this.
|
Please feel free to use this.
|
||||||
|
|
||||||
|
|
||||||
|
#### Are you using Picasso?
|
||||||
|
[Picasso Transformations](https://github.com/wasabeef/picasso-transformations)
|
||||||
|
|
||||||
# Demo
|
# Demo
|
||||||
|
|
||||||
### Original Image
|
### Original Image
|
||||||
@@ -24,10 +28,12 @@ Please feel free to use this.
|
|||||||
```groovy
|
```groovy
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
|
mavenCentral() // GPUImage for Android
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'jp.wasabeef:glide-transformations:1.0.0'
|
compile 'jp.wasabeef:glide-transformations:1.0.3@aar'
|
||||||
|
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3@aar'
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -52,6 +58,21 @@ Glide.with(this).load(R.drawable.demo).bitmapTransform(
|
|||||||
.into((ImageView) findViewById(R.id.image));
|
.into((ImageView) findViewById(R.id.image));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Step 4
|
||||||
|
|
||||||
|
If you are using `BlurTransformation`.
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
android {
|
||||||
|
...
|
||||||
|
defaultConfig {
|
||||||
|
...
|
||||||
|
renderscriptTargetApi 21
|
||||||
|
renderscriptSupportModeEnabled true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Transformations
|
## Transformations
|
||||||
|
|
||||||
### Crop
|
### Crop
|
||||||
@@ -77,7 +98,7 @@ Daichi Furiya (Wasabeef) - <dadadada.chop@gmail.com>
|
|||||||
|
|
||||||
<a href="https://twitter.com/wasabeef_jp">
|
<a href="https://twitter.com/wasabeef_jp">
|
||||||
<img alt="Follow me on Twitter"
|
<img alt="Follow me on Twitter"
|
||||||
src="https://raw.githubusercontent.com/wasabeef/wasabeef.github.io/master/art/twitter.png" width="75"/>
|
src="https://raw.githubusercontent.com/wasabeef/art/master/twitter.png" width="75"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
Thanks
|
Thanks
|
||||||
|
@@ -16,5 +16,6 @@ buildscript {
|
|||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,8 @@ android {
|
|||||||
targetSdkVersion TARGET_SDK_VERSION as int
|
targetSdkVersion TARGET_SDK_VERSION as int
|
||||||
versionCode VERSION_CODE as int
|
versionCode VERSION_CODE as int
|
||||||
versionName VERSION_NAME
|
versionName VERSION_NAME
|
||||||
|
renderscriptTargetApi RENDERSCRIPT_TARGET_API as int
|
||||||
|
renderscriptSupportModeEnabled true
|
||||||
}
|
}
|
||||||
|
|
||||||
signingConfigs {
|
signingConfigs {
|
||||||
@@ -51,7 +53,9 @@ def getKeyAliasPasswordProperty() {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile project(':transformations')
|
compile project(':transformations')
|
||||||
compile 'com.android.support:appcompat-v7:+'
|
// compile 'jp.wasabeef:glide-transformations:1.0.3'
|
||||||
|
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3'
|
||||||
|
compile 'com.android.support:appcompat-v7:21.+'
|
||||||
compile 'com.android.support:recyclerview-v7:21.+'
|
compile 'com.android.support:recyclerview-v7:21.+'
|
||||||
compile 'com.github.bumptech.glide:glide:3.+'
|
compile 'com.github.bumptech.glide:glide:3.5.1'
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
VERSION_NAME=1.0.0
|
VERSION_NAME=1.0.3
|
||||||
VERSION_CODE=3
|
VERSION_CODE=3
|
||||||
GROUP=jp.wasabeef
|
GROUP=jp.wasabeef
|
||||||
ARTIFACT_ID=glide-transformations
|
ARTIFACT_ID=glide-transformations
|
||||||
|
@@ -16,7 +16,8 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
compile 'com.github.bumptech.glide:glide:3.4.0'
|
compile 'com.github.bumptech.glide:glide:3.5.1'
|
||||||
|
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3'
|
||||||
}
|
}
|
||||||
|
|
||||||
android.libraryVariants.all { variant ->
|
android.libraryVariants.all { variant ->
|
||||||
|
Binary file not shown.
@@ -23,11 +23,12 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.os.Build;
|
import android.graphics.Canvas;
|
||||||
import android.renderscript.Allocation;
|
import android.graphics.Paint;
|
||||||
import android.renderscript.Element;
|
import android.graphics.Rect;
|
||||||
import android.renderscript.RenderScript;
|
import android.support.v8.renderscript.Allocation;
|
||||||
import android.renderscript.ScriptIntrinsicBlur;
|
import android.support.v8.renderscript.RenderScript;
|
||||||
|
import android.support.v8.renderscript.ScriptIntrinsicBlur;
|
||||||
|
|
||||||
public class BlurTransformation implements Transformation<Bitmap> {
|
public class BlurTransformation implements Transformation<Bitmap> {
|
||||||
|
|
||||||
@@ -45,254 +46,36 @@ public class BlurTransformation implements Transformation<Bitmap> {
|
|||||||
public BlurTransformation(Context context, BitmapPool pool, int radius) {
|
public BlurTransformation(Context context, BitmapPool pool, int radius) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mBitmapPool = pool;
|
mBitmapPool = pool;
|
||||||
this.mRadius = radius;
|
mRadius = radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
Bitmap source = resource.get();
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT > 16) {
|
int width = source.getWidth();
|
||||||
Bitmap bitmap = source.copy(source.getConfig(), true);
|
int height = source.getHeight();
|
||||||
|
|
||||||
final RenderScript rs = RenderScript.create(mContext);
|
Bitmap.Config config =
|
||||||
final Allocation input = Allocation.createFromBitmap(rs, source,
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
Allocation.MipmapControl.MIPMAP_NONE,
|
Bitmap bitmap = mBitmapPool.get(width, height, config);
|
||||||
Allocation.USAGE_SCRIPT);
|
if (bitmap == null) {
|
||||||
final Allocation output = Allocation.createTyped(rs, input.getType());
|
bitmap = Bitmap.createBitmap(width, height, config);
|
||||||
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
|
|
||||||
script.setRadius(mRadius /* e.g. 3.f */);
|
|
||||||
script.setInput(input);
|
|
||||||
script.forEach(output);
|
|
||||||
output.copyTo(bitmap);
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stack Blur v1.0 from
|
Canvas canvas = new Canvas(bitmap);
|
||||||
// http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html
|
canvas.drawBitmap(source, 0, 0, null);
|
||||||
//
|
|
||||||
// Java Author: Mario Klingemann <mario at quasimondo.com>
|
|
||||||
// http://incubator.quasimondo.com
|
|
||||||
// created Feburary 29, 2004
|
|
||||||
// Android port : Yahel Bouaziz <yahel at kayenko.com>
|
|
||||||
// http://www.kayenko.com
|
|
||||||
// ported april 5th, 2012
|
|
||||||
|
|
||||||
// This is a compromise between Gaussian Blur and Box blur
|
RenderScript rs = RenderScript.create(mContext);
|
||||||
// It creates much better looking blurs than Box Blur, but is
|
Allocation overlayAlloc = Allocation.createFromBitmap(rs, bitmap);
|
||||||
// 7x faster than my Gaussian Blur implementation.
|
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
|
||||||
//
|
blur.setInput(overlayAlloc);
|
||||||
// I called it Stack Blur because this describes best how this
|
blur.setRadius(mRadius);
|
||||||
// filter works internally: it creates a kind of moving stack
|
blur.forEach(overlayAlloc);
|
||||||
// of colors whilst scanning through the image. Thereby it
|
overlayAlloc.copyTo(bitmap);
|
||||||
// just has to add one new block of color to the right side
|
|
||||||
// of the stack and remove the leftmost color. The remaining
|
|
||||||
// colors on the topmost layer of the stack are either added on
|
|
||||||
// or reduced by one, depending on if they are on the right or
|
|
||||||
// on the left side of the stack.
|
|
||||||
//
|
|
||||||
// If you are using this algorithm in your code please add
|
|
||||||
// the following line:
|
|
||||||
//
|
|
||||||
// Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com>
|
|
||||||
|
|
||||||
Bitmap bitmap = source.copy(source.getConfig(), true);
|
|
||||||
|
|
||||||
if (mRadius < 1) {
|
|
||||||
return (null);
|
|
||||||
}
|
|
||||||
|
|
||||||
int w = bitmap.getWidth();
|
|
||||||
int h = bitmap.getHeight();
|
|
||||||
|
|
||||||
int[] pix = new int[w * h];
|
|
||||||
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
|
|
||||||
|
|
||||||
int wm = w - 1;
|
|
||||||
int hm = h - 1;
|
|
||||||
int wh = w * h;
|
|
||||||
int div = mRadius + mRadius + 1;
|
|
||||||
|
|
||||||
int r[] = new int[wh];
|
|
||||||
int g[] = new int[wh];
|
|
||||||
int b[] = new int[wh];
|
|
||||||
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
|
|
||||||
int vmin[] = new int[Math.max(w, h)];
|
|
||||||
|
|
||||||
int divsum = (div + 1) >> 1;
|
|
||||||
divsum *= divsum;
|
|
||||||
int dv[] = new int[256 * divsum];
|
|
||||||
for (i = 0; i < 256 * divsum; i++) {
|
|
||||||
dv[i] = (i / divsum);
|
|
||||||
}
|
|
||||||
|
|
||||||
yw = yi = 0;
|
|
||||||
|
|
||||||
int[][] stack = new int[div][3];
|
|
||||||
int stackpointer;
|
|
||||||
int stackstart;
|
|
||||||
int[] sir;
|
|
||||||
int rbs;
|
|
||||||
int r1 = mRadius + 1;
|
|
||||||
int routsum, goutsum, boutsum;
|
|
||||||
int rinsum, ginsum, binsum;
|
|
||||||
|
|
||||||
for (y = 0; y < h; y++) {
|
|
||||||
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
|
|
||||||
for (i = -mRadius; i <= mRadius; i++) {
|
|
||||||
p = pix[yi + Math.min(wm, Math.max(i, 0))];
|
|
||||||
sir = stack[i + mRadius];
|
|
||||||
sir[0] = (p & 0xff0000) >> 16;
|
|
||||||
sir[1] = (p & 0x00ff00) >> 8;
|
|
||||||
sir[2] = (p & 0x0000ff);
|
|
||||||
rbs = r1 - Math.abs(i);
|
|
||||||
rsum += sir[0] * rbs;
|
|
||||||
gsum += sir[1] * rbs;
|
|
||||||
bsum += sir[2] * rbs;
|
|
||||||
if (i > 0) {
|
|
||||||
rinsum += sir[0];
|
|
||||||
ginsum += sir[1];
|
|
||||||
binsum += sir[2];
|
|
||||||
} else {
|
|
||||||
routsum += sir[0];
|
|
||||||
goutsum += sir[1];
|
|
||||||
boutsum += sir[2];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stackpointer = mRadius;
|
|
||||||
|
|
||||||
for (x = 0; x < w; x++) {
|
|
||||||
|
|
||||||
r[yi] = dv[rsum];
|
|
||||||
g[yi] = dv[gsum];
|
|
||||||
b[yi] = dv[bsum];
|
|
||||||
|
|
||||||
rsum -= routsum;
|
|
||||||
gsum -= goutsum;
|
|
||||||
bsum -= boutsum;
|
|
||||||
|
|
||||||
stackstart = stackpointer - mRadius + div;
|
|
||||||
sir = stack[stackstart % div];
|
|
||||||
|
|
||||||
routsum -= sir[0];
|
|
||||||
goutsum -= sir[1];
|
|
||||||
boutsum -= sir[2];
|
|
||||||
|
|
||||||
if (y == 0) {
|
|
||||||
vmin[x] = Math.min(x + mRadius + 1, wm);
|
|
||||||
}
|
|
||||||
p = pix[yw + vmin[x]];
|
|
||||||
|
|
||||||
sir[0] = (p & 0xff0000) >> 16;
|
|
||||||
sir[1] = (p & 0x00ff00) >> 8;
|
|
||||||
sir[2] = (p & 0x0000ff);
|
|
||||||
|
|
||||||
rinsum += sir[0];
|
|
||||||
ginsum += sir[1];
|
|
||||||
binsum += sir[2];
|
|
||||||
|
|
||||||
rsum += rinsum;
|
|
||||||
gsum += ginsum;
|
|
||||||
bsum += binsum;
|
|
||||||
|
|
||||||
stackpointer = (stackpointer + 1) % div;
|
|
||||||
sir = stack[(stackpointer) % div];
|
|
||||||
|
|
||||||
routsum += sir[0];
|
|
||||||
goutsum += sir[1];
|
|
||||||
boutsum += sir[2];
|
|
||||||
|
|
||||||
rinsum -= sir[0];
|
|
||||||
ginsum -= sir[1];
|
|
||||||
binsum -= sir[2];
|
|
||||||
|
|
||||||
yi++;
|
|
||||||
}
|
|
||||||
yw += w;
|
|
||||||
}
|
|
||||||
for (x = 0; x < w; x++) {
|
|
||||||
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
|
|
||||||
yp = -mRadius * w;
|
|
||||||
for (i = -mRadius; i <= mRadius; i++) {
|
|
||||||
yi = Math.max(0, yp) + x;
|
|
||||||
|
|
||||||
sir = stack[i + mRadius];
|
|
||||||
|
|
||||||
sir[0] = r[yi];
|
|
||||||
sir[1] = g[yi];
|
|
||||||
sir[2] = b[yi];
|
|
||||||
|
|
||||||
rbs = r1 - Math.abs(i);
|
|
||||||
|
|
||||||
rsum += r[yi] * rbs;
|
|
||||||
gsum += g[yi] * rbs;
|
|
||||||
bsum += b[yi] * rbs;
|
|
||||||
|
|
||||||
if (i > 0) {
|
|
||||||
rinsum += sir[0];
|
|
||||||
ginsum += sir[1];
|
|
||||||
binsum += sir[2];
|
|
||||||
} else {
|
|
||||||
routsum += sir[0];
|
|
||||||
goutsum += sir[1];
|
|
||||||
boutsum += sir[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i < hm) {
|
|
||||||
yp += w;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
yi = x;
|
|
||||||
stackpointer = mRadius;
|
|
||||||
for (y = 0; y < h; y++) {
|
|
||||||
// Preserve alpha channel: ( 0xff000000 & pix[yi] )
|
|
||||||
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];
|
|
||||||
|
|
||||||
rsum -= routsum;
|
|
||||||
gsum -= goutsum;
|
|
||||||
bsum -= boutsum;
|
|
||||||
|
|
||||||
stackstart = stackpointer - mRadius + div;
|
|
||||||
sir = stack[stackstart % div];
|
|
||||||
|
|
||||||
routsum -= sir[0];
|
|
||||||
goutsum -= sir[1];
|
|
||||||
boutsum -= sir[2];
|
|
||||||
|
|
||||||
if (x == 0) {
|
|
||||||
vmin[y] = Math.min(y + r1, hm) * w;
|
|
||||||
}
|
|
||||||
p = x + vmin[y];
|
|
||||||
|
|
||||||
sir[0] = r[p];
|
|
||||||
sir[1] = g[p];
|
|
||||||
sir[2] = b[p];
|
|
||||||
|
|
||||||
rinsum += sir[0];
|
|
||||||
ginsum += sir[1];
|
|
||||||
binsum += sir[2];
|
|
||||||
|
|
||||||
rsum += rinsum;
|
|
||||||
gsum += ginsum;
|
|
||||||
bsum += binsum;
|
|
||||||
|
|
||||||
stackpointer = (stackpointer + 1) % div;
|
|
||||||
sir = stack[stackpointer];
|
|
||||||
|
|
||||||
routsum += sir[0];
|
|
||||||
goutsum += sir[1];
|
|
||||||
boutsum += sir[2];
|
|
||||||
|
|
||||||
rinsum -= sir[0];
|
|
||||||
ginsum -= sir[1];
|
|
||||||
binsum -= sir[2];
|
|
||||||
|
|
||||||
yi += w;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
rs.destroy();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
@@ -45,13 +45,19 @@ public class ColorFilterTransformation implements Transformation<Bitmap> {
|
|||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
Bitmap.Config config =
|
||||||
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
|
Bitmap bitmap = mBitmapPool.get(width, height, config);
|
||||||
|
if (bitmap == null) {
|
||||||
|
bitmap = Bitmap.createBitmap(width, height, config);
|
||||||
|
}
|
||||||
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
|
paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
canvas.drawBitmap(source, 0, 0, paint);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
@@ -24,6 +24,7 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
|
|||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapShader;
|
import android.graphics.BitmapShader;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.Matrix;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
|
|
||||||
public class CropCircleTransformation implements Transformation<Bitmap> {
|
public class CropCircleTransformation implements Transformation<Bitmap> {
|
||||||
@@ -42,24 +43,27 @@ public class CropCircleTransformation implements Transformation<Bitmap> {
|
|||||||
int width = (source.getWidth() - size) / 2;
|
int width = (source.getWidth() - size) / 2;
|
||||||
int height = (source.getHeight() - size) / 2;
|
int height = (source.getHeight() - size) / 2;
|
||||||
|
|
||||||
Bitmap squaredBitmap = Bitmap.createBitmap(source, width, height, size, size);
|
Bitmap.Config config =
|
||||||
if (squaredBitmap != source) {
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
source.recycle();
|
Bitmap bitmap = mBitmapPool.get(size, size, config);
|
||||||
|
if (bitmap == null) {
|
||||||
|
bitmap = Bitmap.createBitmap(size, size, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
|
|
||||||
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
|
BitmapShader shader = new BitmapShader(source, BitmapShader.TileMode.CLAMP,
|
||||||
BitmapShader.TileMode.CLAMP);
|
BitmapShader.TileMode.CLAMP);
|
||||||
|
Matrix matrix = new Matrix();
|
||||||
|
matrix.setTranslate(-width, -height);
|
||||||
|
shader.setLocalMatrix(matrix);
|
||||||
paint.setShader(shader);
|
paint.setShader(shader);
|
||||||
paint.setAntiAlias(true);
|
paint.setAntiAlias(true);
|
||||||
|
|
||||||
float r = size / 2f;
|
float r = size / 2f;
|
||||||
canvas.drawCircle(r, r, r, paint);
|
canvas.drawCircle(r, r, r, paint);
|
||||||
|
|
||||||
squaredBitmap.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
@@ -33,21 +33,21 @@ public class CropSquareTransformation implements Transformation<Bitmap> {
|
|||||||
this.mBitmapPool = pool;
|
this.mBitmapPool = pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CropSquareTransformation(BitmapPool pool, int width, int height) {
|
|
||||||
mBitmapPool = pool;
|
|
||||||
mWidth = width;
|
|
||||||
mHeight = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
Bitmap source = resource.get();
|
||||||
int size = Math.min(source.getWidth(), source.getHeight());
|
int size = Math.min(source.getWidth(), source.getHeight());
|
||||||
|
|
||||||
int width = (source.getWidth() - size) / 2;
|
mWidth = (source.getWidth() - size) / 2;
|
||||||
int height = (source.getHeight() - size) / 2;
|
mHeight = (source.getHeight() - size) / 2;
|
||||||
|
|
||||||
|
Bitmap.Config config =
|
||||||
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
|
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
|
||||||
|
if (bitmap == null) {
|
||||||
|
bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size);
|
||||||
|
}
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(source, width, height, size, size);
|
|
||||||
if (bitmap != source) {
|
if (bitmap != source) {
|
||||||
source.recycle();
|
source.recycle();
|
||||||
}
|
}
|
||||||
|
@@ -51,6 +51,13 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
mHeight = source.getHeight();
|
mHeight = source.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bitmap.Config config =
|
||||||
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
|
Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config);
|
||||||
|
if (bitmap == null) {
|
||||||
|
bitmap = Bitmap.createBitmap(mWidth, mHeight, config);
|
||||||
|
}
|
||||||
|
|
||||||
float scaleX = (float) mWidth / source.getWidth();
|
float scaleX = (float) mWidth / source.getWidth();
|
||||||
float scaleY = (float) mHeight / source.getHeight();
|
float scaleY = (float) mHeight / source.getHeight();
|
||||||
float scale = Math.max(scaleX, scaleY);
|
float scale = Math.max(scaleX, scaleY);
|
||||||
@@ -61,9 +68,9 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
float top = (mHeight - scaledHeight) / 2;
|
float top = (mHeight - scaledHeight) / 2;
|
||||||
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
|
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, source.getConfig());
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
canvas.drawBitmap(source, null, targetRect, null);
|
canvas.drawBitmap(source, null, targetRect, null);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
@@ -42,7 +42,12 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
|
|||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
Bitmap.Config config =
|
||||||
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
|
Bitmap bitmap = mBitmapPool.get(width, height, config);
|
||||||
|
if (bitmap == null) {
|
||||||
|
bitmap = Bitmap.createBitmap(width, height, config);
|
||||||
|
}
|
||||||
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
ColorMatrix saturation = new ColorMatrix();
|
ColorMatrix saturation = new ColorMatrix();
|
||||||
@@ -50,6 +55,7 @@ public class GrayscaleTransformation implements Transformation<Bitmap> {
|
|||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
|
paint.setColorFilter(new ColorMatrixColorFilter(saturation));
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
canvas.drawBitmap(source, 0, 0, paint);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
@@ -48,7 +48,12 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
|
|||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
Bitmap.Config config =
|
||||||
|
source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
|
||||||
|
Bitmap bitmap = mBitmapPool.get(width, height, config);
|
||||||
|
if (bitmap == null) {
|
||||||
|
bitmap = Bitmap.createBitmap(width, height, config);
|
||||||
|
}
|
||||||
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
Paint paint = new Paint();
|
Paint paint = new Paint();
|
||||||
@@ -56,6 +61,7 @@ public class RoundedCornersTransformation implements Transformation<Bitmap> {
|
|||||||
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
|
paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
|
||||||
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin),
|
canvas.drawRoundRect(new RectF(margin, margin, width - margin, height - margin),
|
||||||
radius, radius, paint);
|
radius, radius, paint);
|
||||||
|
|
||||||
source.recycle();
|
source.recycle();
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(bitmap, mBitmapPool);
|
||||||
|
Reference in New Issue
Block a user