2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.imageshow;
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;
26 import com.android.gallery3d.filtershow.editors.EditorVignette;
27 import com.android.gallery3d.filtershow.filters.FilterVignetteRepresentation;
29 public class ImageVignette extends ImageShow {
30 private static final String LOGTAG = "ImageVignette";
32 private FilterVignetteRepresentation mVignetteRep;
33 private EditorVignette mEditorVignette;
35 private int mActiveHandle = -1;
37 EclipseControl mElipse;
39 public ImageVignette(Context context) {
41 mElipse = new EclipseControl(context);
44 public ImageVignette(Context context, AttributeSet attrs) {
45 super(context, attrs);
46 mElipse = new EclipseControl(context);
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);
56 if (event.getPointerCount() == 1) {
57 mActiveHandle = mElipse.getCloseHandle(event.getX(), event.getY());
59 if (mActiveHandle == -1) {
60 return super.onTouchEvent(event);
64 case MotionEvent.ACTION_UP:
67 case MotionEvent.ACTION_DOWN:
71 float x = event.getX();
72 float y = event.getY();
74 mElipse.setScrImageInfo(getScreenToImageMatrix(true),
75 MasterImage.getImage().getOriginalBounds());
77 boolean didComputeEllipses = false;
79 case (MotionEvent.ACTION_DOWN):
80 mElipse.actionDown(x, y, mVignetteRep);
82 case (MotionEvent.ACTION_UP):
83 case (MotionEvent.ACTION_MOVE):
84 mElipse.actionMove(mActiveHandle, x, y, mVignetteRep);
85 setRepresentation(mVignetteRep);
86 didComputeEllipses = true;
89 if (!didComputeEllipses) {
96 public void setRepresentation(FilterVignetteRepresentation vignetteRep) {
97 mVignetteRep = vignetteRep;
101 public void computeEllipses() {
102 if (mVignetteRep == null) {
105 Matrix toImg = getScreenToImageMatrix(false);
106 Matrix toScr = new Matrix();
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;
116 mVignetteRep.setCenter(cx, cy);
117 mVignetteRep.setRadius(rx, ry);
122 if (getWidth() != 0) {
123 mElipse.setCenter(c[0], c[1]);
124 mElipse.setRadius(c[0] * 0.8f, c[1] * 0.8f);
130 mElipse.setCenter(c[0], c[1]);
131 mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX()),
132 toScr.mapRadius(mVignetteRep.getRadiusY()));
134 mEditorVignette.commitLocalRepresentation();
137 public void setEditor(EditorVignette editorVignette) {
138 mEditorVignette = editorVignette;
142 public void onSizeChanged(int w, int h, int oldw, int oldh) {
143 super.onSizeChanged(w, h, oldw, oldh);
148 public void onDraw(Canvas canvas) {
149 super.onDraw(canvas);
150 if (mVignetteRep == null) {
153 Matrix toImg = getScreenToImageMatrix(false);
154 Matrix toScr = new Matrix();
156 float[] c = new float[] {
157 mVignetteRep.getCenterX(), mVignetteRep.getCenterY() };
159 mElipse.setCenter(c[0], c[1]);
160 mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX()),
161 toScr.mapRadius(mVignetteRep.getRadiusY()));
163 mElipse.draw(canvas);