• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.ui;
18 
19 import android.content.Context;
20 import android.opengl.Matrix;
21 
22 // EdgeView draws EdgeEffect (blue glow) at four sides of the view.
23 public class EdgeView extends GLView {
24     @SuppressWarnings("unused")
25     private static final String TAG = "EdgeView";
26 
27     public static final int INVALID_DIRECTION = -1;
28     public static final int TOP = 0;
29     public static final int LEFT = 1;
30     public static final int BOTTOM = 2;
31     public static final int RIGHT = 3;
32 
33     // Each edge effect has a transform matrix, and each matrix has 16 elements.
34     // We put all the elements in one array. These constants specify the
35     // starting index of each matrix.
36     private static final int TOP_M = TOP * 16;
37     private static final int LEFT_M = LEFT * 16;
38     private static final int BOTTOM_M = BOTTOM * 16;
39     private static final int RIGHT_M = RIGHT * 16;
40 
41     private EdgeEffect[] mEffect = new EdgeEffect[4];
42     private float[] mMatrix = new float[4 * 16];
43 
EdgeView(Context context)44     public EdgeView(Context context) {
45         for (int i = 0; i < 4; i++) {
46             mEffect[i] = new EdgeEffect(context);
47         }
48     }
49 
50     @Override
onLayout( boolean changeSize, int left, int top, int right, int bottom)51     protected void onLayout(
52             boolean changeSize, int left, int top, int right, int bottom) {
53         if (!changeSize) return;
54 
55         int w = right - left;
56         int h = bottom - top;
57         for (int i = 0; i < 4; i++) {
58             if ((i & 1) == 0) {  // top or bottom
59                 mEffect[i].setSize(w, h);
60             } else {  // left or right
61                 mEffect[i].setSize(h, w);
62             }
63         }
64 
65         // Set up transforms for the four edges. Without transforms an
66         // EdgeEffect draws the TOP edge from (0, 0) to (w, Y * h) where Y
67         // is some factor < 1. For other edges we need to move, rotate, and
68         // flip the effects into proper places.
69         Matrix.setIdentityM(mMatrix, TOP_M);
70         Matrix.setIdentityM(mMatrix, LEFT_M);
71         Matrix.setIdentityM(mMatrix, BOTTOM_M);
72         Matrix.setIdentityM(mMatrix, RIGHT_M);
73 
74         Matrix.rotateM(mMatrix, LEFT_M, 90, 0, 0, 1);
75         Matrix.scaleM(mMatrix, LEFT_M, 1, -1, 1);
76 
77         Matrix.translateM(mMatrix, BOTTOM_M, 0, h, 0);
78         Matrix.scaleM(mMatrix, BOTTOM_M, 1, -1, 1);
79 
80         Matrix.translateM(mMatrix, RIGHT_M, w, 0, 0);
81         Matrix.rotateM(mMatrix, RIGHT_M, 90, 0, 0, 1);
82     }
83 
84     @Override
render(GLCanvas canvas)85     protected void render(GLCanvas canvas) {
86         super.render(canvas);
87         boolean more = false;
88         for (int i = 0; i < 4; i++) {
89             canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
90             canvas.multiplyMatrix(mMatrix, i * 16);
91             more |= mEffect[i].draw(canvas);
92             canvas.restore();
93         }
94         if (more) {
95             invalidate();
96         }
97     }
98 
99     // Called when the content is pulled away from the edge.
100     // offset is in pixels. direction is one of {TOP, LEFT, BOTTOM, RIGHT}.
onPull(int offset, int direction)101     public void onPull(int offset, int direction) {
102         int fullLength = ((direction & 1) == 0) ? getWidth() : getHeight();
103         mEffect[direction].onPull((float)offset / fullLength);
104         if (!mEffect[direction].isFinished()) {
105             invalidate();
106         }
107     }
108 
109     // Call when the object is released after being pulled.
onRelease()110     public void onRelease() {
111         boolean more = false;
112         for (int i = 0; i < 4; i++) {
113             mEffect[i].onRelease();
114             more |= !mEffect[i].isFinished();
115         }
116         if (more) {
117             invalidate();
118         }
119     }
120 
121     // Call when the effect absorbs an impact at the given velocity.
122     // Used when a fling reaches the scroll boundary. velocity is in pixels
123     // per second. direction is one of {TOP, LEFT, BOTTOM, RIGHT}.
onAbsorb(int velocity, int direction)124     public void onAbsorb(int velocity, int direction) {
125         mEffect[direction].onAbsorb(velocity);
126         if (!mEffect[direction].isFinished()) {
127             invalidate();
128         }
129     }
130 }
131