summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/ui/AdaptiveBackground.java
blob: 42cb2ccdb055138378b21c8c578b60f12fdd4be3 (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
/*
 * 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.Bitmap;
import android.graphics.Canvas;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;

import com.android.gallery3d.anim.FloatAnimation;

public class AdaptiveBackground extends GLView {

    private static final int BACKGROUND_WIDTH = 128;
    private static final int BACKGROUND_HEIGHT = 64;
    private static final int FILTERED_COLOR = 0xffaaaaaa;
    private static final int ANIMATION_DURATION = 500;

    private BasicTexture mOldBackground;
    private BasicTexture mBackground;

    private final Paint mPaint;
    private Bitmap mPendingBitmap;
    private final FloatAnimation mAnimation =
            new FloatAnimation(0, 1, ANIMATION_DURATION);

    public AdaptiveBackground() {
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setColorFilter(new LightingColorFilter(FILTERED_COLOR, 0));
        mPaint = paint;
    }

    public Bitmap getAdaptiveBitmap(Bitmap bitmap) {
        Bitmap target = Bitmap.createBitmap(
                BACKGROUND_WIDTH, BACKGROUND_HEIGHT, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int left = 0;
        int top = 0;
        if (width * BACKGROUND_HEIGHT > height * BACKGROUND_WIDTH) {
            float scale = (float) BACKGROUND_HEIGHT / height;
            canvas.scale(scale, scale);
            left = (BACKGROUND_WIDTH - (int) (width * scale + 0.5)) / 2;
        } else {
            float scale = (float) BACKGROUND_WIDTH / width;
            canvas.scale(scale, scale);
            top = (BACKGROUND_HEIGHT - (int) (height * scale + 0.5)) / 2;
        }
        canvas.drawBitmap(bitmap, left, top, mPaint);
        BoxBlurFilter.apply(target,
                BoxBlurFilter.MODE_REPEAT, BoxBlurFilter.MODE_CLAMP);
        return target;
    }

    private void startTransition(Bitmap bitmap) {
        BitmapTexture texture = new BitmapTexture(bitmap);
        if (mBackground == null) {
            mBackground = texture;
        } else {
            if (mOldBackground != null) mOldBackground.recycle();
            mOldBackground = mBackground;
            mBackground = texture;
            mAnimation.start();
        }
        invalidate();
    }

    public void setImage(Bitmap bitmap) {
        if (mAnimation.isActive()) {
            mPendingBitmap = bitmap;
        } else {
            startTransition(bitmap);
        }
    }

    public void setScrollPosition(int position) {
        if (mScrollX == position) return;
        mScrollX = position;
        invalidate();
    }

    @Override
    protected void render(GLCanvas canvas) {
        if (mBackground == null) return;

        int height = getHeight();
        float scale = (float) height / BACKGROUND_HEIGHT;
        int width = (int) (BACKGROUND_WIDTH * scale + 0.5f);
        int scroll = mScrollX;
        int start = (scroll / width) * width;

        if (mOldBackground == null) {
            for (int i = start, n = scroll + getWidth(); i < n; i += width) {
                mBackground.draw(canvas, i - scroll, 0, width, height);
            }
        } else {
            boolean moreAnimation =
                    mAnimation.calculate(canvas.currentAnimationTimeMillis());
            float ratio = mAnimation.get();
            for (int i = start, n = scroll + getWidth(); i < n; i += width) {
                canvas.drawMixed(mOldBackground,
                        mBackground, ratio, i - scroll, 0, width, height);
            }
            if (moreAnimation) {
                invalidate();
            } else if (mPendingBitmap != null) {
                startTransition(mPendingBitmap);
                mPendingBitmap = null;
            }
        }
    }
}