• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.gallery3d.ui;
18 
19 import android.content.Context;
20 import android.graphics.Rect;
21 
22 public class Icon extends GLView {
23     private final BasicTexture mIcon;
24 
25     // The width and height requested by the user.
26     private int mReqWidth;
27     private int mReqHeight;
28 
Icon(Context context, int iconId, int width, int height)29     public Icon(Context context, int iconId, int width, int height) {
30         this(context, new ResourceTexture(context, iconId), width, height);
31     }
32 
Icon(Context context, BasicTexture icon, int width, int height)33     public Icon(Context context, BasicTexture icon, int width, int height) {
34         mIcon = icon;
35         mReqWidth = width;
36         mReqHeight = height;
37     }
38 
39     @Override
onMeasure(int widthSpec, int heightSpec)40     protected void onMeasure(int widthSpec, int heightSpec) {
41         MeasureHelper.getInstance(this)
42                 .setPreferredContentSize(mReqWidth, mReqHeight)
43                 .measure(widthSpec, heightSpec);
44     }
45 
46     @Override
render(GLCanvas canvas)47     protected void render(GLCanvas canvas) {
48         Rect p = mPaddings;
49 
50         int width = getWidth() - p.left - p.right;
51         int height = getHeight() - p.top - p.bottom;
52 
53         // Draw the icon in the center of the space
54         int xoffset = p.left + (width - mReqWidth) / 2;
55         int yoffset = p.top + (height - mReqHeight) / 2;
56 
57         mIcon.draw(canvas, xoffset, yoffset, mReqWidth, mReqHeight);
58     }
59 }
60