1 /* 2 * Copyright (C) 2014 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.graphics.Canvas; 21 import android.graphics.drawable.Drawable; 22 import android.graphics.drawable.GradientDrawable; 23 import android.util.AttributeSet; 24 import android.view.View; 25 26 import com.android.camera2.R; 27 28 /** 29 * This class encapsulates the logic of drawing different states of the icon in 30 * mode drawer for when it is highlighted (to indicate the current module), or when 31 * it is selected by the user. It handles the internal state change like a state 32 * list drawable. The advantage over a state list drawable is that in the class 33 * multiple states can be rendered using the same drawable with some color modification, 34 * whereas a state list drawable would require a different drawable for each state. 35 */ 36 public class ModeIconView extends View { 37 private final GradientDrawable mBackground; 38 39 private final int mIconBackgroundSize; 40 private int mHighlightColor; 41 private final int mBackgroundDefaultColor; 42 private final int mIconDrawableSize; 43 private Drawable mIconDrawable = null; 44 ModeIconView(Context context, AttributeSet attrs)45 public ModeIconView(Context context, AttributeSet attrs) { 46 super(context, attrs); 47 mBackgroundDefaultColor = getResources().getColor(R.color.mode_selector_icon_background); 48 mIconBackgroundSize = getResources().getDimensionPixelSize( 49 R.dimen.mode_selector_icon_block_width); 50 mBackground = (GradientDrawable) getResources() 51 .getDrawable(R.drawable.mode_icon_background).mutate(); 52 mBackground.setBounds(0, 0, mIconBackgroundSize, mIconBackgroundSize); 53 mIconDrawableSize = getResources().getDimensionPixelSize( 54 R.dimen.mode_selector_icon_drawable_size); 55 } 56 57 /** 58 * Sets the drawable that shows the icon of the mode. 59 * 60 * @param drawable drawable of the mode icon 61 */ setIconDrawable(Drawable drawable)62 public void setIconDrawable(Drawable drawable) { 63 mIconDrawable = drawable; 64 65 // Center icon in the background. 66 if (mIconDrawable != null) { 67 mIconDrawable.setBounds(mIconBackgroundSize / 2 - mIconDrawableSize / 2, 68 mIconBackgroundSize / 2 - mIconDrawableSize / 2, 69 mIconBackgroundSize / 2 + mIconDrawableSize / 2, 70 mIconBackgroundSize / 2 + mIconDrawableSize / 2); 71 invalidate(); 72 } 73 } 74 75 @Override draw(Canvas canvas)76 public void draw(Canvas canvas) { 77 super.draw(canvas); 78 mBackground.draw(canvas); 79 if (mIconDrawable != null) { 80 mIconDrawable.draw(canvas); 81 } 82 } 83 84 /** 85 * @return A clone of the icon drawable associated with this view. 86 */ getIconDrawableClone()87 public Drawable getIconDrawableClone() { 88 return mIconDrawable.getConstantState().newDrawable(); 89 } 90 91 /** 92 * @return The size of the icon drawable. 93 */ getIconDrawableSize()94 public int getIconDrawableSize() { 95 return mIconDrawableSize; 96 } 97 98 /** 99 * This gets called when the selected state is changed. When selected, the background 100 * drawable will use a solid pre-defined color to indicate selection. 101 * 102 * @param selected true when selected, false otherwise. 103 */ 104 @Override setSelected(boolean selected)105 public void setSelected(boolean selected) { 106 if (selected) { 107 mBackground.setColor(mHighlightColor); 108 } else { 109 mBackground.setColor(mBackgroundDefaultColor); 110 } 111 112 invalidate(); 113 } 114 115 /** 116 * Sets the color that will be used in the drawable for highlight state. 117 * 118 * @param highlightColor color for the highlight state 119 */ setHighlightColor(int highlightColor)120 public void setHighlightColor(int highlightColor) { 121 mHighlightColor = highlightColor; 122 } 123 124 /** 125 * @return The highlightColor color the the highlight state. 126 */ getHighlightColor()127 public int getHighlightColor() { 128 return mHighlightColor; 129 } 130 } 131