Home | History | Annotate | Download | only in colorpicker
      1 /*
      2  * Copyright (C) 2013 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.android.colorpicker;
     18 
     19 import android.content.Context;
     20 import android.graphics.PorterDuff.Mode;
     21 import android.graphics.drawable.ColorDrawable;
     22 import android.graphics.drawable.Drawable;
     23 import android.graphics.drawable.LayerDrawable;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.widget.FrameLayout;
     27 import android.widget.ImageView;
     28 
     29 /**
     30  * Creates a circular swatch of a specified color.  Adds a checkmark if marked as checked.
     31  */
     32 public class ColorPickerSwatch extends FrameLayout implements View.OnClickListener {
     33     private int mColor;
     34     private ImageView mSwatchImage;
     35     private ImageView mCheckmarkImage;
     36     private OnColorSelectedListener mOnColorSelectedListener;
     37 
     38     /**
     39      * Interface for a callback when a color square is selected.
     40      */
     41     public interface OnColorSelectedListener {
     42 
     43         /**
     44          * Called when a specific color square has been selected.
     45          */
     46         public void onColorSelected(int color);
     47     }
     48 
     49     public ColorPickerSwatch(Context context, int color, boolean checked,
     50             OnColorSelectedListener listener) {
     51         super(context);
     52         mColor = color;
     53         mOnColorSelectedListener = listener;
     54 
     55         LayoutInflater.from(context).inflate(R.layout.color_picker_swatch, this);
     56         mSwatchImage = (ImageView) findViewById(R.id.color_picker_swatch);
     57         mCheckmarkImage = (ImageView) findViewById(R.id.color_picker_checkmark);
     58         setColor(color);
     59         setChecked(checked);
     60         setOnClickListener(this);
     61     }
     62 
     63     protected void setColor(int color) {
     64         Drawable[] colorDrawable = new Drawable[]
     65                 {getContext().getResources().getDrawable(R.drawable.color_picker_swatch)};
     66         mSwatchImage.setImageDrawable(new ColorStateDrawable(colorDrawable, color));
     67     }
     68 
     69     private void setChecked(boolean checked) {
     70         if (checked) {
     71             mCheckmarkImage.setVisibility(View.VISIBLE);
     72         } else {
     73             mCheckmarkImage.setVisibility(View.GONE);
     74         }
     75     }
     76 
     77     @Override
     78     public void onClick(View v) {
     79         if (mOnColorSelectedListener != null) {
     80             mOnColorSelectedListener.onColorSelected(mColor);
     81         }
     82     }
     83 }
     84