1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.supportv7.graphics;
import com.example.android.supportv7.R;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.graphics.Palette;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.List;
/**
* Activity which displays the more details about a generated {@link Palette} for a specific
* {@link android.provider.MediaStore} image.
*
* Displays the full generated palette of colors in a grid, which allows clicking on an palette item
* to display more information in a {@link Toast}.
*
* Also allows the customization of the number of colors used in the palette generation for
* demonstration purposes.
*/
public class PaletteDetailActivity extends ActionBarActivity {
private ImageView mImageView;
private GridView mGridView;
private SwatchesPalette mSwatchesPalette;
private Uri mImageUri;
private Toast mCurrentToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.palette_activity_detail);
mImageUri = getIntent().getData();
mImageView = (ImageView) findViewById(R.id.image);
mGridView = (GridView) findViewById(R.id.palette);
mSwatchesPalette = new SwatchesPalette();
mGridView.setAdapter(mSwatchesPalette);
// Set an OnItemClickListener to display a information Toast when a Palette item is clicked
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
// Cancel the current Toast if there is already one being displayed
if (mCurrentToast != null) {
mCurrentToast.cancel();
}
final Palette.Swatch item = (Palette.Swatch) adapterView.getItemAtPosition(pos);
mCurrentToast = Toast.makeText(PaletteDetailActivity.this,
item.toString(), Toast.LENGTH_LONG);
mCurrentToast.show();
}
});
// Load the image with a default number of colors
loadImage(16);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_palette_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_num_colors_8:
loadImage(8);
item.setChecked(true);
return true;
case R.id.menu_num_colors_12:
loadImage(12);
item.setChecked(true);
return true;
case R.id.menu_num_colors_16:
loadImage(16);
item.setChecked(true);
return true;
case R.id.menu_num_colors_24:
loadImage(24);
item.setChecked(true);
return true;
case R.id.menu_num_colors_32:
loadImage(32);
item.setChecked(true);
return true;
}
return super.onOptionsItemSelected(item);
}
private void loadImage(final int numColors) {
final int id = Integer.parseInt(mImageUri.getLastPathSegment());
ImageLoader.loadMediaStoreThumbnail(mImageView, id, new ImageLoader.Listener() {
@Override
public void onImageLoaded(Bitmap bitmap) {
Palette.generateAsync(bitmap, numColors, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
populatePalette(palette);
}
});
}
});
}
private class SwatchesPalette extends BaseAdapter {
private List<Palette.Swatch> mSwatches;
@Override
public int getCount() {
return mSwatches != null ? mSwatches.size() : 0;
}
@Override
public Palette.Swatch getItem(int position) {
return mSwatches.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
void setSwatches(List<Palette.Swatch> palette) {
mSwatches = palette;
notifyDataSetChanged();
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = getLayoutInflater().inflate(R.layout.palette_grid_item, parent, false);
}
setBackgroundColor(view, getItem(position));
return view;
}
}
private void populatePalette(Palette palette) {
mSwatchesPalette.setSwatches(palette.getSwatches());
setBackgroundColor(findViewById(R.id.text_vibrant), palette.getVibrantSwatch());
setBackgroundColor(findViewById(R.id.text_muted), palette.getMutedSwatch());
setBackgroundColor(findViewById(R.id.text_light_vibrant), palette.getLightVibrantSwatch());
setBackgroundColor(findViewById(R.id.text_light_muted), palette.getLightMutedSwatch());
setBackgroundColor(findViewById(R.id.text_dark_vibrant), palette.getDarkVibrantSwatch());
setBackgroundColor(findViewById(R.id.text_dark_muted), palette.getDarkMutedSwatch());
}
private void setBackgroundColor(View view, Palette.Swatch swatch) {
if (view != null && swatch != null) {
view.setBackgroundColor(swatch.getRgb());
}
}
}
|