OSDN Git Service

am e1466c18: am cfc25ca6: am 960e83bd: Fix toast message when saving to Gallery
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / imageshow / ImageVignette.java
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.gallery3d.filtershow.imageshow;
18
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Matrix;
22 import android.util.AttributeSet;
23 import android.util.Log;
24 import android.view.MotionEvent;
25
26 import com.android.gallery3d.filtershow.editors.EditorVignette;
27 import com.android.gallery3d.filtershow.filters.FilterVignetteRepresentation;
28
29 public class ImageVignette extends ImageShow {
30     private static final String LOGTAG = "ImageVignette";
31
32     private FilterVignetteRepresentation mVignetteRep;
33     private EditorVignette mEditorVignette;
34
35     private int mActiveHandle = -1;
36
37     EclipseControl mElipse;
38
39     public ImageVignette(Context context) {
40         super(context);
41         mElipse = new EclipseControl(context);
42     }
43
44     public ImageVignette(Context context, AttributeSet attrs) {
45         super(context, attrs);
46         mElipse = new EclipseControl(context);
47     }
48
49     @Override
50     public boolean onTouchEvent(MotionEvent event) {
51         int mask = event.getActionMasked();
52         if (mActiveHandle == -1) {
53             if (MotionEvent.ACTION_DOWN != mask) {
54                 return super.onTouchEvent(event);
55             }
56             if (event.getPointerCount() == 1) {
57                 mActiveHandle = mElipse.getCloseHandle(event.getX(), event.getY());
58             }
59             if (mActiveHandle == -1) {
60                 return super.onTouchEvent(event);
61             }
62         } else {
63             switch (mask) {
64                 case MotionEvent.ACTION_UP:
65                     mActiveHandle = -1;
66                     break;
67                 case MotionEvent.ACTION_DOWN:
68                     break;
69             }
70         }
71         float x = event.getX();
72         float y = event.getY();
73
74         mElipse.setScrImageInfo(getScreenToImageMatrix(true),
75                 MasterImage.getImage().getOriginalBounds());
76
77         boolean didComputeEllipses = false;
78         switch (mask) {
79             case (MotionEvent.ACTION_DOWN):
80                 mElipse.actionDown(x, y, mVignetteRep);
81                 break;
82             case (MotionEvent.ACTION_UP):
83             case (MotionEvent.ACTION_MOVE):
84                 mElipse.actionMove(mActiveHandle, x, y, mVignetteRep);
85                 setRepresentation(mVignetteRep);
86                 didComputeEllipses = true;
87                 break;
88         }
89         if (!didComputeEllipses) {
90             computeEllipses();
91         }
92         invalidate();
93         return true;
94     }
95
96     public void setRepresentation(FilterVignetteRepresentation vignetteRep) {
97         mVignetteRep = vignetteRep;
98         computeEllipses();
99     }
100
101     public void computeEllipses() {
102         if (mVignetteRep == null) {
103             return;
104         }
105         Matrix toImg = getScreenToImageMatrix(false);
106         Matrix toScr = new Matrix();
107         toImg.invert(toScr);
108
109         float[] c = new float[] {
110                 mVignetteRep.getCenterX(), mVignetteRep.getCenterY() };
111         if (Float.isNaN(c[0])) {
112             float cx = MasterImage.getImage().getOriginalBounds().width() / 2;
113             float cy = MasterImage.getImage().getOriginalBounds().height() / 2;
114             float rx = Math.min(cx, cy) * .8f;
115             float ry = rx;
116             mVignetteRep.setCenter(cx, cy);
117             mVignetteRep.setRadius(rx, ry);
118
119             c[0] = cx;
120             c[1] = cy;
121             toScr.mapPoints(c);
122             if (getWidth() != 0) {
123                 mElipse.setCenter(c[0], c[1]);
124                 mElipse.setRadius(c[0] * 0.8f, c[1] * 0.8f);
125             }
126         } else {
127
128             toScr.mapPoints(c);
129
130             mElipse.setCenter(c[0], c[1]);
131             mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX()),
132                     toScr.mapRadius(mVignetteRep.getRadiusY()));
133         }
134         mEditorVignette.commitLocalRepresentation();
135     }
136
137     public void setEditor(EditorVignette editorVignette) {
138         mEditorVignette = editorVignette;
139     }
140
141     @Override
142     public void onSizeChanged(int w, int h, int oldw, int oldh) {
143         super.onSizeChanged(w,  h, oldw, oldh);
144         computeEllipses();
145     }
146
147     @Override
148     public void onDraw(Canvas canvas) {
149         super.onDraw(canvas);
150         if (mVignetteRep == null) {
151             return;
152         }
153         Matrix toImg = getScreenToImageMatrix(false);
154         Matrix toScr = new Matrix();
155         toImg.invert(toScr);
156         float[] c = new float[] {
157                 mVignetteRep.getCenterX(), mVignetteRep.getCenterY() };
158         toScr.mapPoints(c);
159         mElipse.setCenter(c[0], c[1]);
160         mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX()),
161                 toScr.mapRadius(mVignetteRep.getRadiusY()));
162
163         mElipse.draw(canvas);
164     }
165
166 }