ImageShow.java revision bf93da72576b28f4e9dfb27f8f3fef702c8ae82d
1 2package com.android.gallery3d.filtershow.imageshow; 3 4import com.android.gallery3d.filtershow.FilterShowActivity; 5import com.android.gallery3d.filtershow.HistoryAdapter; 6import com.android.gallery3d.filtershow.ImageStateAdapter; 7import com.android.gallery3d.filtershow.cache.ImageLoader; 8import com.android.gallery3d.filtershow.filters.ImageFilter; 9import com.android.gallery3d.filtershow.presets.ImagePreset; 10import com.android.gallery3d.filtershow.ui.SliderListener; 11import com.android.gallery3d.filtershow.ui.SliderController; 12import com.android.gallery3d.R; 13import com.android.gallery3d.R.id; 14import com.android.gallery3d.R.layout; 15 16import android.content.Context; 17import android.graphics.Bitmap; 18import android.graphics.Canvas; 19import android.graphics.Paint; 20import android.graphics.Rect; 21import android.net.Uri; 22import android.os.Handler; 23import android.os.Message; 24import android.util.AttributeSet; 25import android.util.Log; 26import android.view.MotionEvent; 27import android.view.View; 28import android.view.View.MeasureSpec; 29import android.widget.ArrayAdapter; 30 31public class ImageShow extends View implements SliderListener { 32 33 private static final String LOGTAG = "ImageShow"; 34 35 protected Paint mPaint = new Paint(); 36 private static int mTextSize = 24; 37 private static int mTextPadding = 20; 38 39 protected ImagePreset mImagePreset = null; 40 protected ImageLoader mImageLoader = null; 41 private ImageFilter mCurrentFilter = null; 42 43 private Bitmap mBackgroundImage = null; 44 protected Bitmap mForegroundImage = null; 45 protected Bitmap mFilteredImage = null; 46 47 protected SliderController mSliderController = new SliderController(); 48 49 private HistoryAdapter mHistoryAdapter = null; 50 private ImageStateAdapter mImageStateAdapter = null; 51 52 protected Rect mImageBounds = null; 53 protected float mImageRotation = 0; 54 protected float mImageRotationZoomFactor = 0; 55 56 private boolean mShowControls = false; 57 private boolean mShowOriginal = false; 58 private String mToast = null; 59 private boolean mShowToast = false; 60 private boolean mImportantToast = false; 61 62 private Handler mHandler = new Handler(); 63 64 public void onNewValue(int value) { 65 if (mCurrentFilter != null) { 66 mCurrentFilter.setParameter(value); 67 } 68 if (mImagePreset != null) { 69 mImageLoader.resetImageForPreset(mImagePreset, this); 70 mImagePreset.fillImageStateAdapter(mImageStateAdapter); 71 } 72 invalidate(); 73 } 74 75 public ImageShow(Context context, AttributeSet attrs) { 76 super(context, attrs); 77 mSliderController.setListener(this); 78 mHistoryAdapter = new HistoryAdapter(context, R.layout.filtershow_history_operation_row, 79 R.id.rowTextView); 80 mImageStateAdapter = new ImageStateAdapter(context, 81 R.layout.filtershow_imagestate_row); 82 } 83 84 public ImageShow(Context context) { 85 super(context); 86 mSliderController.setListener(this); 87 mHistoryAdapter = new HistoryAdapter(context, R.layout.filtershow_history_operation_row, 88 R.id.rowTextView); 89 } 90 91 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 92 int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 93 int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 94 setMeasuredDimension(parentWidth, parentHeight); 95 mSliderController.setWidth(parentWidth); 96 mSliderController.setHeight(parentHeight); 97 } 98 99 public void setCurrentFilter(ImageFilter filter) { 100 mCurrentFilter = filter; 101 } 102 103 public void setAdapter(HistoryAdapter adapter) { 104 mHistoryAdapter = adapter; 105 } 106 107 public void showToast(String text) { 108 showToast(text, false); 109 } 110 111 public void showToast(String text, boolean important) { 112 mToast = text; 113 mShowToast = true; 114 mImportantToast = important; 115 invalidate(); 116 117 mHandler.postDelayed(new Runnable() { 118 @Override 119 public void run() { 120 mShowToast = false; 121 invalidate(); 122 } 123 }, 400); 124 } 125 126 public ImagePreset getImagePreset() { 127 return mImagePreset; 128 } 129 130 public Bitmap getOriginalFrontBitmap() { 131 if (mImageLoader != null) { 132 return mImageLoader.getOriginalBitmapLarge(); 133 } 134 return null; 135 } 136 137 public void drawToast(Canvas canvas) { 138 if (mShowToast && mToast != null) { 139 Paint paint = new Paint(); 140 paint.setTextSize(128); 141 float textWidth = paint.measureText(mToast); 142 int toastX = (int) ((getWidth() - textWidth) / 2.0f); 143 int toastY = (int) (getHeight() / 3.0f); 144 145 paint.setARGB(255, 0, 0, 0); 146 canvas.drawText(mToast, toastX - 2, toastY - 2, paint); 147 canvas.drawText(mToast, toastX - 2, toastY, paint); 148 canvas.drawText(mToast, toastX, toastY - 2, paint); 149 canvas.drawText(mToast, toastX + 2, toastY + 2, paint); 150 canvas.drawText(mToast, toastX + 2, toastY, paint); 151 canvas.drawText(mToast, toastX, toastY + 2, paint); 152 if (mImportantToast) { 153 paint.setARGB(255, 200, 0, 0); 154 } else { 155 paint.setARGB(255, 255, 255, 255); 156 } 157 canvas.drawText(mToast, toastX, toastY, paint); 158 } 159 } 160 161 public void onDraw(Canvas canvas) { 162 if (mBackgroundImage == null) { 163 mBackgroundImage = mImageLoader.getBackgroundBitmap(getResources()); 164 } 165 if (mBackgroundImage != null) { 166 Rect s = new Rect(0, 0, mBackgroundImage.getWidth(), 167 mBackgroundImage.getHeight()); 168 Rect d = new Rect(0, 0, getWidth(), getHeight()); 169 canvas.drawBitmap(mBackgroundImage, s, d, mPaint); 170 } 171 172 Bitmap filteredImage = null; 173 if (mImageLoader != null) { 174 filteredImage = mImageLoader.getImageForPreset(this, 175 getImagePreset(), showHires()); 176 } 177 178 if (filteredImage == null) { 179 // if no image for the current preset, use the previous one 180 filteredImage = mFilteredImage; 181 } else { 182 mFilteredImage = filteredImage; 183 } 184 185 if (mShowOriginal || mFilteredImage == null) { 186 mFilteredImage = mForegroundImage; 187 } 188 189 if (mFilteredImage != null) { 190 Rect s = new Rect(0, 0, mFilteredImage.getWidth(), 191 mFilteredImage.getHeight()); 192 float ratio = mFilteredImage.getWidth() 193 / (float) mFilteredImage.getHeight(); 194 float w = getWidth(); 195 float h = w / ratio; 196 float ty = (getHeight() - h) / 2.0f; 197 float tx = 0; 198 // t = 0; 199 if (ratio < 1.0f) { // portrait image 200 h = getHeight(); 201 w = h * ratio; 202 tx = (getWidth() - w) / 2.0f; 203 ty = 0; 204 } 205 Rect d = new Rect((int) tx, (int) ty, (int) (w + tx), 206 (int) (h + ty)); 207 mImageBounds = d; 208 209 canvas.drawBitmap(mFilteredImage, s, d, mPaint); 210 } 211 212 if (showTitle() && getImagePreset() != null) { 213 mPaint.setARGB(200, 0, 0, 0); 214 mPaint.setTextSize(mTextSize); 215 216 Rect textRect = new Rect(0, 0, getWidth(), mTextSize + mTextPadding); 217 canvas.drawRect(textRect, mPaint); 218 mPaint.setARGB(255, 200, 200, 200); 219 canvas.drawText(getImagePreset().name(), mTextPadding, 220 10 + mTextPadding, mPaint); 221 } 222 mPaint.setARGB(255, 150, 150, 150); 223 mPaint.setStrokeWidth(4); 224 canvas.drawLine(0, 0, getWidth(), 0, mPaint); 225 226 if (showControls()) { 227 mSliderController.onDraw(canvas); 228 } 229 230 drawToast(canvas); 231 } 232 233 public void setShowControls(boolean value) { 234 mShowControls = value; 235 } 236 237 public boolean showControls() { 238 return mShowControls; 239 } 240 241 public boolean showHires() { 242 return true; 243 } 244 245 public boolean showTitle() { 246 return false; 247 } 248 249 public void setImagePreset(ImagePreset preset) { 250 setImagePreset(preset, true); 251 } 252 253 public void setImagePreset(ImagePreset preset, boolean addToHistory) { 254 mImagePreset = preset; 255 if (getImagePreset() != null) { 256 if (addToHistory) { 257 mHistoryAdapter.insert(getImagePreset(), 0); 258 } 259 getImagePreset().setEndpoint(this); 260 updateImage(); 261 } 262 mImagePreset.fillImageStateAdapter(mImageStateAdapter); 263 invalidate(); 264 } 265 266 public void setImageLoader(ImageLoader loader) { 267 mImageLoader = loader; 268 if (mImageLoader != null) { 269 mImageLoader.addListener(this); 270 } 271 } 272 273 public void updateImage() { 274 mForegroundImage = getOriginalFrontBitmap(); 275 /* 276 * if (mImageLoader != null) { 277 * mImageLoader.resetImageForPreset(getImagePreset(), this); } 278 */ 279 280 /* 281 * if (mForegroundImage != null) { Bitmap filteredImage = 282 * mForegroundImage.copy(mConfig, true); 283 * getImagePreset().apply(filteredImage); invalidate(); } 284 */ 285 } 286 287 public void updateFilteredImage(Bitmap bitmap) { 288 mFilteredImage = bitmap; 289 } 290 291 public void saveImage(FilterShowActivity filterShowActivity) { 292 mImageLoader.saveImage(getImagePreset(), filterShowActivity); 293 } 294 295 public boolean onTouchEvent(MotionEvent event) { 296 super.onTouchEvent(event); 297 mSliderController.onTouchEvent(event); 298 invalidate(); 299 return true; 300 } 301 302 // listview stuff 303 304 public ArrayAdapter getHistoryAdapter() { 305 return mHistoryAdapter; 306 } 307 308 public ArrayAdapter getImageStateAdapter() { 309 return mImageStateAdapter; 310 } 311 312 public void onItemClick(int position) { 313 setImagePreset(new ImagePreset(mHistoryAdapter.getItem(position)), false); 314 // we need a copy from the history 315 mHistoryAdapter.setCurrentPreset(position); 316 } 317 318 public void showOriginal(boolean show) { 319 mShowOriginal = show; 320 invalidate(); 321 } 322 323 public float getImageRotation() { 324 return mImageRotation; 325 } 326 327 public float getImageRotationZoomFactor() { 328 return mImageRotationZoomFactor; 329 } 330 331 public void setImageRotation(float imageRotation, 332 float imageRotationZoomFactor) { 333 if (imageRotation != mImageRotation) { 334 invalidate(); 335 } 336 mImageRotation = imageRotation; 337 mImageRotationZoomFactor = imageRotationZoomFactor; 338 } 339} 340