티스토리 뷰

반응형
[Android] 

Picasso 블러 - Blur 적용하기 ( Transform )

android Blur effect example Using the image loader library Picasso

 

 

   이미지 로드 라이브러리 Picasso를 안드로이드에 적용하기

 

아래의 포스팅에서 라이브러리 적용하는 방법과 간단히 사용하는 방법이 있다.

 

 [Android] 이미지 로드 라이브러리 Picasso 사용하기 참조

 

 

 

 

 

 

   Android  Renderscript 라이브러리를  Blur 사용하기

 

 

 1.  Renderscript 사용 제한

▷ Android SDK Tools revision 22.2 or higher -- SDK툴 버전이 22.2 버전이상 사용하고

 

▷ Android SDK Build-tools revision 18.1.0 or higher  -- 안드로이드 SDK 빌드 툴 버전은 18.1.0 버전이상 사용한다면 Renderscript를 사용할 수 있다.

 

 

 2.  Build.gradle 설정

 

아래는 예제로 작성한 안드로이드 앱의 build.gradle 파일이다.

Blur 를 사용하기 위해 Renderscript 옵션을  추가하였다..

빨간색 글씨를 추가하면 될 것이다.

 

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
32
apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
 
    defaultConfig {
        applicationId "com.packagename"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
 
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
}
 
cs

 

 

 

 

 3.  RenderScript의 Blur 작성하기 

 RenderScriptBlur.java 

아래는 Bitmap 파일을 Blur 처리 할 수 있기 때문에 Picasso적용만을 위한 것은 아니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class RenderScriptBlur {
    public static 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 > 25 ? 25 : radius));
            blur.forEach(output);
            output.copyTo(targetBitmap);
 
        } finally {
            if (renderScript != null) {
                renderScript.destroy();
            }
        }
        return targetBitmap;
    }
}
 
cs

 

 

 

 4.  Picasso의 Transformation Blur 작성하기 

BlurTransformation.java

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
 
public class BlurTransformation implements Transformation {
 
    private static int MAX_RADIUS = 25;
    private static int DEFAULT_DOWN_SAMPLING = 1;
 
    private Context mContext;
 
    private int mRadius;
    private int mSampling;
 
    public BlurTransformation(Context context) {
        this(context, MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
    }
 
    public BlurTransformation(Context context, int radius) {
        this(context, radius, DEFAULT_DOWN_SAMPLING);
    }
 
    public BlurTransformation(Context context, int radius, int sampling) {
        mContext = context.getApplicationContext();
        mRadius = radius;
        if (mRadius > 25) {
            mRadius = 25;
        } else if (mRadius <= 0) {
            mRadius = 1;
        }
        mSampling = sampling;
    }
 
 
    @Override
    public Bitmap transform(Bitmap source) {
        int scaledWidth = source.getWidth() / mSampling;
        int scaledHeight = source.getHeight() / mSampling;
 
        Bitmap bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
 
        Canvas canvas = new Canvas(bitmap);
        canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(source, 00, paint);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            try {
                bitmap = RenderScriptBlur.blur(mContext, bitmap, mRadius);
            } catch (RSRuntimeException e) {
 
            }
        } else {
 
        }
        source.recycle();
        return bitmap;
    }
 
    @Override
    public String key() {
        return "BlurTransformation(radius=" + mRadius + ", sampling=" + mSampling + ")";
    }
}
 
cs

 

 

 

   Picasso 적용하기

 

1
2
3
4
5
        Picasso.with(this)
                .load("http://kaboompics.com/files/upload/o_19v85jk82r7a1ibk1b1r189j1atq7_new.jpg")
                .transform(new BlurTransformation(this25))
                .into(picassoImageView);
 
cs

 

   결과화면

 

>> 블러 효과 없을때

 

>> 블러 Radius 25

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함