summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/ui/PhotoFallbackEffect.java
blob: 3ca09ab968d4f5ebb72f25e81e0e72972e19dfcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.ui;

import android.graphics.Rect;
import android.graphics.RectF;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

import com.android.gallery3d.anim.Animation;
import com.android.gallery3d.data.Path;
import com.android.gallery3d.ui.AlbumSlotRenderer.SlotFilter;

import java.util.ArrayList;

public class PhotoFallbackEffect extends Animation implements SlotFilter {

    private static final int ANIM_DURATION = 300;
    private static final Interpolator ANIM_INTERPOLATE = new DecelerateInterpolator(1.5f);

    public static class Entry {
        public int index;
        public Path path;
        public Rect source;
        public Rect dest;
        public RawTexture texture;

        public Entry(Path path, Rect source, RawTexture texture) {
            this.path = path;
            this.source = source;
            this.texture = texture;
        }
    }

    public interface PositionProvider {
        public Rect getPosition(int index);
        public int getItemIndex(Path path);
    }

    private RectF mSource = new RectF();
    private RectF mTarget = new RectF();
    private float mProgress;
    private PositionProvider mPositionProvider;

    private ArrayList<Entry> mList = new ArrayList<Entry>();

    public PhotoFallbackEffect() {
        setDuration(ANIM_DURATION);
        setInterpolator(ANIM_INTERPOLATE);
    }

    public void addEntry(Path path, Rect rect, RawTexture texture) {
        mList.add(new Entry(path, rect, texture));
    }

    public Entry getEntry(Path path) {
        for (int i = 0, n = mList.size(); i < n; ++i) {
            Entry entry = mList.get(i);
            if (entry.path == path) return entry;
        }
        return null;
    }

    public boolean draw(GLCanvas canvas) {
        boolean more = calculate(AnimationTime.get());
        for (int i = 0, n = mList.size(); i < n; ++i) {
            Entry entry = mList.get(i);
            if (entry.index < 0) continue;
            entry.dest = mPositionProvider.getPosition(entry.index);
            drawEntry(canvas, entry);
        }
        return more;
    }

    private void drawEntry(GLCanvas canvas, Entry entry) {
        if (!entry.texture.isLoaded()) return;

        int w = entry.texture.getWidth();
        int h = entry.texture.getHeight();

        Rect s = entry.source;
        Rect d = entry.dest;

        // the following calculation is based on d.width() == d.height()

        float p = mProgress;

        float fullScale = (float) d.height() / Math.min(s.width(), s.height());
        float scale = fullScale * p + 1 * (1 - p);

        float cx = d.centerX() * p + s.centerX() * (1 - p);
        float cy = d.centerY() * p + s.centerY() * (1 - p);

        float ch = s.height() * scale;
        float cw = s.width() * scale;

        if (w > h) {
            // draw the center part
            mTarget.set(cx - ch / 2, cy - ch / 2, cx + ch / 2, cy + ch / 2);
            mSource.set((w - h) / 2, 0, (w + h) / 2, h);
            canvas.drawTexture(entry.texture, mSource, mTarget);

            canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
            canvas.multiplyAlpha(1 - p);

            // draw the left part
            mTarget.set(cx - cw / 2, cy - ch / 2, cx - ch / 2, cy + ch / 2);
            mSource.set(0, 0, (w - h) / 2, h);
            canvas.drawTexture(entry.texture, mSource, mTarget);

            // draw the right part
            mTarget.set(cx + ch / 2, cy - ch / 2, cx + cw / 2, cy + ch / 2);
            mSource.set((w + h) / 2, 0, w, h);
            canvas.drawTexture(entry.texture, mSource, mTarget);

            canvas.restore();
        } else {
            // draw the center part
            mTarget.set(cx - cw / 2, cy - cw / 2, cx + cw / 2, cy + cw / 2);
            mSource.set(0, (h - w) / 2, w, (h + w) / 2);
            canvas.drawTexture(entry.texture, mSource, mTarget);

            canvas.save(GLCanvas.SAVE_FLAG_ALPHA);
            canvas.multiplyAlpha(1 - p);

            // draw the upper part
            mTarget.set(cx - cw / 2, cy - ch / 2, cx + cw / 2, cy - cw / 2);
            mSource.set(0, 0, w, (h - w) / 2);
            canvas.drawTexture(entry.texture, mSource, mTarget);

            // draw the bottom part
            mTarget.set(cx - cw / 2, cy + cw / 2, cx + cw / 2, cy + ch / 2);
            mSource.set(0, (w + h) / 2, w, h);
            canvas.drawTexture(entry.texture, mSource, mTarget);

            canvas.restore();
        }
    }

    @Override
    protected void onCalculate(float progress) {
        mProgress = progress;
    }

    public void setPositionProvider(PositionProvider provider) {
        mPositionProvider = provider;
        if (mPositionProvider != null) {
            for (int i = 0, n = mList.size(); i < n; ++i) {
                Entry entry = mList.get(i);
                entry.index = mPositionProvider.getItemIndex(entry.path);
            }
        }
    }

    @Override
    public boolean acceptSlot(int index) {
        for (int i = 0, n = mList.size(); i < n; ++i) {
            Entry entry = mList.get(i);
            if (entry.index == index) return false;
        }
        return true;
    }
}