반응형
이미지에 그림자를 넣으려면...
배경이미지를 깔아주면 되는데 이건 영역 자체에 깔리는 거니까 모양과 상관이 없다.
이미지 모양대로 그림자를 깔아주려면 뭘 좀 해줘야겠지.
아래 함수를 사용하면 된다.
public Bitmap addShadow(final Bitmap bm, final int dstHeight, final int dstWidth, int size, float dx, float dy, int color, int alpha) { final Bitmap mask = Bitmap.createBitmap(dstWidth, dstHeight, Config.ALPHA_8); final Matrix scaleToFit = new Matrix(); final RectF src = new RectF(0, 0, bm.getWidth(), bm.getHeight()); final RectF dst = new RectF(0, 0, dstWidth - dx, dstHeight - dy); scaleToFit.setRectToRect(src, dst, ScaleToFit.CENTER); final Matrix dropShadow = new Matrix(scaleToFit); dropShadow.postTranslate(dx, dy); final Canvas maskCanvas = new Canvas(mask); final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); maskCanvas.drawBitmap(bm, scaleToFit, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); maskCanvas.drawBitmap(bm, dropShadow, paint); final BlurMaskFilter filter = new BlurMaskFilter(size, Blur.SOLID); paint.reset(); paint.setAntiAlias(true); paint.setColor(color); paint.setAlpha(alpha); paint.setMaskFilter(filter); paint.setFilterBitmap(true); final Bitmap ret = Bitmap.createBitmap(dstWidth, dstHeight, Config.ARGB_8888); final Canvas retCanvas = new Canvas(ret); retCanvas.drawBitmap(mask, 0, 0, paint); retCanvas.drawBitmap(bm, scaleToFit, null); mask.recycle(); return ret; }
원본 이미지와 여러 값들을 넣으면 그림자를 넣은 Bitmap 을 반환하는 함수다.
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Bitmap bmp2 = addShadow(bmp, bmp.getHeight(), bmp.getWidth(), 2, 3, 3, Color.RED, 80); iv.setImageBitmap(bmp2);
이런식으로 호출해주면 아래와 같이 나온다.
반응형
'안드로이드' 카테고리의 다른 글
[Android] 윈도우 adb 맛이 갔을때 (1) | 2015.04.17 |
---|---|
[Android] Shaders and Filters 쉐이더와 필터 (0) | 2015.04.16 |
[Android] 통화설정 관련 사항 메모 (0) | 2015.04.08 |
[Android] 여러가지 색깔의 둥근 사각형 그리기 (0) | 2015.03.24 |
(Android) dp to px, px to dp (0) | 2015.03.16 |