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.graphics.RectF;
     24 import android.util.AttributeSet;
     25 
     26 import com.android.gallery3d.R;
     27 import com.android.gallery3d.filtershow.editors.EditorStraighten;
     28 
     29 public class ImageStraighten extends ImageGeometry {
     30 
     31     private float mBaseAngle = 0;
     32     private float mAngle = 0;
     33     private EditorStraighten mEditorStraighten;
     34 
     35     private static final String LOGTAG = "ImageStraighten";
     36     private static final Paint gPaint = new Paint();
     37     public ImageStraighten(Context context) {
     38         super(context);
     39     }
     40 
     41     public ImageStraighten(Context context, AttributeSet attrs) {
     42         super(context, attrs);
     43     }
     44 
     45     @Override
     46     public String getName() {
     47         return getContext().getString(R.string.straighten);
     48     }
     49 
     50     @Override
     51     protected void setActionDown(float x, float y) {
     52         super.setActionDown(x, y);
     53         mBaseAngle = mAngle = getLocalStraighten();
     54     }
     55 
     56     private void setCropToStraighten(){
     57         setLocalCropBounds(getUntranslatedStraightenCropBounds(getLocalPhotoBounds(),
     58                 getLocalStraighten()));
     59     }
     60 
     61     @Override
     62     protected void setActionMove(float x, float y) {
     63         super.setActionMove(x, y);
     64         computeValue();
     65         setLocalStraighten(mAngle);
     66         setCropToStraighten();
     67     }
     68 
     69     private void computeValue() {
     70         float angle = getCurrentTouchAngle();
     71         mAngle = (mBaseAngle - angle) % 360;
     72         mAngle = Math.max(MIN_STRAIGHTEN_ANGLE, mAngle);
     73         mAngle = Math.min(MAX_STRAIGHTEN_ANGLE, mAngle);
     74     }
     75 
     76     @Override
     77     protected void lostVisibility() {
     78         saveAndSetPreset();
     79     }
     80 
     81     @Override
     82     protected void gainedVisibility(){
     83         setCropToStraighten();
     84     }
     85 
     86     @Override
     87     protected void setActionUp() {
     88         super.setActionUp();
     89         setCropToStraighten();
     90     }
     91 
     92     @Override
     93     public void onNewValue(int value) {
     94         setLocalStraighten(GeometryMath.clamp(value, MIN_STRAIGHTEN_ANGLE, MAX_STRAIGHTEN_ANGLE));
     95         invalidate();
     96     }
     97 
     98     @Override
     99     protected int getLocalValue() {
    100         return (int) getLocalStraighten();
    101     }
    102 
    103     @Override
    104     protected void drawShape(Canvas canvas, Bitmap image) {
    105         float [] o = {0, 0};
    106         RectF bounds = drawTransformed(canvas, image, gPaint, o);
    107 
    108         // Draw the grid
    109         gPaint.setARGB(255, 255, 255, 255);
    110         gPaint.setStrokeWidth(3);
    111         gPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    112 
    113         RectF display = getLocalDisplayBounds();
    114         float dWidth = display.width();
    115         float dHeight = display.height();
    116 
    117         if (mMode == MODES.MOVE) {
    118             canvas.save();
    119             canvas.clipRect(bounds);
    120 
    121             int n = 16;
    122             float step = dWidth / n;
    123             float p = 0;
    124             for (int i = 1; i < n; i++) {
    125                 p = i * step;
    126                 gPaint.setARGB(60, 255, 255, 255);
    127                 canvas.drawLine(p, 0, p, dHeight, gPaint);
    128                 canvas.drawLine(0, p, dWidth, p, gPaint);
    129             }
    130             canvas.restore();
    131         }
    132     }
    133 
    134     public void setEditor(EditorStraighten editorStraighten) {
    135         mEditorStraighten = editorStraighten;
    136     }
    137 
    138 }
    139