1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * 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.Path; 22 import android.graphics.drawable.BitmapDrawable; 23 import android.graphics.drawable.Drawable; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * Pie menu item 30 */ 31 public class PieItem { 32 33 public static interface OnClickListener { onClick(PieItem item)34 void onClick(PieItem item); 35 } 36 37 private Drawable mDrawable; 38 private int level; 39 private float mCenter; 40 private float start; 41 private float sweep; 42 private float animate; 43 private int inner; 44 private int outer; 45 private boolean mSelected; 46 private boolean mEnabled; 47 private List<PieItem> mItems; 48 private Path mPath; 49 private OnClickListener mOnClickListener; 50 private float mAlpha; 51 52 // Gray out the view when disabled 53 private static final float ENABLED_ALPHA = 1; 54 private static final float DISABLED_ALPHA = (float) 0.3; 55 private boolean mChangeAlphaWhenDisabled = true; 56 PieItem(Drawable drawable, int level)57 public PieItem(Drawable drawable, int level) { 58 mDrawable = drawable; 59 this.level = level; 60 setAlpha(1f); 61 mEnabled = true; 62 setAnimationAngle(getAnimationAngle()); 63 start = -1; 64 mCenter = -1; 65 } 66 hasItems()67 public boolean hasItems() { 68 return mItems != null; 69 } 70 getItems()71 public List<PieItem> getItems() { 72 return mItems; 73 } 74 addItem(PieItem item)75 public void addItem(PieItem item) { 76 if (mItems == null) { 77 mItems = new ArrayList<PieItem>(); 78 } 79 mItems.add(item); 80 } 81 setPath(Path p)82 public void setPath(Path p) { 83 mPath = p; 84 } 85 getPath()86 public Path getPath() { 87 return mPath; 88 } 89 setChangeAlphaWhenDisabled(boolean enable)90 public void setChangeAlphaWhenDisabled (boolean enable) { 91 mChangeAlphaWhenDisabled = enable; 92 } 93 setAlpha(float alpha)94 public void setAlpha(float alpha) { 95 mAlpha = alpha; 96 mDrawable.setAlpha((int) (255 * alpha)); 97 } 98 setAnimationAngle(float a)99 public void setAnimationAngle(float a) { 100 animate = a; 101 } 102 getAnimationAngle()103 public float getAnimationAngle() { 104 return animate; 105 } 106 setEnabled(boolean enabled)107 public void setEnabled(boolean enabled) { 108 mEnabled = enabled; 109 if (mChangeAlphaWhenDisabled) { 110 if (mEnabled) { 111 setAlpha(ENABLED_ALPHA); 112 } else { 113 setAlpha(DISABLED_ALPHA); 114 } 115 } 116 } 117 isEnabled()118 public boolean isEnabled() { 119 return mEnabled; 120 } 121 setSelected(boolean s)122 public void setSelected(boolean s) { 123 mSelected = s; 124 } 125 isSelected()126 public boolean isSelected() { 127 return mSelected; 128 } 129 getLevel()130 public int getLevel() { 131 return level; 132 } 133 setGeometry(float st, float sw, int inside, int outside)134 public void setGeometry(float st, float sw, int inside, int outside) { 135 start = st; 136 sweep = sw; 137 inner = inside; 138 outer = outside; 139 } 140 setFixedSlice(float center, float sweep)141 public void setFixedSlice(float center, float sweep) { 142 mCenter = center; 143 this.sweep = sweep; 144 } 145 getCenter()146 public float getCenter() { 147 return mCenter; 148 } 149 getStart()150 public float getStart() { 151 return start; 152 } 153 getStartAngle()154 public float getStartAngle() { 155 return start + animate; 156 } 157 getSweep()158 public float getSweep() { 159 return sweep; 160 } 161 getInnerRadius()162 public int getInnerRadius() { 163 return inner; 164 } 165 getOuterRadius()166 public int getOuterRadius() { 167 return outer; 168 } 169 setOnClickListener(OnClickListener listener)170 public void setOnClickListener(OnClickListener listener) { 171 mOnClickListener = listener; 172 } 173 performClick()174 public void performClick() { 175 if (mOnClickListener != null) { 176 mOnClickListener.onClick(this); 177 } 178 } 179 getIntrinsicWidth()180 public int getIntrinsicWidth() { 181 return mDrawable.getIntrinsicWidth(); 182 } 183 getIntrinsicHeight()184 public int getIntrinsicHeight() { 185 return mDrawable.getIntrinsicHeight(); 186 } 187 setBounds(int left, int top, int right, int bottom)188 public void setBounds(int left, int top, int right, int bottom) { 189 mDrawable.setBounds(left, top, right, bottom); 190 } 191 draw(Canvas canvas)192 public void draw(Canvas canvas) { 193 mDrawable.draw(canvas); 194 } 195 setImageResource(Context context, int resId)196 public void setImageResource(Context context, int resId) { 197 Drawable d = context.getResources().getDrawable(resId).mutate(); 198 d.setBounds(mDrawable.getBounds()); 199 mDrawable = d; 200 setAlpha(mAlpha); 201 } 202 203 } 204