Chris Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.example.android.supportv7.graphics; |
| 18 | |
Chris Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
| 20 | import android.net.Uri; |
| 21 | import android.os.Bundle; |
Chris Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 22 | import android.view.Menu; |
| 23 | import android.view.MenuItem; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
| 26 | import android.widget.AdapterView; |
| 27 | import android.widget.BaseAdapter; |
| 28 | import android.widget.GridView; |
| 29 | import android.widget.ImageView; |
| 30 | import android.widget.Toast; |
| 31 | |
Aurimas Liutikas | def582a | 2018-03-13 14:32:27 -0700 | [diff] [blame] | 32 | import androidx.appcompat.app.AppCompatActivity; |
| 33 | import androidx.palette.graphics.Palette; |
| 34 | |
| 35 | import com.example.android.supportv7.R; |
| 36 | |
Chris Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 37 | import 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 Banes | 44d2526 | 2015-01-05 15:54:17 +0000 | [diff] [blame] | 49 | public class PaletteDetailActivity extends AppCompatActivity { |
Chris Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 50 | |
| 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 Viverette | fa2e2ac | 2017-03-31 14:01:12 -0400 | [diff] [blame] | 66 | mImageView = findViewById(R.id.image); |
| 67 | mGridView = findViewById(R.id.palette); |
Chris Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 68 | 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 Liutikas | e8e0eae | 2016-08-24 16:52:51 -0700 | [diff] [blame] | 130 | 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 Banes | ef3b660 | 2014-05-19 16:32:48 +0100 | [diff] [blame] | 137 | } |
| 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 | } |