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