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