2016. 6. 23. 11:04ㆍ개발일기/스타일판다
Glide 사용 중에 문제가 발생되서 결국에는 해결 방법을 못찾고 picasso를 사용하기로 결정..
기존 Glide 라이브러리와 설정을 다 걷어내고, picasso를 적용했다.
아래와 같이 gradle 추가.
dependencies {
....
compile 'com.squareup.picasso:picasso:2.5.2'
}
ProGuard 추가
-dontwarn com.squareup.okhttp.**
사용법은 Glide 만큼이나 간단하다.
Picasso.with(getContext()).load("image url").
error(R.mipmap.no_photo).
resize(400, 400).
centerInside().
into(imageView);
기존에 Glide에서 구현했었던 Circle Image를 picasso에서도 구현했어야 했기 때문에, CircleTransform이란 class를 새로 생성하였다.
public class CircleTransform implements Transformation { | |
@Override | |
public Bitmap transform(Bitmap source) { | |
int size = Math.min(source.getWidth(), source.getHeight()); | |
int x = (source.getWidth() - size) / 2; | |
int y = (source.getHeight() - size) / 2; | |
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); | |
if(squaredBitmap != source){ | |
source.recycle(); | |
} | |
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); | |
Canvas canvas = new Canvas(bitmap); | |
Paint paint = new Paint(); | |
BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); | |
paint.setShader(shader); | |
paint.setAntiAlias(true); | |
float r = size / 2f; | |
canvas.drawCircle(r, r, r, paint); | |
squaredBitmap.recycle(); | |
return bitmap; | |
} | |
@Override | |
public String key() { | |
return "circle"; | |
} | |
} |
그리고 아래와 같이 호출하면 이미지가 원으로 crop 되서 생성이 된다.
CircleTransform transForm = new CircleTransform();
Picasso.with(this).load("image url").transform(transForm).into(imageView);