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 android.content.Context;
     20 import android.util.Log;
     21 
     22 import com.android.camera.R;
     23 import com.android.camera.ui.ZoomControllerListener;
     24 
     25 import java.text.DecimalFormat;
     26 
     27 class ZoomIndicator extends AbstractIndicator {
     28     private static final DecimalFormat sZoomFormat = new DecimalFormat("#.#x");
     29     private static final float FONT_SIZE = 18;
     30     private static final int FONT_COLOR = 0xA8FFFFFF;
     31     private static final int COLOR_OPTION_HEADER = 0xFF2B2B2B;
     32 
     33     protected static final String TAG = "ZoomIndicator";
     34 
     35     private final float mFontSize;
     36 
     37     private ZoomController mZoomController;
     38     private LinearLayout mPopupContent;
     39     private ZoomControllerListener mZoomListener;
     40     private int mZoomIndex = 0;
     41     private int mDrawIndex = -1;
     42     private float mZoomRatios[];
     43 
     44     private StringTexture mTitle;
     45 
     46     public ZoomIndicator(Context context) {
     47         super(context);
     48         mFontSize = GLRootView.dpToPixel(context, FONT_SIZE);
     49     }
     50 
     51     @Override
     52     protected void onMeasure(int widthSpec, int heightSpec) {
     53         int maxWidth = 0;
     54         int maxHeight = 0;
     55         int n = mZoomRatios == null ? 0: mZoomRatios.length;
     56         for (int i = 0; i < n; ++i) {
     57             float value = mZoomRatios[i];
     58             BitmapTexture tex = StringTexture.newInstance(
     59                     sZoomFormat.format(value), mFontSize, FONT_COLOR);
     60             if (maxWidth < tex.getWidth()) maxWidth = tex.getWidth();
     61             if (maxHeight < tex.getHeight()) maxHeight = tex.getHeight();
     62         }
     63         new MeasureHelper(this)
     64                 .setPreferredContentSize(maxWidth, maxHeight)
     65                 .measure(widthSpec, heightSpec);
     66     }
     67 
     68     @Override
     69     protected BitmapTexture getIcon() {
     70         if (mDrawIndex != mZoomIndex) {
     71             mDrawIndex = mZoomIndex;
     72             if (mTitle != null) mTitle.deleteFromGL();
     73             float value = mZoomRatios[mZoomIndex];
     74             mTitle = StringTexture.newInstance(
     75                     sZoomFormat.format(value), mFontSize, FONT_COLOR);
     76         }
     77         return mTitle;
     78     }
     79 
     80     @Override
     81     public GLView getPopupContent() {
     82         if (mZoomController == null) {
     83             Context context = getGLRootView().getContext();
     84             mZoomController = new ZoomController(context);
     85             mZoomController.setAvailableZoomRatios(mZoomRatios);
     86             mZoomController.setPaddings(15, 6, 15, 6);
     87 
     88             mPopupContent = new LinearLayout();
     89             GLOptionHeader header = new GLOptionHeader(context,
     90                     context.getString(R.string.zoom_control_title));
     91             header.setBackground(new ColorTexture(COLOR_OPTION_HEADER));
     92             header.setPaddings(6, 3, 6, 3);
     93             mPopupContent.addComponent(header);
     94             mPopupContent.addComponent(mZoomController);
     95 
     96             mZoomController.setZoomListener(new MyZoomListener());
     97             mZoomController.setZoomIndex(mZoomIndex);
     98         }
     99         return mPopupContent;
    100     }
    101 
    102     @Override
    103     public void overrideSettings(String key, String settings) {
    104         // do nothing
    105     }
    106 
    107     @Override
    108     public void reloadPreferences() {
    109         // do nothing
    110     }
    111 
    112     public void setZoomRatios(float[] ratios) {
    113         mZoomRatios = ratios;
    114         mDrawIndex = -1;
    115         invalidate();
    116     }
    117 
    118     private class MyZoomListener implements ZoomControllerListener {
    119         public void onZoomChanged(int index, float value, boolean isMoving) {
    120             if (mZoomListener != null) {
    121                 mZoomListener.onZoomChanged(index, value, isMoving);
    122             }
    123             if (mZoomIndex != index) onZoomIndexChanged(index);
    124         }
    125     }
    126 
    127     private void onZoomIndexChanged(int index) {
    128         if (mZoomIndex == index) return;
    129         mZoomIndex = index;
    130         invalidate();
    131     }
    132 
    133     public void setZoomListener(ZoomControllerListener listener) {
    134         mZoomListener = listener;
    135     }
    136 
    137     public void setZoomIndex(int index) {
    138         if (mZoomIndex == index) return;
    139         if (mZoomController != null) {
    140             mZoomController.setZoomIndex(index);
    141         } else {
    142             onZoomIndexChanged(index);
    143         }
    144     }
    145 }
    146