App
LayerView.java
Go to the documentation of this file.
1 package mhr.appandroid.views.imagepresentationview;
2 
3 import mhr.appandroid.R;
4 import mhr.appandroid.adapters.APDBitmap;
5 import mhr.appandroid.views.SimpleBitmapView;
6 import mhr.appandroid.views.imagepresentationview.ImagePresentationEvent.EventType;
7 import mhr.appcore.image.ImageSelectedState;
8 import mhr.appcore.image.layers.LayerPDInfo;
9 import mhr.appcore.image.layers.LayerType;
10 import android.app.Activity;
11 import android.content.ClipData;
12 import android.content.Context;
13 import android.graphics.BitmapFactory;
14 import android.graphics.BitmapShader;
15 import android.graphics.Rect;
16 import android.graphics.Shader.TileMode;
17 import android.util.AttributeSet;
18 import android.widget.CheckBox;
19 import android.widget.CompoundButton;
20 import android.widget.CompoundButton.OnCheckedChangeListener;
21 import android.widget.PopupMenu.OnMenuItemClickListener;
22 import android.widget.FrameLayout;
23 import android.widget.ImageView;
24 import android.widget.PopupMenu;
25 import android.widget.TextView;
26 import android.view.DragEvent;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.view.View.OnLongClickListener;
31 
32 public class LayerView extends FrameLayout implements OnCheckedChangeListener, OnClickListener, OnLongClickListener, OnMenuItemClickListener {
33 
34  //===================================================================================================================================================//
35  //===== PUBLIC ==============================================================================================================================//
36  //----- interface a třídy -------------------------------------------------------------------------------------------------------------------//
37  public interface OnLayerViewEventListener {
39  }
40 
41  public static enum SelectedState {
42  SELECTED_NONE, SELECTED_LAYER, SELECTED_MASK
43  }
44 
45  //----- pole --------------------------------------------------------------------------------------------------------------------------------//
46  //----- acesory a primitivní metody ---------------------------------------------------------------------------------------------------------//
48  this.listener = listener;
49  }
50 
51  public void setState(SelectedState state) {
52  this.state = state;
53  switch (state) {
54  case SELECTED_LAYER:
55  layerPreviewCheckedV.setBackgroundColor(checkedColor);
56  layerMaskPreviewCheckedV.setBackgroundColor(uncheckedColor);
57  break;
58  case SELECTED_MASK:
59  layerPreviewCheckedV.setBackgroundColor(uncheckedColor);
60  layerMaskPreviewCheckedV.setBackgroundColor(checkedColor);
61  break;
62  case SELECTED_NONE:
63  layerPreviewCheckedV.setBackgroundColor(uncheckedColor);
64  layerMaskPreviewCheckedV.setBackgroundColor(uncheckedColor);
65  break;
66  }
67  }
68 
70  return state;
71  }
72 
73  @Deprecated
74  // Problém s ISV, pokud neexistuje instance třídy...
75  public Rect getLayerPreviewRect() {
76  return layerPreviewSBV.getDstRect();
77  }
78 
79  @Deprecated
80  // Problém s ISV, pokud neexistuje instance třídy...
81  public Rect getMaskPreviewRect() {
82  return maskPreviewSBV.getDstRect();
83  }
84 
85  //----- konstruktory ------------------------------------------------------------------------------------------------------------------------//
86  public LayerView(Context context) {
87  super(context);
88  init();
89  }
90 
91  public LayerView(Context context, AttributeSet attrs) {
92  super(context, attrs);
93  init();
94  }
95 
96  public LayerView(Context context, AttributeSet attrs, int defStyle) {
97  super(context, attrs, defStyle);
98  init();
99  }
100 
101  //----- destruktory -------------------------------------------------------------------------------------------------------------------------//
102  //----- metody ------------------------------------------------------------------------------------------------------------------------------//
103  public void presentLayer(LayerPDInfo info) {
104  if (layerId == info.id && !info.isChanged) {
105  return;
106  }
107  layerId = info.id;
108  layerType = info.type;
109  layerVisibleChB.setChecked(info.visible);
110  maskActiveChB.setChecked(info.maskActive);
111  if (info.getMaskThumb(false) != null) {
112  maskPreviewSBV.setBitmap(((APDBitmap) info.getMaskThumb(false)).getBitmap());
113  maskPreviewSBV.setVisibility(VISIBLE);
114  maskThumbIV.setVisibility(INVISIBLE);
115  } else {
116  maskPreviewSBV.setVisibility(INVISIBLE);
117  maskThumbIV.setVisibility(VISIBLE);
118  }
119  switch (layerType) {
120  case LAYER_RASTER:
121  layerPreviewSBV.setBitmap(((APDBitmap) info.getLayerThumb(false)).getBitmap());
122  layerPreviewSBV.setVisibility(VISIBLE);
123  layerThumbIV.setVisibility(INVISIBLE);
124  break;
125  default:
126  break;
127  }
128  info.isChanged = false;
129  }
130 
131  //===================================================================================================================================================//
132  //===== NON-PUBLIC ==========================================================================================================================//
133  //----- interface a třídy -------------------------------------------------------------------------------------------------------------------//
134  //----- pole --------------------------------------------------------------------------------------------------------------------------------//
135  protected LayerType layerType;
136  protected int layerId;
137  protected SelectedState state;
138 
139  protected ImageView layerThumbIV;
140  protected ImageView maskThumbIV;
143  protected CheckBox layerVisibleChB;
144  protected CheckBox maskActiveChB;
145  protected View layerPreviewCheckedV;
146  protected View layerMaskPreviewCheckedV;
147  protected View LayerViewOverlay;
148  protected ImageView moveLayerIcoIV;
149 
150  protected PopupMenu handlePopup;
151  protected PopupMenu layerPopup;
152  protected PopupMenu maskPopup;
153 
154  protected int checkedColor;
155  protected int uncheckedColor;
156 
158 
159  //----- acesory a primitivní metody ---------------------------------------------------------------------------------------------------------//
160  //----- konstruktory ------------------------------------------------------------------------------------------------------------------------//
161 
162  protected void init() {
163  try {
164  ((Activity) getContext()).getLayoutInflater().inflate(R.layout.view_layer, this, true);
165  } catch (ClassCastException exc) {
166  // Tento kod probehne, pokud je poroblem s pretypovanim, napriklad pro nahled v Eclipse
167  TextView tv = new TextView(getContext());
168  tv.setText("Prewiew not available, requires Activity to be Context.");
169  addView(tv);
170  return;
171  }
172 
173  layerThumbIV = (ImageView) findViewById(R.id.LayerThumb);
174  maskThumbIV = (ImageView) findViewById(R.id.LayerMaskThumb);
175  layerPreviewSBV = (SimpleBitmapView) findViewById(R.id.LayerPreview);
176  maskPreviewSBV = (SimpleBitmapView) findViewById(R.id.LayerMaskPreview);
177  layerVisibleChB = (CheckBox) findViewById(R.id.LayerVisibleChckB);
178  maskActiveChB = (CheckBox) findViewById(R.id.MaskAppliedChckB);
179  layerPreviewCheckedV = findViewById(R.id.LayerPreviewChecked);
180  layerMaskPreviewCheckedV = findViewById(R.id.LayerMaskPreviewChecked);
181  moveLayerIcoIV = (ImageView) findViewById(R.id.MoveLayerIco);
182  LayerViewOverlay = findViewById(R.id.LayerViewOverlay);
183 
184  layerPreviewCheckedV.setOnClickListener(this);
185  layerPreviewCheckedV.setOnLongClickListener(this);
186 
187  layerMaskPreviewCheckedV.setOnClickListener(this);
188  layerMaskPreviewCheckedV.setOnLongClickListener(this);
189 
190  moveLayerIcoIV.setOnClickListener(this);
191  moveLayerIcoIV.setOnLongClickListener(this);
192 
193  layerVisibleChB.setOnCheckedChangeListener(this);
194  maskActiveChB.setOnCheckedChangeListener(this);
195 
196  maskPreviewSBV.getBitmapBackground().setShader(new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.checkboard), TileMode.REPEAT, TileMode.REPEAT));
197  layerPreviewSBV.getBitmapBackground().setShader(new BitmapShader(BitmapFactory.decodeResource(getResources(), R.drawable.checkboard), TileMode.REPEAT, TileMode.REPEAT));
199 
200  uncheckedColor = 0x00000000;
201  checkedColor = getResources().getColor(R.color.SelectedToolBackground);
202 
204 
205  layerType = null;
206  layerId = -1;
207 
208  handlePopup = new PopupMenu(getContext(), this);
209  handlePopup.inflate(R.menu.view_layer_view);
210  handlePopup.setOnMenuItemClickListener(this);
211 
212  layerPopup = new PopupMenu(getContext(), this);
213  layerPopup.inflate(R.menu.view_layer_view_layer);
214  layerPopup.setOnMenuItemClickListener(this);
215 
216  maskPopup = new PopupMenu(getContext(), this);
217  maskPopup.inflate(R.menu.view_layer_view_mask);
218  maskPopup.setOnMenuItemClickListener(this);
219  }
220 
221  @Override
222  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
223  if (buttonView == layerVisibleChB) {
224  if (listener != null) {
226  }
227  } else if (buttonView == maskActiveChB) {
228  if (listener != null) {
230  }
231  }
232  }
233 
234  @Override
235  public void onClick(View v) {
236  if (v == layerPreviewCheckedV) {
239  if (listener != null) {
241  }
242  }
243  } else if (v == layerMaskPreviewCheckedV) {
246  if (listener != null) {
248  }
249  }
250  } else if (v == moveLayerIcoIV) {
251  handlePopup.show();
252  }
253  }
254 
255  @Override
256  public boolean onLongClick(View v) {
257  if (v == moveLayerIcoIV) {
258  ClipData data = ClipData.newPlainText("LayerId", Integer.toString(layerId));
259  DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(this);
260  startDrag(data, shadowBuilder, this, 0);
261  LayerViewOverlay.setVisibility(VISIBLE);
262  return true;
263  } else if (v == layerPreviewCheckedV) {
264  layerPopup.show();
265  return true;
266  } else if (v == layerMaskPreviewCheckedV) {
267  maskPopup.show();
268  return true;
269  } else {
270  return true;
271  }
272 
273  }
274 
275  @Override
276  public boolean onDragEvent(DragEvent event) {
277  if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
278  LayerViewOverlay.setVisibility(GONE);
279  return true;
280  }
281  return false; // Dost podezrely vyznam
282  }
283 
284  @Override
285  public boolean onMenuItemClick(MenuItem item) {
286  final int id = item.getItemId();
287  if (id == R.id.LayerPopUpDelete) {
288  if (listener != null) {
290  }
291  } else if (id == R.id.LayerPopUpDuplicate) {
292  if (listener != null) {
294  }
295 
296  } else if (id == R.id.LayerPopUpCopyLayerBitmap) {
297  if (listener != null) {
299  }
300  } else if (id == R.id.LayerPopUpPasteLayerBitmap) {
301  if (listener != null) {
303  }
304  } else if (id == R.id.LayerPopUpClearLayerBitmap) {
305  if (listener != null) {
307  }
308  } else if (id == R.id.LayerPopUpAddLayerBitmap) {
309  if (listener != null) {
311  }
312  } else if (id == R.id.LayerPopUpRemoveLayerBitmap) {
313  if (listener != null) {
315  }
316  } else if (id == R.id.LayerPopUpBlendLayerBitmap) {
317  if (listener != null) {
319  }
320 
321 
322  } else if (id == R.id.LayerPopUpCopyMaskBitmap) {
323  if (listener != null) {
325  }
326  } else if (id == R.id.LayerPopUpPasteMaskBitmap) {
327  if (listener != null) {
329  }
330  } else if (id == R.id.LayerPopUpClearMaskBitmap) {
331  if (listener != null) {
333  }
334  } else if (id == R.id.LayerPopUpFillMaskBitmap) {
335  if (listener != null) {
337  }
338  } else if (id == R.id.LayerPopUpInvertMaskBitmap) {
339  if (listener != null) {
341  }
342  } else if (id == R.id.LayerPopUpAddMaskBitmap) {
343  if (listener != null) {
345  }
346  } else if (id == R.id.LayerPopUpRemoveMaskBitmap) {
347  if (listener != null) {
349  }
350  }
351  return true;
352  }
353  //----- destruktory -------------------------------------------------------------------------------------------------------------------------//
354  //----- metody ------------------------------------------------------------------------------------------------------------------------------//
355 
356  //===================================================================================================================================================//
357  //===== DEPRECATED ==========================================================================================================================//
358 
359 }