Created
May 9, 2016 12:55
-
-
Save ksu3101/d5fc3ec100d9fe5c6357b5d8bee955b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 비동기로 비트맵 이미지에 Blur효과를 설정하고 필요한 후 처리들을 한다. | |
* | |
* @author KangSung-Woo | |
*/ | |
public class AsyncBlurTask | |
extends AsyncTask<Bitmap, Integer, Bitmap> { | |
// 이미지 리사이징 할 최소 사이즈 | |
public static final int DEFAULT_SIZE_PX = 30; | |
private static final String TAG = AsyncBlurTask.class.getSimpleName(); | |
private final boolean DEBUG = false; | |
private final WeakReference<ImageView> imageViewRef; | |
private Context context; | |
private int uniqueId = 0; | |
private int blurRadius = 0; | |
private int brightness = 0; | |
private int resizeMax = DEFAULT_SIZE_PX; | |
private boolean isTransitionEffectEnabled = false; | |
private int transitionEffectDuration = 200; | |
private OnAsyncBlurTaskCompleted listener; | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param blurRadius blur 효과의 radius value. | |
*/ | |
public AsyncBlurTask(Context context, int blurRadius) { | |
this(context, null, null, blurRadius, 0); | |
} | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param targetIv 후처리 된 비트맵 이미지가 적용될 ImageView. | |
* @param blurRadius blur 효과의 radius value. | |
*/ | |
public AsyncBlurTask(Context context, ImageView targetIv, int blurRadius) { | |
this(context, targetIv, null, blurRadius, 0); | |
} | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param targetIv 후처리 된 비트맵 이미지가 적용될 ImageView. | |
* @param blurRadius blur 효과의 radius value. | |
* @param brightness 이미지 밝기 효과의 brightness value. (-255 ~ 255) | |
*/ | |
public AsyncBlurTask(Context context, ImageView targetIv, int blurRadius, int brightness) { | |
this(context, targetIv, null, blurRadius, brightness); | |
} | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param listener 비트맵 이미지의 후처리에 대한 Callback internface를 구현한 객체. | |
* @param blurRadius blur 효과의 radius value. | |
*/ | |
public AsyncBlurTask(Context context, OnAsyncBlurTaskCompleted listener, int blurRadius) { | |
this(context, null, listener, blurRadius, 0); | |
} | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param targetIv 후처리 된 비트맵 이미지가 적용될 ImageView. | |
* @param listener 비트맵 이미지의 후처리에 대한 Callback internface를 구현한 객체. | |
* @param blurRadius blur 효과의 radius value. | |
* @param brightness 이미지 밝기 효과의 brightness value. (-255 ~ 255) | |
*/ | |
public AsyncBlurTask(Context context, ImageView targetIv, OnAsyncBlurTaskCompleted listener, int blurRadius, int brightness) { | |
this(context, targetIv, listener, blurRadius, brightness, false, 0, DEFAULT_SIZE_PX); | |
} | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param targetIv 후처리 된 비트맵 이미지가 적용될 ImageView. | |
* @param blurRadius blur 효과의 radius value. | |
* @param isTransitionEnable 후처리 후 이미지를 ImageView에 세팅 할 시 Cross fade transition 효과를 적용 할 것인지에 대한 여부. | |
* @param transitionDuration Cross fade transition효과 적용 시 transition animation의 duration. | |
*/ | |
public AsyncBlurTask(Context context, | |
ImageView targetIv, | |
int blurRadius, | |
boolean isTransitionEnable, | |
int transitionDuration) { | |
this(context, targetIv, null, blurRadius, 0, isTransitionEnable, transitionDuration, DEFAULT_SIZE_PX); | |
} | |
/** | |
* 생성자 | |
* | |
* @param context Context | |
* @param targetIv 후처리 된 비트맵 이미지가 적용될 ImageView. | |
* @param listener 비트맵 이미지의 후처리에 대한 Callback internface를 구현한 객체. | |
* @param blurRadius blur 효과의 radius value. (1 ~ 25) | |
* @param brightness 이미지 밝기 효과의 brightness value. (-255 ~ 255) | |
* @param isTransitionEnable 후처리 후 이미지를 ImageView에 세팅 할 시 Cross fade transition 효과를 적용 할 것인지에 대한 여부. | |
* @param transitionDuration Cross fade transition효과 적용 시 transition animation의 duration. | |
* @param maxSize 이미지를 후 처리 하기전에 리사이징 할 size value. DEFAULT_SIZE_PX보다 낮을 경우 리사이즈 하지 않는다. | |
*/ | |
public AsyncBlurTask(Context context, | |
ImageView targetIv, | |
OnAsyncBlurTaskCompleted listener, | |
int blurRadius, | |
int brightness, | |
boolean isTransitionEnable, | |
int transitionDuration, | |
int maxSize) { | |
this.context = context; | |
this.listener = listener; | |
this.blurRadius = blurRadius; | |
this.brightness = brightness; | |
this.isTransitionEffectEnabled = isTransitionEnable; | |
this.transitionEffectDuration = transitionDuration; | |
this.resizeMax = maxSize; | |
imageViewRef = new WeakReference<ImageView>(targetIv); | |
} | |
public void setImagesTransitionEffectEnabled(boolean enable) { | |
this.isTransitionEffectEnabled = enable; | |
} | |
public void setImageTransitionEffectDuration(int duration) { | |
transitionEffectDuration = duration; | |
} | |
public boolean isImageTransitionEffectEnable() { | |
return isTransitionEffectEnabled; | |
} | |
/** | |
* 이미지 처리 후 콜백 인터페이스 리스너를 설정 한다. | |
* | |
* @param listner 콜백 인터페이스를 구현한 객체. | |
*/ | |
public void setOnAsyncBlurTaskCompleteListener(OnAsyncBlurTaskCompleted listner) { | |
this.listener = listner; | |
} | |
/** | |
* 이미지의 Blur효과에 들어가는 radius value를 얻는다. | |
* | |
* @return Blur effect radius value. | |
*/ | |
public int getBlurRadius() { | |
return blurRadius; | |
} | |
/** | |
* 이미지에 Blur효과를 주게 될 경우 blur효과의 radius value를 설정 한다. | |
* | |
* @param blurRadius Blur effect radius value. | |
*/ | |
public void setBlurRadius(int blurRadius) { | |
this.blurRadius = blurRadius; | |
} | |
public int getResizeValue() { | |
return resizeMax; | |
} | |
public void setResizeValue(int resizeMax) { | |
this.resizeMax = resizeMax; | |
} | |
@Override | |
protected void onPreExecute() { | |
} | |
@Override | |
protected Bitmap doInBackground(Bitmap... params) { | |
Bitmap result = params[0]; | |
if (result != null) { | |
// 이미지 리사이즈 | |
if (resizeMax >= DEFAULT_SIZE_PX) { | |
result = Utils.getResizeImg(result, resizeMax); | |
} | |
// 이미지의 채널 여부 파악 및 ARGB_8888(PNG) 전환. | |
if (result.getConfig() != Bitmap.Config.ARGB_8888) { | |
result = Utils.convertRGB565toARGB8888(result); | |
} | |
// blur 효과 적용. | |
if (blurRadius > 0) { | |
result = Utils.getBlurImage(result, blurRadius); | |
} | |
// brightness 변경. | |
if (brightness != 0) { | |
result = Utils.setImageBrightness(result, brightness); | |
} | |
} | |
return result; | |
} | |
@Override | |
protected void onPostExecute(Bitmap bitmap) { | |
if (listener != null) { | |
listener.onAsyncBlurredImageTaskCompleted(bitmap != null, bitmap); | |
} | |
if (bitmap != null && imageViewRef != null) { | |
if (isTransitionEffectEnabled) { | |
final ImageView iv = imageViewRef.get(); | |
iv.clearAnimation(); | |
final Drawable drawable = iv.getDrawable(); | |
final TransitionDrawable transitionDrawable = Utils.createTransitionDrawable( | |
drawable == null ? | |
new ColorDrawable(context.getResources() | |
.getColor(android.R.color.black)) : drawable, | |
new BitmapDrawable(bitmap)); | |
if (transitionDrawable != null) { | |
iv.setImageDrawable(transitionDrawable); | |
transitionDrawable.startTransition(transitionEffectDuration); | |
} | |
else { | |
setImageBitmap(bitmap); | |
} | |
} | |
else { | |
setImageBitmap(bitmap); | |
} | |
} | |
} | |
private void setImageBitmap(Bitmap bmp) { | |
final ImageView iv = imageViewRef.get(); | |
if (iv != null) { | |
iv.setImageBitmap(bmp); | |
} | |
} | |
/** | |
* 비동기 비트맵 이미지 처리 후 콜백 인터페이스. | |
*/ | |
public interface OnAsyncBlurTaskCompleted { | |
/** | |
* 비동기 비트맵 이미지 처리 후 콜백 인터페이스. | |
* | |
* @param isSuccess 작업 성공 여부. | |
* @param resultBmp 결과물 Bitmap instance. | |
*/ | |
void onAsyncBlurredImageTaskCompleted(boolean isSuccess, Bitmap resultBmp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment