1 /* 2 * Copyright (C) 2010 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.photoeditor.actions; 18 19 import android.graphics.Path; 20 import android.graphics.RectF; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 import com.android.photoeditor.FilterStack; 25 import com.android.photoeditor.R; 26 import com.android.photoeditor.filters.CropFilter; 27 28 /** 29 * An action handling crop effect. 30 */ 31 public class CropAction extends FilterAction { 32 33 private static final float DEFAULT_CROP = 0.2f; 34 private CropFilter filter; 35 CropAction(FilterStack filterStack, ViewGroup tools)36 public CropAction(FilterStack filterStack, ViewGroup tools) { 37 super(filterStack, tools, R.string.crop_tooltip); 38 } 39 mapPhotoBounds(RectF bounds, RectF photoBounds)40 private RectF mapPhotoBounds(RectF bounds, RectF photoBounds) { 41 return new RectF((bounds.left - photoBounds.left) / photoBounds.width(), 42 (bounds.top - photoBounds.top) / photoBounds.height(), 43 (bounds.right - photoBounds.left) / photoBounds.width(), 44 (bounds.bottom - photoBounds.top) / photoBounds.height()); 45 } 46 47 @Override onBegin()48 public void onBegin() { 49 filter = new CropFilter(); 50 51 final RectF photoBounds = photoView.getPhotoDisplayBounds(); 52 cropView.setPhotoBounds(new RectF(photoBounds)); 53 cropView.setOnCropChangeListener(new CropView.OnCropChangeListener() { 54 55 @Override 56 public void onCropChanged(RectF bounds, boolean fromUser) { 57 if (fromUser) { 58 filter.setCropBounds(mapPhotoBounds(bounds, photoBounds)); 59 notifyFilterChanged(filter, false); 60 } 61 } 62 }); 63 RectF cropBounds = new RectF(photoBounds); 64 cropBounds.inset(photoBounds.width() * DEFAULT_CROP, photoBounds.height() * DEFAULT_CROP); 65 cropView.setCropBounds(cropBounds); 66 if (!cropView.fullPhotoCropped()) { 67 filter.setCropBounds(mapPhotoBounds(cropBounds, photoBounds)); 68 notifyFilterChanged(filter, false); 69 } 70 cropView.setVisibility(View.VISIBLE); 71 } 72 73 @Override onEnd()74 public void onEnd() { 75 notifyFilterChanged(filter, true); 76 } 77 } 78