๊ฐœ๋ฐœ/Android

[Android] Bitmap์— ์ƒ‰์ƒ ์ž…ํžˆ๊ธฐ

๋„๋ฆฌ ๐ŸŸ 2021. 4. 8. 22:54

์ž‘์„ฑ์ผ: 2017.12.10


๋น„ํŠธ๋งต์— ์ƒ‰์ƒ์„ ๋ฎ์–ด ์”Œ์šฐ๋Š” ๋ฐฉ๋ฒ•
-> ๊ฐ™์€ ์ด๋ฏธ์ง€๋ฅผ ์—ฌ๋Ÿฌ ์ƒ‰์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค. 

 


์˜ˆ์‹œ๋Š”, ํฐ์ƒ‰ ์•„์ด์ฝ˜์„ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ. (์•„์ด์ฝ˜์ด ํฐ์ƒ‰์ด๋ผ ์ž„์‹œ๋กœ ๋ฐฐ๊ฒฝ์ƒ‰์„ ์คŒ.)

//1. Drawable -> Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.email_xxl).copy(Bitmap.Config.ARGB_8888, true);

//2. ColorFilter ๋งŒ๋“ค๊ธฐ
Paint paint = new Paint();
ColorFilter colorFilter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP); //(color, mode)
paint.setColorFilter(colorFilter); 

//3. ColorFilter ์ ์šฉํ•˜๊ธฐ
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, 0, paint);

 

1. Drawable -> Bitmap

๊ฐ€์ง€๊ณ  ์žˆ๋Š” Drawable์„ Bitmap์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•
๊ทธ๋ƒฅ decode~() ๋ฉ”์†Œ๋“œ๋งŒ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ  Immutable bitmap passed to Canvas constructor ์˜ค๋ฅ˜๊ฐ€ ๋‚  ์ˆ˜ ์žˆ์œผ๋‹ˆ
์ด ๊ฒฝ์šฐ, copy() ๋ฅผ ํ†ตํ•ด ๋ณต์‚ฌํ•˜์—ฌ ์ €์žฅํ•˜๋ฉด ๋œ๋‹ค.

2. ColorFilter ๋งŒ๋“ค๊ธฐ

Bitmap์— ์ ์šฉํ•  ColorFilter๋ฅผ ๋งŒ๋“ ๋‹ค.
์˜ˆ์‹œ๋Š” ๋‹คํฌ๊ทธ๋ ˆ์ด ์ƒ‰์ƒ์„ ์ ์šฉํ•˜์˜€๊ณ , PorterDuff.Mode.SRC_ATOP๋Š” source๋ฅผ ์œ„์— ๋ฎ๊ฒ ๋‹ค ๋ผ๋Š” ์˜๋ฏธ.
(์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ Destination์€ Bitmap / Source๋Š” ColorFilter๊ฐ€ ๋œ๋‹ค.)

์ฐธ๊ณ : https://developer.android.com/reference/android/graphics/PorterDuff.Mode.html

3. ColorFilter ์ ์šฉํ•˜๊ธฐ

๋งˆ์ง€๋ง‰์œผ๋กœ ์œ„์—์„œ ๋งŒ๋“  ColorFilter๋ฅผ Bitmap์— ์ ์šฉํ•˜๋ฉด ์™„๋ฃŒ!

์ด๋ ‡๊ฒŒ ํฐ์ƒ‰ ์•„์ด์ฝ˜์— ์ปฌ๋Ÿฌ ํ•„ํ„ฐ๊ฐ€ ์ž…ํ˜€์„œ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.