Home | History | Annotate | Download | only in imageshow
      1 /*
      2  * Copyright (C) 2012 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.filtershow.imageshow;
     18 
     19 import android.content.Context;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Canvas;
     22 import android.graphics.Paint;
     23 import android.util.AttributeSet;
     24 
     25 import com.android.gallery3d.R;
     26 import com.android.gallery3d.filtershow.editors.EditorRotate;
     27 
     28 public class ImageRotate extends ImageGeometry {
     29 
     30     private float mBaseAngle = 0;
     31     private float mAngle = 0;
     32 
     33     private final boolean mSnapToNinety = true;
     34     private EditorRotate mEditorRotate;
     35     private static final String LOGTAG = "ImageRotate";
     36 
     37     public ImageRotate(Context context, AttributeSet attrs) {
     38         super(context, attrs);
     39     }
     40 
     41     public ImageRotate(Context context) {
     42         super(context);
     43     }
     44 
     45     @Override
     46     public String getName() {
     47         return getContext().getString(R.string.rotate);
     48     }
     49 
     50     private static final Paint gPaint = new Paint();
     51 
     52     private void computeValue() {
     53         float angle = getCurrentTouchAngle();
     54         mAngle = (mBaseAngle - angle) % 360;
     55     }
     56 
     57     public void rotate() {
     58         mAngle += 90;
     59         mAngle = snappedAngle(mAngle);
     60         mAngle %= 360;
     61         setLocalRotation(mAngle);
     62     }
     63 
     64     @Override
     65     protected void setActionDown(float x, float y) {
     66         super.setActionDown(x, y);
     67         mBaseAngle = mAngle = getLocalRotation();
     68     }
     69 
     70     @Override
     71     protected void setActionMove(float x, float y) {
     72         super.setActionMove(x, y);
     73         computeValue();
     74         setLocalRotation(mAngle % 360);
     75     }
     76 
     77     @Override
     78     protected void setActionUp() {
     79         super.setActionUp();
     80         if (mSnapToNinety) {
     81             setLocalRotation(snappedAngle(mAngle % 360));
     82         }
     83     }
     84 
     85     @Override
     86     public int getLocalValue() {
     87         return constrainedRotation(snappedAngle(getLocalRotation()));
     88     }
     89 
     90     @Override
     91     protected void drawShape(Canvas canvas, Bitmap image) {
     92         gPaint.setAntiAlias(true);
     93         gPaint.setARGB(255, 255, 255, 255);
     94         drawTransformedCropped(canvas, image, gPaint);
     95     }
     96 
     97     public void setEditor(EditorRotate editorRotate) {
     98         mEditorRotate = editorRotate;
     99     }
    100 }
    101