Skip to content

Instantly share code, notes, and snippets.

@Turnsole
Last active August 29, 2015 14:23
Show Gist options
  • Save Turnsole/7dcc7e10673d58c07bb3 to your computer and use it in GitHub Desktop.
Save Turnsole/7dcc7e10673d58c07bb3 to your computer and use it in GitHub Desktop.
Porter and Duff Demonstration
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
public class DummyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.porter_duff_demo);
int srcColor = Color.CYAN;
ViewGroup parent = (ViewGroup) findViewById(R.id.cat_holder);
tintImage(srcColor, PorterDuff.Mode.ADD, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.CLEAR, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.DARKEN, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.DST, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.DST_ATOP, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.DST_IN, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.DST_OUT, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.DST_OVER, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.LIGHTEN, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.OVERLAY, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.MULTIPLY, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.SCREEN, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.SRC, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.SRC_ATOP, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.SRC_IN, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.SRC_OUT, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.SRC_OVER, inflateCat(parent));
tintImage(srcColor, PorterDuff.Mode.XOR, inflateCat(parent));
}
private ImageView inflateCat(ViewGroup parent) {
LayoutInflater.from(this).inflate(R.layout.cat_holder, parent);
return (ImageView) parent.getChildAt(parent.getChildCount() - 1);
}
private void tintImage(int color, PorterDuff.Mode mode, ImageView imageView) {
PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(color, mode);
imageView.setColorFilter(porterDuffColorFilter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment