1 package com.android.gallery3d.filtershow.controller; 2 3 import android.app.ActionBar.LayoutParams; 4 import android.content.Context; 5 import android.content.res.Resources; 6 import android.graphics.Color; 7 import android.graphics.drawable.GradientDrawable; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.Button; 12 import android.widget.LinearLayout; 13 14 import com.android.gallery3d.R; 15 import com.android.gallery3d.filtershow.colorpicker.ColorListener; 16 import com.android.gallery3d.filtershow.colorpicker.ColorPickerDialog; 17 import com.android.gallery3d.filtershow.editors.Editor; 18 19 import java.util.Arrays; 20 import java.util.Vector; 21 22 public class ColorChooser implements Control { 23 private final String LOGTAG = "StyleChooser"; 24 protected ParameterColor mParameter; 25 protected LinearLayout mLinearLayout; 26 protected Editor mEditor; 27 private View mTopView; 28 private Vector<Button> mIconButton = new Vector<Button>(); 29 protected int mLayoutID = R.layout.filtershow_control_color_chooser; 30 Context mContext; 31 private int mTransparent; 32 private int mSelected; 33 private static final int OPACITY_OFFSET = 3; 34 private int[] mButtonsID = { 35 R.id.draw_color_button01, 36 R.id.draw_color_button02, 37 R.id.draw_color_button03, 38 R.id.draw_color_button04, 39 R.id.draw_color_button05, 40 }; 41 private Button[] mButton = new Button[mButtonsID.length]; 42 43 int mSelectedButton = 0; 44 45 @Override 46 public void setUp(ViewGroup container, Parameter parameter, Editor editor) { 47 container.removeAllViews(); 48 Resources res = container.getContext().getResources(); 49 mTransparent = res.getColor(R.color.color_chooser_unslected_border); 50 mSelected = res.getColor(R.color.color_chooser_slected_border); 51 mEditor = editor; 52 mContext = container.getContext(); 53 int iconDim = res.getDimensionPixelSize(R.dimen.draw_style_icon_dim); 54 mParameter = (ParameterColor) parameter; 55 LayoutInflater inflater = 56 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 57 mTopView = inflater.inflate(mLayoutID, container, true); 58 mLinearLayout = (LinearLayout) mTopView.findViewById(R.id.listStyles); 59 mTopView.setVisibility(View.VISIBLE); 60 61 mIconButton.clear(); 62 LayoutParams lp = new LayoutParams(iconDim, iconDim); 63 int [] palette = mParameter.getColorPalette(); 64 for (int i = 0; i < mButtonsID.length; i++) { 65 final Button button = (Button) mTopView.findViewById(mButtonsID[i]); 66 mButton[i] = button; 67 float[] hsvo = new float[4]; 68 Color.colorToHSV(palette[i], hsvo); 69 hsvo[OPACITY_OFFSET] = (0xFF & (palette[i] >> 24)) / (float) 255; 70 button.setTag(hsvo); 71 GradientDrawable sd = ((GradientDrawable) button.getBackground()); 72 sd.setColor(palette[i]); 73 sd.setStroke(3, (mSelectedButton == i) ? mSelected : mTransparent); 74 75 final int buttonNo = i; 76 button.setOnClickListener(new View.OnClickListener() { 77 @Override 78 public void onClick(View arg0) { 79 selectColor(arg0, buttonNo); 80 } 81 }); 82 } 83 Button button = (Button) mTopView.findViewById(R.id.draw_color_popupbutton); 84 85 button.setOnClickListener(new View.OnClickListener() { 86 @Override 87 public void onClick(View arg0) { 88 showColorPicker(); 89 } 90 }); 91 92 } 93 94 public void setColorSet(int[] basColors) { 95 int []palette = mParameter.getColorPalette(); 96 for (int i = 0; i < palette.length; i++) { 97 palette[i] = basColors[i]; 98 float[] hsvo = new float[4]; 99 Color.colorToHSV(palette[i], hsvo); 100 hsvo[OPACITY_OFFSET] = (0xFF & (palette[i] >> 24)) / (float) 255; 101 mButton[i].setTag(hsvo); 102 GradientDrawable sd = ((GradientDrawable) mButton[i].getBackground()); 103 sd.setColor(palette[i]); 104 } 105 106 } 107 108 public int[] getColorSet() { 109 return mParameter.getColorPalette(); 110 } 111 112 private void resetBorders() { 113 int []palette = mParameter.getColorPalette(); 114 for (int i = 0; i < mButtonsID.length; i++) { 115 final Button button = mButton[i]; 116 GradientDrawable sd = ((GradientDrawable) button.getBackground()); 117 sd.setColor(palette[i]); 118 sd.setStroke(3, (mSelectedButton == i) ? mSelected : mTransparent); 119 } 120 } 121 122 public void selectColor(View button, int buttonNo) { 123 mSelectedButton = buttonNo; 124 float[] hsvo = (float[]) button.getTag(); 125 mParameter.setValue(Color.HSVToColor((int) (hsvo[OPACITY_OFFSET] * 255), hsvo)); 126 resetBorders(); 127 mEditor.commitLocalRepresentation(); 128 } 129 130 @Override 131 public View getTopView() { 132 return mTopView; 133 } 134 135 @Override 136 public void setPrameter(Parameter parameter) { 137 mParameter = (ParameterColor) parameter; 138 updateUI(); 139 } 140 141 @Override 142 public void updateUI() { 143 if (mParameter == null) { 144 return; 145 } 146 } 147 148 public void changeSelectedColor(float[] hsvo) { 149 int []palette = mParameter.getColorPalette(); 150 int c = Color.HSVToColor((int) (hsvo[3] * 255), hsvo); 151 final Button button = mButton[mSelectedButton]; 152 GradientDrawable sd = ((GradientDrawable) button.getBackground()); 153 sd.setColor(c); 154 palette[mSelectedButton] = c; 155 mParameter.setValue(Color.HSVToColor((int) (hsvo[OPACITY_OFFSET] * 255), hsvo)); 156 button.setTag(hsvo); 157 mEditor.commitLocalRepresentation(); 158 button.invalidate(); 159 } 160 161 public void showColorPicker() { 162 ColorListener cl = new ColorListener() { 163 @Override 164 public void setColor(float[] hsvo) { 165 changeSelectedColor(hsvo); 166 } 167 @Override 168 public void addColorListener(ColorListener l) { 169 } 170 }; 171 ColorPickerDialog cpd = new ColorPickerDialog(mContext, cl); 172 float[] c = (float[]) mButton[mSelectedButton].getTag(); 173 cpd.setColor(Arrays.copyOf(c, 4)); 174 cpd.setOrigColor(Arrays.copyOf(c, 4)); 175 cpd.show(); 176 } 177 } 178