Skip to content

Commit 4330045

Browse files
author
Paul Ruiz
committed
Added Picasso Color Transformation Example
1 parent 0e67d61 commit 4330045

File tree

29 files changed

+635
-0
lines changed

29 files changed

+635
-0
lines changed

IconColorization/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gradle
2+
/local.properties
3+
/.idea/workspace.xml
4+
/.idea/libraries
5+
.DS_Store
6+
/build

IconColorization/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

IconColorization/app/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 20
5+
buildToolsVersion "20.0.0"
6+
7+
defaultConfig {
8+
applicationId "com.ptrprograms.iconcolorization"
9+
minSdkVersion 9
10+
targetSdkVersion 20
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
runProguard false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:appcompat-v7:20.0.0'
25+
compile 'com.squareup.picasso:picasso:2.3.3'
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Applications/Android Studio.app/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ptrprograms.iconcolorization;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.ptrprograms.iconcolorization" >
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@drawable/ic_launcher"
10+
android:label="@string/app_name"
11+
android:theme="@style/AppTheme" >
12+
<activity
13+
android:name=".activity.MainActivity"
14+
android:label="@string/app_name" >
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.ptrprograms.iconcolorization.activity;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.drawable.BitmapDrawable;
5+
import android.graphics.drawable.Drawable;
6+
import android.os.Build;
7+
import android.support.v7.app.ActionBarActivity;
8+
import android.os.Bundle;
9+
10+
import android.widget.ImageView;
11+
12+
import com.ptrprograms.iconcolorization.R;
13+
import com.ptrprograms.iconcolorization.utility.ColorTransformation;
14+
import com.squareup.picasso.Picasso;
15+
import com.squareup.picasso.Target;
16+
17+
18+
public class MainActivity extends ActionBarActivity {
19+
20+
private ImageView mDrawableTransformedImage;
21+
private ImageView mDrawableUrlTransformedImage;
22+
private ImageView mDrawableUrlImage;
23+
24+
private Target ActionBarIconTarget = new Target()
25+
{
26+
@Override
27+
public void onBitmapLoaded( Bitmap bitmap, Picasso.LoadedFrom from )
28+
{
29+
getSupportActionBar().setIcon( new BitmapDrawable( getResources(), bitmap ) );
30+
}
31+
32+
@Override
33+
public void onBitmapFailed( Drawable errorDrawable )
34+
{
35+
getSupportActionBar().setIcon( R.drawable.ic_launcher );
36+
}
37+
38+
@Override
39+
public void onPrepareLoad( Drawable placeHolderDrawable )
40+
{
41+
42+
}
43+
};
44+
45+
@Override
46+
protected void onCreate(Bundle savedInstanceState) {
47+
super.onCreate(savedInstanceState);
48+
setContentView(R.layout.activity_main);
49+
50+
mDrawableTransformedImage = (ImageView) findViewById( R.id.transformed_drawable );
51+
mDrawableUrlImage = (ImageView) findViewById( R.id.url_drawable );
52+
mDrawableUrlTransformedImage = (ImageView) findViewById( R.id.transformed_url_drawable );
53+
54+
Picasso.with( this ).load( R.drawable.ic_star ).transform( new ColorTransformation(getResources().getColor( R.color.local_drawable_color ) ) ).into( mDrawableTransformedImage );
55+
Picasso.with( this ).load( getString( R.string.image_url ) ).into( mDrawableUrlImage );
56+
Picasso.with( this ).load( getString( R.string.image_url ) ).transform( new ColorTransformation( getResources().getColor( R.color.remote_image_color ) ) ).into( mDrawableUrlTransformedImage );
57+
58+
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
59+
Picasso.with( this ).load( R.drawable.ic_star ).transform( new ColorTransformation( getResources().getColor( R.color.action_bar_icon_color ) ) ).into(ActionBarIconTarget);
60+
}
61+
62+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.ptrprograms.iconcolorization.utility;
2+
3+
import android.content.Context;
4+
import android.content.res.Resources;
5+
import android.graphics.Bitmap;
6+
import android.graphics.Canvas;
7+
import android.graphics.PorterDuff;
8+
import android.graphics.drawable.BitmapDrawable;
9+
import com.squareup.picasso.Transformation;
10+
11+
/**
12+
* Created by paulruiz on 8/22/14.
13+
*/
14+
public class ColorTransformation implements Transformation {
15+
16+
private int color = 0;
17+
18+
public ColorTransformation( int color ) {
19+
setColor( color );
20+
}
21+
22+
public void setColor( int color ) {
23+
this.color = color;
24+
}
25+
26+
public void setColorFromRes( Context context, int colorResId ) {
27+
setColor( context.getResources().getColor( colorResId ) );
28+
}
29+
30+
public int getColor() {
31+
return color;
32+
}
33+
34+
@Override
35+
public Bitmap transform(Bitmap source) {
36+
if( color == 0 ) {
37+
return source;
38+
}
39+
40+
BitmapDrawable drawable = new BitmapDrawable(Resources.getSystem(), source );
41+
Bitmap result = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888 );
42+
Canvas canvas = new Canvas( result );
43+
drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight() );
44+
drawable.setColorFilter( color, PorterDuff.Mode.SRC_IN );
45+
drawable.draw(canvas);
46+
drawable.setColorFilter(null);
47+
drawable.setCallback(null);
48+
49+
if( result != source ) {
50+
source.recycle();
51+
}
52+
53+
return result;
54+
}
55+
56+
@Override
57+
public String key() {
58+
return "DrawableColor:" + color;
59+
}
60+
}
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingLeft="@dimen/activity_horizontal_margin"
6+
android:paddingRight="@dimen/activity_horizontal_margin"
7+
android:paddingTop="@dimen/activity_vertical_margin"
8+
android:paddingBottom="@dimen/activity_vertical_margin"
9+
tools:context=".MainActivity">
10+
11+
<LinearLayout
12+
android:id="@+id/row_1"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content">
15+
<TextView
16+
android:text="@string/label_normal_drawable"
17+
android:layout_width="0dp"
18+
android:layout_height="wrap_content"
19+
android:layout_gravity="center"
20+
android:layout_weight="1"/>
21+
22+
<ImageView
23+
android:id="@+id/normal_drawable"
24+
android:layout_width="0dp"
25+
android:layout_height="wrap_content"
26+
android:src="@drawable/ic_star"
27+
android:gravity="center"
28+
android:layout_weight="1"/>
29+
</LinearLayout>
30+
31+
<LinearLayout
32+
android:id="@+id/row_2"
33+
android:layout_below="@+id/row_1"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content">
36+
<TextView
37+
android:text="@string/label_transformed_drawable"
38+
android:layout_width="0dp"
39+
android:layout_height="wrap_content"
40+
android:layout_weight="1"
41+
android:layout_gravity="center"/>
42+
43+
<ImageView
44+
android:id="@+id/transformed_drawable"
45+
android:layout_width="0dp"
46+
android:layout_height="wrap_content"
47+
android:layout_weight="1"
48+
android:gravity="center"/>
49+
</LinearLayout>
50+
51+
<LinearLayout
52+
android:id="@+id/row_3"
53+
android:layout_below="@+id/row_2"
54+
android:layout_width="match_parent"
55+
android:layout_height="wrap_content">
56+
<TextView
57+
android:text="@string/label_normal_url_drawable"
58+
android:layout_width="0dp"
59+
android:layout_height="wrap_content"
60+
android:layout_weight="1"
61+
android:layout_gravity="center"/>
62+
63+
<ImageView
64+
android:id="@+id/url_drawable"
65+
android:layout_width="0dp"
66+
android:layout_height="wrap_content"
67+
android:layout_weight="1"
68+
android:layout_gravity="center"
69+
android:gravity="center"/>
70+
</LinearLayout>
71+
72+
<LinearLayout
73+
android:id="@+id/row_4"
74+
android:layout_below="@+id/row_3"
75+
android:layout_width="match_parent"
76+
android:layout_height="wrap_content">
77+
<TextView
78+
android:text="@string/label_transformed_url_drawable"
79+
android:layout_width="0dp"
80+
android:layout_height="wrap_content"
81+
android:layout_weight="1"
82+
android:layout_gravity="center"/>
83+
84+
<ImageView
85+
android:id="@+id/transformed_url_drawable"
86+
android:layout_width="0dp"
87+
android:layout_height="wrap_content"
88+
android:layout_weight="1"
89+
android:layout_gravity="center"
90+
android:gravity="center"/>
91+
</LinearLayout>
92+
</RelativeLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="local_drawable_color">#00FF00</color>
4+
<color name="remote_image_color">#FF0000</color>
5+
<color name="action_bar_icon_color">#00FFFF</color>
6+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">IconColorization</string>
5+
6+
<string name="image_url">https://cdn1.iconfinder.com/data/icons/simple-icons/128/android-128-black.png</string>
7+
8+
<string name="label_normal_drawable">Normal Drawable</string>
9+
<string name="label_transformed_drawable">Transformed Local Drawable</string>
10+
<string name="label_normal_url_drawable">URL Drawable</string>
11+
<string name="label_transformed_url_drawable">Transformed URL Drawable</string>
12+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="Theme.AppCompat">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>

IconColorization/build.gradle

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
repositories {
5+
jcenter()
6+
}
7+
dependencies {
8+
classpath 'com.android.tools.build:gradle:0.12.2'
9+
10+
// NOTE: Do not place your application dependencies here; they belong
11+
// in the individual module build.gradle files
12+
}
13+
}
14+
15+
allprojects {
16+
repositories {
17+
jcenter()
18+
}
19+
}

IconColorization/gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Settings specified in this file will override any Gradle settings
5+
# configured through the IDE.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# Specifies the JVM arguments used for the daemon process.
11+
# The setting is particularly useful for tweaking memory settings.
12+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
13+
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14+
15+
# When configured, Gradle will run in incubating parallel mode.
16+
# This option should only be used with decoupled projects. More details, visit
17+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18+
# org.gradle.parallel=true
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Wed Apr 10 15:27:10 PDT 2013
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip

0 commit comments

Comments
 (0)