Skip to content

Instantly share code, notes, and snippets.

@ericleong
ericleong / SaveAndParseResponse.java
Created January 5, 2016 21:00
Method to simultaneously save and parse the response from an OkHttp ResponseBody.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> Pair<String, T> saveAndParseResponse(final ResponseBody response, final TypeReference type)
throws IOException {
final Buffer sink = new Buffer();
final StringBuilder builder = new StringBuilder();
final Charset charset = responseCharset(response);
response.source().readAll(new ForwardingSink(sink) {
@ericleong
ericleong / ImageUtil.java
Created October 26, 2015 04:17
Render a list of drawables as layers on a canvas, with support for different blend modes.
package com.eleong.giferator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
@ericleong
ericleong / fade_fragment_shader.glsl
Created February 26, 2015 05:02
Fade Effect Fragment Shader
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
void main() {
vec4 color = texture2D(sTexture, vTextureCoord);
float r = 0.2 + 0.8 * color.r;
float g = 0.2 + 0.8 * color.g;
float b = 0.2 + 0.8 * color.b;
@ericleong
ericleong / map_arguments_with_default.py
Created April 18, 2014 18:41
Map Constructor Arguments to Object Fields
class Thing:
def __init__(self, a=1, b='two'):
# Iterate through local variables and assign as fields
for name, value in locals().items():
if name != 'self':
setattr(self, name, value)
@ericleong
ericleong / map_arguments.py
Created April 18, 2014 17:31
Map Constructor Keyword Arguments to Object Fields
class Thing:
def __init__(self, **kwargs):
# Set fields according to their name in the arguments
for name, value in kwargs.items():
setattr(self, name, value)