• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.gallery3d.photoeditor.actions;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import com.android.gallery3d.photoeditor.PhotoView;
23 import com.android.gallery3d.photoeditor.filters.RotateFilter;
24 
25 /**
26  * An action handling rotate effect.
27  */
28 public class RotateAction extends EffectAction {
29 
30     private static final float DEFAULT_ANGLE = 0.0f;
31     private static final float DEFAULT_ROTATE_SPAN = 360.0f;
32 
RotateAction(Context context, AttributeSet attrs)33     public RotateAction(Context context, AttributeSet attrs) {
34         super(context, attrs);
35     }
36 
37     @Override
prepare()38     public void prepare() {
39         // Disable outputting rotated results and directly rotate photo-view for animations.
40         final RotateFilter filter = new RotateFilter();
41         disableFilterOutput();
42 
43         final RotateView rotateView = toolKit.addRotateView();
44         rotateView.setOnRotateChangeListener(new RotateView.OnRotateChangeListener() {
45 
46             float rotateDegrees;
47             Runnable queuedTransform;
48             PhotoView photoView = toolKit.getPhotoView();
49 
50             @Override
51             public void onAngleChanged(float degrees, boolean fromUser) {
52                 if (fromUser) {
53                     rotateDegrees = degrees;
54                     transformPhotoView(rotateDegrees);
55                     notifyChanged(filter);
56                 }
57             }
58 
59             @Override
60             public void onStartTrackingTouch() {
61                 // no-op
62             }
63 
64             @Override
65             public void onStopTrackingTouch() {
66                 // Round rotate degrees to multiples of 90 degrees.
67                 if (rotateDegrees % 90 != 0) {
68                     rotateDegrees = Math.round(rotateDegrees / 90) * 90;
69                 }
70                 transformPhotoView(rotateDegrees);
71                 rotateView.setRotatedAngle(rotateDegrees);
72                 filter.setAngle(rotateDegrees);
73                 notifyChanged(filter);
74             }
75 
76             private void transformPhotoView(final float degrees) {
77                 // Remove the outdated rotation change before queuing a new one.
78                 if (queuedTransform != null) {
79                     photoView.remove(queuedTransform);
80                 }
81                 queuedTransform = new Runnable() {
82 
83                     @Override
84                     public void run() {
85                         photoView.rotatePhoto(degrees);
86                     }
87                 };
88                 photoView.queue(queuedTransform);
89             }
90         });
91         rotateView.setRotatedAngle(DEFAULT_ANGLE);
92         rotateView.setRotateSpan(DEFAULT_ROTATE_SPAN);
93     }
94 }
95