1 /* 2 * Copyright (C) 2011 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; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Paint; 22 import android.graphics.RectF; 23 import android.util.AttributeSet; 24 import android.widget.ImageView; 25 26 class PanoProgressBar extends ImageView { 27 @SuppressWarnings("unused") 28 private static final String TAG = "PanoProgressBar"; 29 public static final int DIRECTION_NONE = 0; 30 public static final int DIRECTION_LEFT = 1; 31 public static final int DIRECTION_RIGHT = 2; 32 private float mProgress = 0; 33 private float mMaxProgress = 0; 34 private float mLeftMostProgress = 0; 35 private float mRightMostProgress = 0; 36 private float mProgressOffset = 0; 37 private float mIndicatorWidth = 0; 38 private int mDirection = 0; 39 private final Paint mBackgroundPaint = new Paint(); 40 private final Paint mDoneAreaPaint = new Paint(); 41 private final Paint mIndicatorPaint = new Paint(); 42 private float mWidth; 43 private float mHeight; 44 private RectF mDrawBounds; 45 private OnDirectionChangeListener mListener = null; 46 47 public interface OnDirectionChangeListener { 48 public void onDirectionChange(int direction); 49 } 50 51 public PanoProgressBar(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 mDoneAreaPaint.setStyle(Paint.Style.FILL); 54 mDoneAreaPaint.setAlpha(0xff); 55 56 mBackgroundPaint.setStyle(Paint.Style.FILL); 57 mBackgroundPaint.setAlpha(0xff); 58 59 mIndicatorPaint.setStyle(Paint.Style.FILL); 60 mIndicatorPaint.setAlpha(0xff); 61 62 mDrawBounds = new RectF(); 63 } 64 65 public void setOnDirectionChangeListener(OnDirectionChangeListener l) { 66 mListener = l; 67 } 68 69 private void setDirection(int direction) { 70 if (mDirection != direction) { 71 mDirection = direction; 72 if (mListener != null) { 73 mListener.onDirectionChange(mDirection); 74 } 75 invalidate(); 76 } 77 } 78 79 public int getDirection() { 80 return mDirection; 81 } 82 83 @Override 84 public void setBackgroundColor(int color) { 85 mBackgroundPaint.setColor(color); 86 invalidate(); 87 } 88 89 public void setDoneColor(int color) { 90 mDoneAreaPaint.setColor(color); 91 invalidate(); 92 } 93 94 public void setIndicatorColor(int color) { 95 mIndicatorPaint.setColor(color); 96 invalidate(); 97 } 98 99 @Override 100 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 101 mWidth = w; 102 mHeight = h; 103 mDrawBounds.set(0, 0, mWidth, mHeight); 104 } 105 106 public void setMaxProgress(int progress) { 107 mMaxProgress = progress; 108 } 109 110 public void setIndicatorWidth(float w) { 111 mIndicatorWidth = w; 112 invalidate(); 113 } 114 115 public void setRightIncreasing(boolean rightIncreasing) { 116 if (rightIncreasing) { 117 mLeftMostProgress = 0; 118 mRightMostProgress = 0; 119 mProgressOffset = 0; 120 setDirection(DIRECTION_RIGHT); 121 } else { 122 mLeftMostProgress = mWidth; 123 mRightMostProgress = mWidth; 124 mProgressOffset = mWidth; 125 setDirection(DIRECTION_LEFT); 126 } 127 invalidate(); 128 } 129 130 public void setProgress(int progress) { 131 // The panning direction will be decided after user pan more than 10 degrees in one 132 // direction. 133 if (mDirection == DIRECTION_NONE) { 134 if (progress > 10) { 135 setRightIncreasing(true); 136 } else if (progress < -10) { 137 setRightIncreasing(false); 138 } 139 } 140 // mDirection might be modified by setRightIncreasing() above. Need to check again. 141 if (mDirection != DIRECTION_NONE) { 142 mProgress = progress * mWidth / mMaxProgress + mProgressOffset; 143 // value bounds. 144 mProgress = Math.min(mWidth, Math.max(0, mProgress)); 145 if (mDirection == DIRECTION_RIGHT) { 146 // The right most progress is adjusted. 147 mRightMostProgress = Math.max(mRightMostProgress, mProgress); 148 } 149 if (mDirection == DIRECTION_LEFT) { 150 // The left most progress is adjusted. 151 mLeftMostProgress = Math.min(mLeftMostProgress, mProgress); 152 } 153 invalidate(); 154 } 155 } 156 157 public void reset() { 158 mProgress = 0; 159 mProgressOffset = 0; 160 setDirection(DIRECTION_NONE); 161 invalidate(); 162 } 163 164 @Override 165 protected void onDraw(Canvas canvas) { 166 // the background 167 canvas.drawRect(mDrawBounds, mBackgroundPaint); 168 if (mDirection != DIRECTION_NONE) { 169 // the progress area 170 canvas.drawRect(mLeftMostProgress, mDrawBounds.top, mRightMostProgress, 171 mDrawBounds.bottom, mDoneAreaPaint); 172 // the indication bar 173 float l; 174 float r; 175 if (mDirection == DIRECTION_RIGHT) { 176 l = Math.max(mProgress - mIndicatorWidth, 0f); 177 r = mProgress; 178 } else { 179 l = mProgress; 180 r = Math.min(mProgress + mIndicatorWidth, mWidth); 181 } 182 canvas.drawRect(l, mDrawBounds.top, r, mDrawBounds.bottom, mIndicatorPaint); 183 } 184 185 // draw the mask image on the top for shaping. 186 super.onDraw(canvas); 187 } 188 } 189