RoundedTransformation.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Copyright 2017 Andrew Dawson
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. package com.keylesspalace.tusky;
  16. import android.graphics.Bitmap;
  17. import android.graphics.BitmapShader;
  18. import android.graphics.Canvas;
  19. import android.graphics.Paint;
  20. import android.graphics.RectF;
  21. import android.graphics.Shader;
  22. import com.squareup.picasso.Transformation;
  23. public class RoundedTransformation implements Transformation {
  24. private final int radius;
  25. private final int margin;
  26. public RoundedTransformation(final int radius, final int margin) {
  27. this.radius = radius;
  28. this.margin = margin;
  29. }
  30. @Override
  31. public Bitmap transform(Bitmap source) {
  32. final Paint paint = new Paint();
  33. paint.setAntiAlias(true);
  34. paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
  35. Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  36. Canvas canvas = new Canvas(output);
  37. canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);
  38. if (source != output) {
  39. source.recycle();
  40. }
  41. return output;
  42. }
  43. @Override
  44. public String key() {
  45. return "rounded";
  46. }
  47. }