1 /*
2 * Copyright (C) 2003-2015 eXo Platform SAS.
3 *
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18 */
19 package org.exoplatform.utils.image;
20
21 import org.exoplatform.R;
22
23 import com.squareup.picasso.Transformation;
24
25 import android.content.Context;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapShader;
28 import android.graphics.Canvas;
29 import android.graphics.Paint;
30 import android.graphics.RectF;
31 import android.graphics.Shader;
32
33 /**
34 * Created by The eXo Platform SAS May 14, 2015
35 *
36 * @author Philippe Aristote paristote@exoplatform.com
37 */
38 public class RoundedCornersTranformer implements Transformation {
39
40 Context mContext;
41
42 public RoundedCornersTranformer(Context ctx) {
43 this.mContext = ctx;
44 }
45
46 @Override
47 public String key() {
48 return "exo_rounded_corners()";
49 }
50
51 @Override
52 public Bitmap transform(Bitmap source) {
53 // Start a new bitmap with the same dimensions and config as the source
54 Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
55 // Create a shader based on the source bitmap
56 BitmapShader shader;
57 shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
58 // Create a paint that uses the shader (bitmap) as texture
59 Paint paint = new Paint();
60 paint.setAntiAlias(true);
61 paint.setShader(shader);
62 // Create a frame with a padding of 2 inside the source bitmap dimensions
63 int padding = 2;
64 RectF frame = new RectF(padding, padding, source.getWidth() - padding, source.getHeight() - padding);
65 // Create a new canvas to draw on the new bitmap
66 Canvas canvas = new Canvas(result);
67 // Draw the rounded rect on the canvas, with the original bitmap (the paint)
68 int radius = mContext.getResources().getDimensionPixelSize(R.dimen.image_radius);
69 canvas.drawRoundRect(frame, radius, radius, paint);
70 // Finish
71 source.recycle();
72 return result;
73 }
74
75 }