[go: nahoru, domu]

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