Home | History | Annotate | Download | only in ui
      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.camera.ui;
     18 
     19 import static com.android.camera.ui.GLRootView.dpToPixel;
     20 import android.content.Context;
     21 import android.graphics.Rect;
     22 
     23 import com.android.camera.R;
     24 
     25 import javax.microedition.khronos.opengles.GL11;
     26 
     27 public class GLOptionHeader extends GLView {
     28     private static final int FONT_COLOR = 0xFF979797;
     29     private static final float FONT_SIZE = 12;
     30     private static final int HORIZONTAL_PADDINGS = 4;
     31     private static final int VERTICAL_PADDINGS = 2;
     32 
     33     private static int sHorizontalPaddings = -1;
     34     private static int sVerticalPaddings;
     35 
     36     private final StringTexture mTitle;
     37     private NinePatchTexture mBackground;
     38 
     39     private static void initializeStaticVariables(Context context) {
     40         if (sHorizontalPaddings >= 0) return;
     41         sHorizontalPaddings = dpToPixel(context, HORIZONTAL_PADDINGS);
     42         sVerticalPaddings = dpToPixel(context, VERTICAL_PADDINGS);
     43     }
     44 
     45     public GLOptionHeader(Context context, String title) {
     46         initializeStaticVariables(context);
     47 
     48         float fontSize = GLRootView.dpToPixel(context, FONT_SIZE);
     49         mTitle = StringTexture.newInstance(title, fontSize, FONT_COLOR);
     50         setBackground(new NinePatchTexture(
     51                 context, R.drawable.optionheader_background));
     52         setPaddings(sHorizontalPaddings,
     53                 sVerticalPaddings, sHorizontalPaddings, sVerticalPaddings);
     54     }
     55 
     56     public void setBackground(NinePatchTexture background) {
     57         if (mBackground == background) return;
     58         mBackground = background;
     59         invalidate();
     60     }
     61 
     62     @Override
     63     protected void onMeasure(int widthSpec, int heightSpec) {
     64         new MeasureHelper(this)
     65                 .setPreferredContentSize(mTitle.getWidth(), mTitle.getHeight())
     66                 .measure(widthSpec, heightSpec);
     67     }
     68 
     69     @Override
     70     protected void render(GLRootView root, GL11 gl) {
     71         if (mBackground != null) {
     72             mBackground.setSize(getWidth(), getHeight());
     73             mBackground.draw(root, 0, 0);
     74         }
     75         Rect p = mPaddings;
     76         mTitle.draw(root, p.left, p.top);
     77     }
     78 }
     79