ImageVignette.java revision fc7d46515db7a3a50f7416c2462030609391cf95
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 17package com.android.gallery3d.filtershow.imageshow; 18 19import android.content.Context; 20import android.graphics.Canvas; 21import android.graphics.Matrix; 22import android.util.AttributeSet; 23import android.util.Log; 24import android.view.MotionEvent; 25 26import com.android.gallery3d.filtershow.editors.EditorVignette; 27import com.android.gallery3d.filtershow.filters.FilterVignetteRepresentation; 28 29public 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 if (event.getPointerCount() == 1) { 69 Log.v(LOGTAG, "################### ACTION_DOWN odd " + mActiveHandle 70 + " touches=1"); 71 } 72 break; 73 } 74 } 75 float x = event.getX(); 76 float y = event.getY(); 77 78 mElipse.setScrToImageMatrix(getScreenToImageMatrix(true)); 79 80 switch (mask) { 81 case (MotionEvent.ACTION_DOWN): 82 mElipse.actionDown(x, y, mVignetteRep); 83 break; 84 case (MotionEvent.ACTION_UP): 85 case (MotionEvent.ACTION_MOVE): 86 87 mElipse.actionMove(mActiveHandle, x, y, mVignetteRep); 88 setRepresentation(mVignetteRep); 89 break; 90 } 91 computeEllipses(); 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 = mImageLoader.getOriginalBounds().width() / 2; 113 float cy = mImageLoader.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} 167