• 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.graphics.Matrix;
21 import android.graphics.PointF;
22 import android.graphics.RectF;
23 import android.util.AttributeSet;
24 import android.view.View;
25 
26 /**
27  * Full-screen tool view that gets photo display bounds and maps positions on photo display bounds
28  * back to exact coordinates on photo.
29  */
30 abstract class FullscreenToolView extends View {
31 
32     protected final RectF displayBounds = new RectF();
33     private final Matrix photoMatrix = new Matrix();
34     private RectF photoBounds;
35 
FullscreenToolView(Context context, AttributeSet attrs)36     public FullscreenToolView(Context context, AttributeSet attrs) {
37         super(context, attrs);
38     }
39 
40     /**
41      * Photo bounds must be set before onSizeChanged() and all other instance methods are invoked.
42      */
setPhotoBounds(RectF photoBounds)43     public void setPhotoBounds(RectF photoBounds) {
44         this.photoBounds = photoBounds;
45     }
46 
47     @Override
onSizeChanged(int w, int h, int oldw, int oldh)48     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
49         super.onSizeChanged(w, h, oldw, oldh);
50 
51         displayBounds.setEmpty();
52         photoMatrix.reset();
53         if (photoBounds.isEmpty()) {
54             return;
55         }
56 
57         // Assumes photo-view is also full-screen as this tool-view and centers/scales photo to fit.
58         Matrix matrix = new Matrix();
59         if (matrix.setRectToRect(photoBounds, new RectF(0, 0, w, h), Matrix.ScaleToFit.CENTER)) {
60             matrix.mapRect(displayBounds, photoBounds);
61         }
62 
63         matrix.invert(photoMatrix);
64     }
65 
getPhotoWidth()66     protected float getPhotoWidth() {
67         return photoBounds.width();
68     }
69 
getPhotoHeight()70     protected float getPhotoHeight() {
71         return photoBounds.height();
72     }
73 
mapPhotoPoint(float x, float y, PointF dst)74     protected void mapPhotoPoint(float x, float y, PointF dst) {
75         if (photoBounds.isEmpty()) {
76             dst.set(0, 0);
77         } else {
78             float[] point = new float[] {x, y};
79             photoMatrix.mapPoints(point);
80             dst.set(point[0] / photoBounds.width(), point[1] / photoBounds.height());
81         }
82     }
83 
mapPhotoRect(RectF src, RectF dst)84     protected void mapPhotoRect(RectF src, RectF dst) {
85         if (photoBounds.isEmpty()) {
86             dst.setEmpty();
87         } else {
88             photoMatrix.mapRect(dst, src);
89             dst.set(dst.left / photoBounds.width(), dst.top / photoBounds.height(),
90                     dst.right / photoBounds.width(), dst.bottom / photoBounds.height());
91         }
92     }
93 }
94