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