Skip to content

Commit e2e9aee

Browse files
Paul TrebilcoxPaul Trebilcox
Paul Trebilcox
authored and
Paul Trebilcox
committed
Added Palette sample
1 parent 97861bd commit e2e9aee

26 files changed

+603
-0
lines changed

Palette/app/.gitignore

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

Palette/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 22
5+
buildToolsVersion "22.0.1"
6+
7+
defaultConfig {
8+
applicationId "com.tutsplus.palette"
9+
minSdkVersion 15
10+
targetSdkVersion 22
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled 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:22.2.0'
25+
compile 'com.android.support:palette-v7:+'
26+
}

Palette/app/proguard-rules.pro

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 /Users/paulruiz/Library/Android/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.tutsplus.palette;
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.tutsplus.palette" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.tutsplus.palette;
2+
3+
import android.graphics.Bitmap;
4+
import android.graphics.BitmapFactory;
5+
import android.os.Bundle;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.support.v7.graphics.Palette;
8+
import android.view.View;
9+
import android.widget.ListView;
10+
import android.widget.TextView;
11+
12+
13+
public class MainActivity extends AppCompatActivity {
14+
15+
private ListView mListView;
16+
private SwatchAdapter mAdapter;
17+
18+
private TextView mVibrantTextView;
19+
private TextView mLightVibrantTextView;
20+
private TextView mDarkVibrantTextView;
21+
private TextView mMutedTextView;
22+
private TextView mLightMutedTextView;
23+
private TextView mDarkMutedTextView;
24+
25+
@Override
26+
protected void onCreate(Bundle savedInstanceState) {
27+
super.onCreate(savedInstanceState);
28+
29+
setContentView( R.layout.activity_main );
30+
31+
initViews();
32+
33+
mAdapter = new SwatchAdapter( this );
34+
mListView.setAdapter( mAdapter );
35+
36+
Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.union_station );
37+
Palette.from( bitmap ).generate( new Palette.PaletteAsyncListener() {
38+
@Override
39+
public void onGenerated( Palette palette ) {
40+
41+
setViewSwatch( mVibrantTextView, palette.getVibrantSwatch() );
42+
setViewSwatch( mLightVibrantTextView, palette.getLightVibrantSwatch() );
43+
setViewSwatch( mDarkVibrantTextView, palette.getDarkVibrantSwatch() );
44+
setViewSwatch( mMutedTextView, palette.getMutedSwatch() );
45+
setViewSwatch( mLightMutedTextView, palette.getLightMutedSwatch() );
46+
setViewSwatch( mDarkMutedTextView, palette.getDarkMutedSwatch() );
47+
48+
for( Palette.Swatch swatch : palette.getSwatches() ) {
49+
mAdapter.add(swatch);
50+
}
51+
52+
mAdapter.sortSwatches();
53+
mAdapter.notifyDataSetChanged();
54+
55+
56+
}
57+
});
58+
}
59+
60+
public void setViewSwatch( TextView view, Palette.Swatch swatch ) {
61+
if( swatch != null ) {
62+
view.setTextColor( swatch.getTitleTextColor() );
63+
view.setBackgroundColor( swatch.getRgb() );
64+
view.setVisibility( View.VISIBLE );
65+
} else {
66+
view.setVisibility( View.GONE );
67+
}
68+
}
69+
70+
private void initViews() {
71+
mListView = (ListView) findViewById( R.id.list );
72+
mVibrantTextView = (TextView) findViewById( R.id.vibrant );
73+
mLightVibrantTextView = (TextView) findViewById( R.id.light_vibrant );
74+
mDarkVibrantTextView = (TextView) findViewById( R.id.dark_vibrant );
75+
mMutedTextView = (TextView) findViewById( R.id.muted );
76+
mLightMutedTextView = (TextView) findViewById( R.id.light_muted );
77+
mDarkMutedTextView = (TextView) findViewById( R.id.dark_muted );
78+
}
79+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.tutsplus.palette;
2+
3+
import android.content.Context;
4+
import android.support.v7.graphics.Palette;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.ArrayAdapter;
9+
import android.widget.TextView;
10+
11+
import java.util.Comparator;
12+
13+
/**
14+
* Created by paulruiz on 5/29/15.
15+
*/
16+
public class SwatchAdapter extends ArrayAdapter<Palette.Swatch> {
17+
18+
public SwatchAdapter( Context context ) {
19+
this( context, 0 );
20+
}
21+
22+
public SwatchAdapter(Context context, int resource) {
23+
super(context, resource);
24+
}
25+
26+
@Override
27+
public View getView(int position, View convertView, ViewGroup parent) {
28+
ViewHolder holder;
29+
if( convertView == null ) {
30+
holder = new ViewHolder();
31+
convertView = LayoutInflater.from( getContext() ).inflate( R.layout.color_item, parent, false );
32+
holder.view = (TextView) convertView.findViewById( R.id.view );
33+
convertView.setTag( holder );
34+
} else {
35+
holder = (ViewHolder) convertView.getTag();
36+
}
37+
38+
holder.view.setBackgroundColor( getItem( position ).getRgb() );
39+
holder.view.setTextColor( getItem( position ).getBodyTextColor() );
40+
holder.view.setText( "Population: " + getItem( position ).getPopulation() );
41+
42+
return convertView;
43+
}
44+
45+
public void sortSwatches() {
46+
sort(new Comparator<Palette.Swatch>() {
47+
@Override
48+
public int compare( Palette.Swatch lhs, Palette.Swatch rhs ) {
49+
return rhs.getPopulation() - lhs.getPopulation();
50+
}
51+
});
52+
}
53+
54+
public class ViewHolder {
55+
TextView view;
56+
}
57+
}
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<LinearLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:orientation="vertical">
6+
<ImageView
7+
android:id="@+id/image"
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:adjustViewBounds="true"
11+
android:src="@drawable/union_station" />
12+
13+
<TextView
14+
android:id="@+id/vibrant"
15+
android:layout_width="match_parent"
16+
android:layout_height="wrap_content"
17+
android:text="Vibrant"/>
18+
19+
<TextView
20+
android:id="@+id/light_vibrant"
21+
android:layout_width="match_parent"
22+
android:layout_height="wrap_content"
23+
android:text="Light Vibrant"/>
24+
25+
<TextView
26+
android:id="@+id/dark_vibrant"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:text="Dark Vibrant"/>
30+
31+
<TextView
32+
android:id="@+id/muted"
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:text="Muted" />
36+
37+
<TextView
38+
android:id="@+id/light_muted"
39+
android:layout_width="match_parent"
40+
android:layout_height="wrap_content"
41+
android:text="Light Muted" />
42+
43+
<TextView
44+
android:id="@+id/dark_muted"
45+
android:layout_width="match_parent"
46+
android:layout_height="wrap_content"
47+
android:text="Dark Muted" />
48+
49+
<ListView
50+
android:id="@+id/list"
51+
android:layout_width="match_parent"
52+
android:layout_height="wrap_content"
53+
android:padding="16dp" />
54+
</LinearLayout>
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+
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/view"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:text="Text"/>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
4+
<item android:id="@+id/action_settings" android:title="@string/action_settings"
5+
android:orderInCategory="100" app:showAsAction="never" />
6+
</menu>
Loading
Loading
Loading
Loading
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: 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<string name="app_name">Palette</string>
3+
4+
<string name="hello_world">Hello world!</string>
5+
<string name="action_settings">Settings</string>
6+
</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.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>

Palette/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:1.1.0'
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+
}

Palette/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+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
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
48.7 KB
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=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip

0 commit comments

Comments
 (0)