summaryrefslogtreecommitdiffstats
path: root/WallpaperPicker/src/com/android/launcher3/DrawableTileSource.java
blob: c1f2eff0f42cb0b987b0c62b0a0fe34dd75b6664 (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
/*
 * Copyright (C) 2013 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.launcher3;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.android.gallery3d.glrenderer.BasicTexture;
import com.android.gallery3d.glrenderer.BitmapTexture;
import com.android.photos.views.TiledImageRenderer;

public class DrawableTileSource implements TiledImageRenderer.TileSource {
    private static final int GL_SIZE_LIMIT = 2048;
    // This must be no larger than half the size of the GL_SIZE_LIMIT
    // due to decodePreview being allowed to be up to 2x the size of the target
    public static final int MAX_PREVIEW_SIZE = GL_SIZE_LIMIT / 2;

    private int mTileSize;
    private int mPreviewSize;
    private Drawable mDrawable;
    private BitmapTexture mPreview;

    public DrawableTileSource(Context context, Drawable d, int previewSize) {
        mTileSize = TiledImageRenderer.suggestedTileSize(context);
        mDrawable = d;
        mPreviewSize = Math.min(previewSize, MAX_PREVIEW_SIZE);
    }

    @Override
    public int getTileSize() {
        return mTileSize;
    }

    @Override
    public int getImageWidth() {
        return mDrawable.getIntrinsicWidth();
    }

    @Override
    public int getImageHeight() {
        return mDrawable.getIntrinsicHeight();
    }

    @Override
    public int getRotation() {
        return 0;
    }

    @Override
    public BasicTexture getPreview() {
        if (mPreviewSize == 0) {
            return null;
        }
        if (mPreview == null){
            float width = getImageWidth();
            float height = getImageHeight();
            while (width > MAX_PREVIEW_SIZE || height > MAX_PREVIEW_SIZE) {
                width /= 2;
                height /= 2;
            }
            Bitmap b = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            mDrawable.setBounds(new Rect(0, 0, (int) width, (int) height));
            mDrawable.draw(c);
            c.setBitmap(null);
            mPreview = new BitmapTexture(b);
        }
        return mPreview;
    }

    @Override
    public Bitmap getTile(int level, int x, int y, Bitmap bitmap) {
        int tileSize = getTileSize();
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888);
        }
        Canvas c = new Canvas(bitmap);
        Rect bounds = new Rect(0, 0, getImageWidth(), getImageHeight());
        bounds.offset(-x, -y);
        mDrawable.setBounds(bounds);
        mDrawable.draw(c);
        c.setBitmap(null);
        return bitmap;
    }
}