1 /* 2 * Copyright (C) 2013 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.wallpaperpicker; 18 19 import android.content.Context; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Rect; 23 import android.graphics.drawable.Drawable; 24 25 import com.android.gallery3d.glrenderer.BasicTexture; 26 import com.android.gallery3d.glrenderer.BitmapTexture; 27 import com.android.photos.views.TiledImageRenderer; 28 29 public class DrawableTileSource implements TiledImageRenderer.TileSource { 30 private static final int GL_SIZE_LIMIT = 2048; 31 // This must be no larger than half the size of the GL_SIZE_LIMIT 32 // due to decodePreview being allowed to be up to 2x the size of the target 33 public static final int MAX_PREVIEW_SIZE = GL_SIZE_LIMIT / 2; 34 35 private int mTileSize; 36 private int mPreviewSize; 37 private Drawable mDrawable; 38 private BitmapTexture mPreview; 39 40 public DrawableTileSource(Context context, Drawable d, int previewSize) { 41 mTileSize = TiledImageRenderer.suggestedTileSize(context); 42 mDrawable = d; 43 mPreviewSize = Math.min(previewSize, MAX_PREVIEW_SIZE); 44 } 45 46 @Override 47 public int getTileSize() { 48 return mTileSize; 49 } 50 51 @Override 52 public int getImageWidth() { 53 return mDrawable.getIntrinsicWidth(); 54 } 55 56 @Override 57 public int getImageHeight() { 58 return mDrawable.getIntrinsicHeight(); 59 } 60 61 @Override 62 public int getRotation() { 63 return 0; 64 } 65 66 @Override 67 public BasicTexture getPreview() { 68 if (mPreviewSize == 0) { 69 return null; 70 } 71 if (mPreview == null){ 72 float width = getImageWidth(); 73 float height = getImageHeight(); 74 while (width > MAX_PREVIEW_SIZE || height > MAX_PREVIEW_SIZE) { 75 width /= 2; 76 height /= 2; 77 } 78 Bitmap b = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888); 79 Canvas c = new Canvas(b); 80 mDrawable.setBounds(new Rect(0, 0, (int) width, (int) height)); 81 mDrawable.draw(c); 82 c.setBitmap(null); 83 mPreview = new BitmapTexture(b); 84 } 85 return mPreview; 86 } 87 88 @Override 89 public Bitmap getTile(int level, int x, int y, Bitmap bitmap) { 90 int tileSize = getTileSize(); 91 if (bitmap == null) { 92 bitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888); 93 } 94 Canvas c = new Canvas(bitmap); 95 Rect bounds = new Rect(0, 0, getImageWidth(), getImageHeight()); 96 bounds.offset(-x, -y); 97 mDrawable.setBounds(bounds); 98 mDrawable.draw(c); 99 c.setBitmap(null); 100 return bitmap; 101 } 102 } 103