ImageStraighten.java revision 87835aa01412f9ba20eb57ac0cbbad3a19b48623
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
17package com.android.gallery3d.filtershow.imageshow;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.graphics.RectF;
24import android.util.AttributeSet;
25
26import com.android.gallery3d.R;
27import com.android.gallery3d.filtershow.editors.EditorStraighten;
28
29public 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        if (getPanelController() != null) {
96            getPanelController().onNewValue((int) getLocalStraighten());
97        }
98        invalidate();
99    }
100
101    @Override
102    protected int getLocalValue() {
103        return (int) getLocalStraighten();
104    }
105
106    @Override
107    protected void drawShape(Canvas canvas, Bitmap image) {
108        float [] o = {0, 0};
109        RectF bounds = drawTransformed(canvas, image, gPaint, o);
110
111        // Draw the grid
112        gPaint.setARGB(255, 255, 255, 255);
113        gPaint.setStrokeWidth(3);
114        gPaint.setStyle(Paint.Style.FILL_AND_STROKE);
115
116        RectF display = getLocalDisplayBounds();
117        float dWidth = display.width();
118        float dHeight = display.height();
119
120        if (mMode == MODES.MOVE) {
121            canvas.save();
122            canvas.clipRect(bounds);
123
124            int n = 16;
125            float step = dWidth / n;
126            float p = 0;
127            for (int i = 1; i < n; i++) {
128                p = i * step;
129                gPaint.setARGB(60, 255, 255, 255);
130                canvas.drawLine(p, 0, p, dHeight, gPaint);
131                canvas.drawLine(0, p, dWidth, p, gPaint);
132            }
133            canvas.restore();
134        }
135    }
136
137    public void setEditor(EditorStraighten editorStraighten) {
138        mEditorStraighten = editorStraighten;
139    }
140
141}
142