Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2012 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.gallery3d.ui;
     18 
     19 import android.content.Context;
     20 import android.graphics.Rect;
     21 
     22 import com.android.gallery3d.R;
     23 
     24 public abstract class AbstractSlotRenderer implements SlotView.SlotRenderer {
     25 
     26     private final ResourceTexture mVideoOverlay;
     27     private final ResourceTexture mVideoPlayIcon;
     28     private final NinePatchTexture mPanoramaBorder;
     29     private final NinePatchTexture mFramePressed;
     30     private final NinePatchTexture mFrameSelected;
     31     private FadeOutTexture mFramePressedUp;
     32 
     33     protected AbstractSlotRenderer(Context context) {
     34         mVideoOverlay = new ResourceTexture(context, R.drawable.ic_video_thumb);
     35         mVideoPlayIcon = new ResourceTexture(context, R.drawable.ic_gallery_play);
     36         mPanoramaBorder = new NinePatchTexture(context, R.drawable.ic_pan_thumb);
     37         mFramePressed = new NinePatchTexture(context, R.drawable.grid_pressed);
     38         mFrameSelected = new NinePatchTexture(context, R.drawable.grid_selected);
     39     }
     40 
     41     protected void drawContent(GLCanvas canvas,
     42             Texture content, int width, int height, int rotation) {
     43         canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
     44 
     45         if (rotation != 0) {
     46             canvas.translate(width / 2, height / 2);
     47             canvas.rotate(rotation, 0, 0, 1);
     48             canvas.translate(-width / 2, -height / 2);
     49             if (((rotation % 90) & 1) != 0) {
     50                 int temp = height;
     51                 height = width;
     52                 width = height;
     53             }
     54         }
     55 
     56         // Fit the content into the box
     57         float scale = Math.min(
     58                 (float) width / content.getWidth(),
     59                 (float) height / content.getHeight());
     60         canvas.scale(scale, scale, 1);
     61         content.draw(canvas, 0, 0);
     62 
     63         canvas.restore();
     64     }
     65 
     66     protected void drawVideoOverlay(GLCanvas canvas, int width, int height) {
     67         // Scale the video overlay to the height of the thumbnail and put it
     68         // on the left side.
     69         ResourceTexture v = mVideoOverlay;
     70         float scale = (float) height / v.getHeight();
     71         int w = Math.round(scale * v.getWidth());
     72         int h = Math.round(scale * v.getHeight());
     73         v.draw(canvas, 0, 0, w, h);
     74 
     75         int s = Math.min(width, height) / 6;
     76         mVideoPlayIcon.draw(canvas, (width - s) / 2, (height - s) / 2, s, s);
     77     }
     78 
     79     protected void drawPanoramaBorder(GLCanvas canvas, int width, int height) {
     80         float scale = (float) width / mPanoramaBorder.getWidth();
     81         int w = Math.round(scale * mPanoramaBorder.getWidth());
     82         int h = Math.round(scale * mPanoramaBorder.getHeight());
     83         // draw at the top
     84         mPanoramaBorder.draw(canvas, 0, 0, w, h);
     85         // draw at the bottom
     86         mPanoramaBorder.draw(canvas, 0, height - h, w, h);
     87     }
     88 
     89     protected boolean isPressedUpFrameFinished() {
     90         if (mFramePressedUp != null) {
     91             if (mFramePressedUp.isAnimating()) {
     92                 return false;
     93             } else {
     94                 mFramePressedUp = null;
     95             }
     96         }
     97         return true;
     98     }
     99 
    100     protected void drawPressedUpFrame(GLCanvas canvas, int width, int height) {
    101         if (mFramePressedUp == null) {
    102             mFramePressedUp = new FadeOutTexture(mFramePressed);
    103         }
    104         drawFrame(canvas, mFramePressed.getPaddings(), mFramePressedUp, 0, 0, width, height);
    105     }
    106 
    107     protected void drawPressedFrame(GLCanvas canvas, int width, int height) {
    108         drawFrame(canvas, mFramePressed.getPaddings(), mFramePressed, 0, 0, width, height);
    109     }
    110 
    111     protected void drawSelectedFrame(GLCanvas canvas, int width, int height) {
    112         drawFrame(canvas, mFrameSelected.getPaddings(), mFrameSelected, 0, 0, width, height);
    113     }
    114 
    115     protected static void drawFrame(GLCanvas canvas, Rect padding, Texture frame,
    116             int x, int y, int width, int height) {
    117         frame.draw(canvas, x - padding.left, y - padding.top, width + padding.left + padding.right,
    118                  height + padding.top + padding.bottom);
    119     }
    120 }
    121