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