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

0 commit comments

Comments
 (0)