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