OutOfMemory
Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a
16777228 byte allocation with 4194304 free bytes and 4MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
at android.graphics.Bitmap.nativeCreate(Bitmap.java)
at android.graphics.Bitmap.createBitmap(Bitmap.java:977)
at android.graphics.Bitmap.createBitmap(Bitmap.java:948)
at android.graphics.Bitmap.createBitmap(Bitmap.java:915)
Slide 13
Slide 13 text
Bitmap too large
Fatal Exception: java.lang.RuntimeException: Canvas: trying to
draw too large(283435200bytes) bitmap.
at
android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas
.java:260)
at android.graphics.Canvas.drawBitmap(Canvas.java:1373)
Slide 14
Slide 14 text
You need to scale your images
Height = 1920px
Width = 700px
1920px * 700px
= 1 344 000 * 4 bytes per pixel
= 5 376 000 bytes
= 5.375 mb
Height = 128px
Width = 50px
= 0.0256mb
Slide 15
Slide 15 text
How do I scale down my image?
val options = BitmapFactory.Options()
options.inSampleSize = calculateInSampleSize(options,
reqWidth, reqHeight)
BitmapFactory.decodeResource(res, resId, options)
setPixel()
override fun filter(bitmap: Bitmap): Bitmap {
val newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
for (i in 0 until bitmap.width){
for (j in 0 until bitmap.height){
newBitmap.setPixel(i, j, Color.BLUE)
}
}
return newBitmap
}
Slide 23
Slide 23 text
Pros %
• Easy to understand
• Manipulate per
Pixel manually
Cons &
• Suuuuuper Slow
• CPU Execution
• No parallel
execution
Slide 24
Slide 24 text
ColorFilter
Slide 25
Slide 25 text
What is a Color Matrix Filter?
Modifies the color of each pixel
drawn with a certain Paint.
Slide 26
Slide 26 text
ColorMatrixColorFilter
4 * 5 matrix applied to each pixel
a, b, c, d, e,
f, g, h, i, j,
k, l, m, n, o,
p, q, r, s, t
Slide 27
Slide 27 text
ColorMatrix
4 * 5 matrix applied to each pixel
R’ = a*R + b*G + c*B + d*A + e;
G’ = f*R + g*G + h*B + i*A + j;
B’ = k*R + l*G + m*B + n*A + o;
A’ = p*R + q*G + r*B + s*A + t;
a, b, c, d, e,
f, g, h, i, j,
k, l, m, n, o,
p, q, r, s, t
[R’, G’, B’, A’]
Slide 28
Slide 28 text
ColorMatrix
Identity Matrix - same image in as out
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
R,
G,
B,
A
* =
R,
G,
B,
A
What else can I do with it?
Warmth, Exposure, Saturation, Contrast, Grayscale, Sepia
Slide 32
Slide 32 text
Other ColorFilters
- LightingColorFilter
- PorterDuffColorFilter
More info: Manipulating images and Drawables
with Android’s ColorFilter - Nick Rout
bit.ly/2Cwvx8t
Slide 33
Slide 33 text
Pros %
• Easy-ish to
understand
• Fast (GPU)
• Write in Kotlin/
Java
• Understand the
exact effect
being applied
Cons &
• Can’t do all
effects like
Sharpen, Unsharp
mask etc
Slide 34
Slide 34 text
ImageFilterView
Slide 35
Slide 35 text
ImageFilterView
Part of ConstraintLayout 2.0
https://developer.android.com/reference/android/support/constraint/utils/ImageFilterView
Slide 36
Slide 36 text
ImageFilterView
Slide 37
Slide 37 text
ImageFilterView
Slide 38
Slide 38 text
Pros %
• Easy to understand
• Fast (GPU)
• Nice API
• Java/Kotlin
Cons &
• Can’t do everything
• Values are
abstracted away
from you
Pros %
• Easy to understand
• Fast
• Nice API
• Java/Kotlin
Cons &
• Can’t do everything
• Values are
abstracted away
from you
• Dependant on Third
Party
Pros %
• Easy to understand
• Fast
• Nice API
• Java/Kotlin
Cons &
• Can’t do everything
• Values are
abstracted away
from you
• Dependant on Third
Party
• Dependent on OpenGL
code
Slide 45
Slide 45 text
Renderscript
Slide 46
Slide 46 text
What is Renderscript?
• Framework on Android for computationally
expensive tasks
• Parallelises tasks across CPU and GPU
• Language: C99-derived language for writing
high-performance compute code
Slide 47
Slide 47 text
Renderscript Compute vs Graphics
• Graphics - Deprecated
• Compute - still widely used for image
processing
Pros %
• Fast
• Highly
customisable
• Parallel execution
on GPU
• Interactions
between layers
isn’t complex
Cons &
• Need to understand
a new language
• Manage your own
memory
• Native Libs
Slide 68
Slide 68 text
What should I use?
Slide 69
Slide 69 text
How do I choose?
ColorFilter / Renderscript /
OpenGL?
+
ImageFilterView or
Glide Transformations
Slide 70
Slide 70 text
Final Tips
Slide 71
Slide 71 text
Tips
OutOfMemory
↕
Scale to display
Use what is
easiest for you