1 /* 2 * Copyright (C) 2010 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 package com.android.gallery3d.ui; 17 18 import com.android.gallery3d.R; 19 import com.android.gallery3d.data.MediaObject; 20 21 import android.content.Context; 22 23 public abstract class IconDrawer extends SelectionDrawer { 24 private static final String TAG = "IconDrawer"; 25 private static final int LABEL_BACKGROUND_COLOR = 0x99000000; // 60% black 26 27 private final ResourceTexture mLocalSetIcon; 28 private final ResourceTexture mCameraIcon; 29 private final ResourceTexture mPicasaIcon; 30 private final ResourceTexture mMtpIcon; 31 private final NinePatchTexture mFramePressed; 32 private final NinePatchTexture mFrameSelected; 33 private final NinePatchTexture mDarkStrip; 34 private final NinePatchTexture mPanoramaBorder; 35 private final Texture mVideoOverlay; 36 private final Texture mVideoPlayIcon; 37 private final int mIconSize; 38 39 public static class IconDimension { 40 int x; 41 int y; 42 int width; 43 int height; 44 } 45 IconDrawer(Context context)46 public IconDrawer(Context context) { 47 mLocalSetIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_folder); 48 mCameraIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_camera); 49 mPicasaIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_picasa); 50 mMtpIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_ptp); 51 mVideoOverlay = new ResourceTexture(context, R.drawable.ic_video_thumb); 52 mVideoPlayIcon = new ResourceTexture(context, R.drawable.ic_gallery_play); 53 mPanoramaBorder = new NinePatchTexture(context, R.drawable.ic_pan_thumb); 54 mFramePressed = new NinePatchTexture(context, R.drawable.grid_pressed); 55 mFrameSelected = new NinePatchTexture(context, R.drawable.grid_selected); 56 mDarkStrip = new NinePatchTexture(context, R.drawable.dark_strip); 57 mIconSize = context.getResources().getDimensionPixelSize( 58 R.dimen.albumset_icon_size); 59 } 60 61 @Override prepareDrawing()62 public void prepareDrawing() { 63 } 64 drawIcon(GLCanvas canvas, int width, int height, int dataSourceType)65 protected IconDimension drawIcon(GLCanvas canvas, int width, int height, 66 int dataSourceType) { 67 ResourceTexture icon = getIcon(dataSourceType); 68 69 if (icon != null) { 70 IconDimension id = getIconDimension(icon, width, height); 71 icon.draw(canvas, id.x, id.y, id.width, id.height); 72 return id; 73 } 74 return null; 75 } 76 getIcon(int dataSourceType)77 protected ResourceTexture getIcon(int dataSourceType) { 78 ResourceTexture icon = null; 79 switch (dataSourceType) { 80 case DATASOURCE_TYPE_LOCAL: 81 icon = mLocalSetIcon; 82 break; 83 case DATASOURCE_TYPE_PICASA: 84 icon = mPicasaIcon; 85 break; 86 case DATASOURCE_TYPE_CAMERA: 87 icon = mCameraIcon; 88 break; 89 case DATASOURCE_TYPE_MTP: 90 icon = mMtpIcon; 91 break; 92 default: 93 break; 94 } 95 96 return icon; 97 } 98 getIconDimension(ResourceTexture icon, int width, int height)99 protected IconDimension getIconDimension(ResourceTexture icon, int width, 100 int height) { 101 IconDimension id = new IconDimension(); 102 float scale = (float) mIconSize / icon.getWidth(); 103 id.width = Math.round(scale * icon.getWidth()); 104 id.height = Math.round(scale * icon.getHeight()); 105 id.x = -width / 2; 106 id.y = (height + 1) / 2 - id.height; 107 return id; 108 } 109 drawMediaTypeOverlay(GLCanvas canvas, int mediaType, boolean isPanorama, int x, int y, int width, int height)110 protected void drawMediaTypeOverlay(GLCanvas canvas, int mediaType, 111 boolean isPanorama, int x, int y, int width, int height) { 112 if (mediaType == MediaObject.MEDIA_TYPE_VIDEO) { 113 drawVideoOverlay(canvas, x, y, width, height); 114 } 115 if (isPanorama) { 116 drawPanoramaBorder(canvas, x, y, width, height); 117 } 118 } 119 drawVideoOverlay(GLCanvas canvas, int x, int y, int width, int height)120 protected void drawVideoOverlay(GLCanvas canvas, int x, int y, 121 int width, int height) { 122 // Scale the video overlay to the height of the thumbnail and put it 123 // on the left side. 124 float scale = (float) height / mVideoOverlay.getHeight(); 125 int w = Math.round(scale * mVideoOverlay.getWidth()); 126 int h = Math.round(scale * mVideoOverlay.getHeight()); 127 mVideoOverlay.draw(canvas, x, y, w, h); 128 129 int side = Math.min(width, height) / 6; 130 mVideoPlayIcon.draw(canvas, -side / 2, -side / 2, side, side); 131 } 132 drawPanoramaBorder(GLCanvas canvas, int x, int y, int width, int height)133 protected void drawPanoramaBorder(GLCanvas canvas, int x, int y, 134 int width, int height) { 135 float scale = (float) width / mPanoramaBorder.getWidth(); 136 int w = Math.round(scale * mPanoramaBorder.getWidth()); 137 int h = Math.round(scale * mPanoramaBorder.getHeight()); 138 // draw at the top 139 mPanoramaBorder.draw(canvas, x, y, w, h); 140 // draw at the bottom 141 mPanoramaBorder.draw(canvas, x, y + width - h, w, h); 142 } 143 drawLabelBackground(GLCanvas canvas, int width, int height, int drawLabelBackground)144 protected void drawLabelBackground(GLCanvas canvas, int width, int height, 145 int drawLabelBackground) { 146 int x = -width / 2; 147 int y = (height + 1) / 2 - drawLabelBackground; 148 drawFrame(canvas, mDarkStrip, x, y, width, drawLabelBackground); 149 } 150 drawPressedFrame(GLCanvas canvas, int x, int y, int width, int height)151 protected void drawPressedFrame(GLCanvas canvas, int x, int y, int width, 152 int height) { 153 drawFrame(canvas, mFramePressed, x, y, width, height); 154 } 155 drawSelectedFrame(GLCanvas canvas, int x, int y, int width, int height)156 protected void drawSelectedFrame(GLCanvas canvas, int x, int y, int width, 157 int height) { 158 drawFrame(canvas, mFrameSelected, x, y, width, height); 159 } 160 161 @Override drawFocus(GLCanvas canvas, int width, int height)162 public void drawFocus(GLCanvas canvas, int width, int height) { 163 } 164 } 165