Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

에이치의 모바일 앱 개발

View-ImageView-Picasso-blur 본문

Android/Android 개발 소스

View-ImageView-Picasso-blur

로이누리 2019. 2. 20. 13:47

블러 설정.

Picasso 는 공식 홈페이지를 참조할 것을 권합니다.

아래는 피카소 사용시 Blur 효과를 넣는 방법입니다.

Radius 값은 최대 25까지 적용 가능하니 참조 바랍니다.

Picasso.with(getContext())
.load(IMG_URL)
.transform(new BlurTransformation(getContext(), 25))
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
Bitmap bitmap = Bitmap.createScaledBitmap(bitmap, binding.img.getWidth(), binding.img.getHeight(), false);
Drawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);
binding.img.setBackground(drawable);
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
});


public class BlurTransformation implements Transformation {
private static int MAX_RADIUS = 25; //Set the radius of the Blur. Supported range 0 < radius <= 25
private static int DEFAULT_DOWN_SAMPLING = 1;

private Context context;

private int radius;
private int sampling;

public BlurTransformation(Context context) {
this(context, MAX_RADIUS);
}

public BlurTransformation(Context context, int radius) {
this(context, radius, DEFAULT_DOWN_SAMPLING);
}

public BlurTransformation(Context context, int radius, int sampling) {
this.context = context.getApplicationContext();
this.radius = radius;
if (this.radius > MAX_RADIUS) {
this.radius = MAX_RADIUS;
} else if (this.radius <= 0) {
this.radius = 1;
}
this.sampling = sampling;
}


@Override
public Bitmap transform(Bitmap source) {
PFLog.e(source.toString());
int scaledWidth = source.getWidth() / sampling;
int scaledHeight = source.getHeight() / sampling;
Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
canvas.scale(1 / (float) sampling, 1 / (float) sampling);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(source, 0, 0, paint);
bitmap = blur(context, bitmap, radius);
source.recycle();
return bitmap;
}

@Override
public String key() {
return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
}

public Bitmap blur(Context context, Bitmap targetBitmap, int radius) throws RSRuntimeException {
RenderScript renderScript = null;
try {
renderScript = RenderScript.create(context);
Allocation input = Allocation.createFromBitmap(renderScript, targetBitmap,
Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
Allocation output = Allocation.createTyped(renderScript, input.getType());
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));

blur.setInput(input);
blur.setRadius((radius > MAX_RADIUS ? MAX_RADIUS : radius));
blur.forEach(output);
output.copyTo(targetBitmap);

} finally {
if (renderScript != null) {
renderScript.destroy();
}
}
return targetBitmap;
}
}


'Android > Android 개발 소스' 카테고리의 다른 글

Android Device Screen zoom 획득  (2) 2019.03.07
View-RecyclerView  (0) 2019.02.20
View-ImageView-Gradient  (0) 2019.02.20
Battery-Awake, Sleep  (0) 2019.02.20
View-TextView-link  (0) 2019.02.18
Comments