两个Picasso的Transformation

圆形


  1. import android.graphics.Bitmap;
  2. import android.graphics.BitmapShader;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import com.squareup.picasso.Transformation;
  6. ``
  7. public class CircleTransform implements Transformation {
  8. @Override
  9. public Bitmap transform(Bitmap source) {
  10. int size = Math.min(source.getWidth(), source.getHeight());
  11. int x = (source.getWidth() - size) / 2;
  12. int y = (source.getHeight() - size) / 2;
  13. Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
  14. if (squaredBitmap != source) {
  15. source.recycle();
  16. }
  17. Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
  18. Canvas canvas = new Canvas(bitmap);
  19. Paint paint = new Paint();
  20. BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
  21. paint.setShader(shader);
  22. paint.setAntiAlias(true);
  23. float r = size / 2f;
  24. canvas.drawCircle(r, r, r, paint);
  25. squaredBitmap.recycle();
  26. return bitmap;
  27. }
  28. ``
  29. @Override
  30. public String key() {
  31. return "circle";
  32. }
  33. }

圆角


  1. import android.graphics.Bitmap;
  2. import android.graphics.BitmapShader;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.graphics.RectF;
  6. import android.graphics.Shader;
  7. ``
  8. public class RoundedTransformation implements com.squareup.picasso.Transformation {
  9. private final int radius;
  10. private final int margin;
  11. public RoundedTransformation(final int radius, final int margin) {
  12. this.radius = radius;
  13. this.margin = margin;
  14. }
  15. @Override
  16. public Bitmap transform(final Bitmap source) {
  17. final Paint paint = new Paint();
  18. paint.setAntiAlias(true);
  19. paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
  20. Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
  21. Canvas canvas = new Canvas(output);
  22. canvas.drawRoundRect(new RectF(margin, margin, source.getWidth() - margin, source.getHeight() - margin), radius, radius, paint);
  23. if (source != output) {
  24. source.recycle();
  25. }
  26. return output;
  27. }
  28. ``
  29. @Override
  30. public String key() {
  31. return "rounded";
  32. }
  33. }

多解释也没啥好解释的,拿去用就好了希望可以帮到你

你可能感兴趣的:(两个Picasso的Transformation)