Home | History | Annotate | Download | only in actions
      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.Matrix;
     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.RectUtils;
     27 import com.android.photoeditor.filters.RotateFilter;
     28 
     29 /**
     30  * An action handling rotate effect.
     31  */
     32 public class RotateAction extends FilterAction {
     33 
     34     private static final float DEFAULT_ANGLE = 0.0f;
     35     private static final float DEFAULT_ROTATE_SPAN = 360.0f;
     36 
     37     private RotateFilter filter;
     38     private float rotateDegrees;
     39 
     40     public RotateAction(FilterStack filterStack, ViewGroup tools) {
     41         super(filterStack, tools, R.string.rotate_tooltip);
     42     }
     43 
     44     @Override
     45     public void onBegin() {
     46         filter = new RotateFilter();
     47         rotateDegrees = 0;
     48 
     49         final Matrix matrix = new Matrix();
     50         final RectF rotateBounds = new RectF();
     51         final RectF photoBounds = photoView.getPhotoBounds();
     52 
     53         // Directly transform photo-view instead of waiting for top-filter output callback.
     54         rotateView.setOnAngleChangeListener(new RotateView.OnRotateChangeListener() {
     55 
     56             @Override
     57             public void onAngleChanged(float degrees, boolean fromUser){
     58                 if (fromUser) {
     59                     rotateDegrees = degrees;
     60                     filter.setAngle(degrees);
     61                     notifyFilterChanged(filter, false);
     62                     transformPhotoView(degrees);
     63                 }
     64             }
     65 
     66             @Override
     67             public void onStartTrackingTouch() {
     68                 // no-op
     69             }
     70 
     71             @Override
     72             public void onStopTrackingTouch() {
     73                 if (roundFilterRotationDegrees()) {
     74                     notifyFilterChanged(filter, false);
     75                     transformPhotoView(rotateDegrees);
     76                     rotateView.setRotatedAngle(rotateDegrees);
     77                 }
     78             }
     79 
     80             private void transformPhotoView(float degrees) {
     81                 matrix.reset();
     82                 rotateBounds.set(photoBounds);
     83                 RectUtils.postRotateMatrix(degrees, rotateBounds, matrix);
     84                 float scale = RectUtils.getDisplayScale(rotateBounds, photoView);
     85                 matrix.postScale(scale, scale);
     86                 photoView.transformDisplay(matrix);
     87             }
     88         });
     89         rotateView.setGridBounds(null);
     90         rotateView.setRotatedAngle(DEFAULT_ANGLE);
     91         rotateView.setRotateSpan(DEFAULT_ROTATE_SPAN);
     92         rotateView.setVisibility(View.VISIBLE);
     93     }
     94 
     95     @Override
     96     public void onEnd() {
     97         // Round the current rotation degrees in case rotation tracking has not stopped yet.
     98         roundFilterRotationDegrees();
     99         notifyFilterChanged(filter, true);
    100     }
    101 
    102     /**
    103      * Rounds filter rotation degrees to multiples of 90 degrees.
    104      *
    105      * @return true if the rotation degrees has been changed.
    106      */
    107     private boolean roundFilterRotationDegrees() {
    108         if (rotateDegrees % 90 != 0) {
    109             rotateDegrees = Math.round(rotateDegrees / 90) * 90;
    110             filter.setAngle(rotateDegrees);
    111             return true;
    112         }
    113         return false;
    114     }
    115 }
    116