From b70dbfcdc6fc52bbff98e1f5fae765bbc500273a Mon Sep 17 00:00:00 2001 From: Ruben Brunk Date: Wed, 10 Apr 2013 00:11:46 -0700 Subject: Adding simple crop ui. Refactoring. Change-Id: I904ce776ca31cf2f782b0aa8e6456ab54b15e540 --- .../filtershow/crop/CropDrawingUtils.java | 106 +++++++++++++++++++++ .../gallery3d/filtershow/crop/CropLoader.java | 3 +- .../gallery3d/filtershow/crop/CropView.java | 91 +----------------- 3 files changed, 112 insertions(+), 88 deletions(-) create mode 100644 src/com/android/gallery3d/filtershow/crop/CropDrawingUtils.java diff --git a/src/com/android/gallery3d/filtershow/crop/CropDrawingUtils.java b/src/com/android/gallery3d/filtershow/crop/CropDrawingUtils.java new file mode 100644 index 000000000..749ba0e29 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/crop/CropDrawingUtils.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.gallery3d.filtershow.crop; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Matrix; +import android.graphics.Paint; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; + +public abstract class CropDrawingUtils { + + public static void drawRuleOfThird(Canvas canvas, RectF bounds) { + Paint p = new Paint(); + p.setStyle(Paint.Style.STROKE); + p.setColor(Color.argb(128, 255, 255, 255)); + p.setStrokeWidth(2); + float stepX = bounds.width() / 3.0f; + float stepY = bounds.height() / 3.0f; + float x = bounds.left + stepX; + float y = bounds.top + stepY; + for (int i = 0; i < 2; i++) { + canvas.drawLine(x, bounds.top, x, bounds.bottom, p); + x += stepX; + } + for (int j = 0; j < 2; j++) { + canvas.drawLine(bounds.left, y, bounds.right, y, p); + y += stepY; + } + } + + public static void drawCropRect(Canvas canvas, RectF bounds) { + Paint p = new Paint(); + p.setStyle(Paint.Style.STROKE); + p.setColor(Color.WHITE); + p.setStrokeWidth(3); + canvas.drawRect(bounds, p); + } + + public static void drawIndicator(Canvas canvas, Drawable indicator, int indicatorSize, + float centerX, float centerY) { + int left = (int) centerX - indicatorSize / 2; + int top = (int) centerY - indicatorSize / 2; + indicator.setBounds(left, top, left + indicatorSize, top + indicatorSize); + indicator.draw(canvas); + } + + public static void drawIndicators(Canvas canvas, Drawable cropIndicator, int indicatorSize, + RectF bounds, boolean fixedAspect, int selection) { + boolean notMoving = (selection == CropObject.MOVE_NONE); + if (fixedAspect) { + if ((selection == CropObject.TOP_LEFT) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.left, bounds.top); + } + if ((selection == CropObject.TOP_RIGHT) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.right, bounds.top); + } + if ((selection == CropObject.BOTTOM_LEFT) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.left, bounds.bottom); + } + if ((selection == CropObject.BOTTOM_RIGHT) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.right, bounds.bottom); + } + } else { + if (((selection & CropObject.MOVE_TOP) != 0) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.centerX(), bounds.top); + } + if (((selection & CropObject.MOVE_BOTTOM) != 0) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.centerX(), bounds.bottom); + } + if (((selection & CropObject.MOVE_LEFT) != 0) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.left, bounds.centerY()); + } + if (((selection & CropObject.MOVE_RIGHT) != 0) || notMoving) { + drawIndicator(canvas, cropIndicator, indicatorSize, bounds.right, bounds.centerY()); + } + } + } + + public static Matrix getBitmapToDisplayMatrix(RectF imageBounds, RectF displayBounds) { + Matrix m = new Matrix(); + CropDrawingUtils.setBitmapToDisplayMatrix(m, imageBounds, displayBounds); + return m; + } + + public static boolean setBitmapToDisplayMatrix(Matrix m, RectF imageBounds, + RectF displayBounds) { + m.reset(); + return m.setRectToRect(imageBounds, displayBounds, Matrix.ScaleToFit.CENTER); + } + +} diff --git a/src/com/android/gallery3d/filtershow/crop/CropLoader.java b/src/com/android/gallery3d/filtershow/crop/CropLoader.java index 40254931f..d2a681f38 100644 --- a/src/com/android/gallery3d/filtershow/crop/CropLoader.java +++ b/src/com/android/gallery3d/filtershow/crop/CropLoader.java @@ -35,7 +35,8 @@ import java.io.IOException; import java.io.InputStream; /** - * This class contains reentrant static methods for loading a bitmap. + * This class contains static methods for loading a bitmap and + * mantains no instance state. */ public abstract class CropLoader { public static final String LOGTAG = "CropLoader"; diff --git a/src/com/android/gallery3d/filtershow/crop/CropView.java b/src/com/android/gallery3d/filtershow/crop/CropView.java index 561f7ae7f..d762ad459 100644 --- a/src/com/android/gallery3d/filtershow/crop/CropView.java +++ b/src/com/android/gallery3d/filtershow/crop/CropView.java @@ -20,7 +20,6 @@ import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Rect; @@ -91,7 +90,7 @@ public class CropView extends View { // If display matrix doesn't exist, create it and its dependencies if (mDisplayMatrix == null || mDisplayMatrixInverse == null) { - mDisplayMatrix = getBitmapToDisplayMatrix(displayBounds, new RectF( + mDisplayMatrix = CropDrawingUtils.getBitmapToDisplayMatrix(displayBounds, new RectF( displayBoundsOriginal)); mDisplayMatrixInverse = new Matrix(); mDisplayMatrixInverse.reset(); @@ -105,9 +104,9 @@ public class CropView extends View { canvas.drawBitmap(mImage, mDisplayMatrix, new Paint()); if (mDisplayMatrix.mapRect(imageBounds)) { - drawCropRect(canvas, imageBounds); - drawRuleOfThird(canvas, imageBounds); - drawIndicators(canvas, mCropIndicator, mIndicatorSize, imageBounds, + CropDrawingUtils.drawCropRect(canvas, imageBounds); + CropDrawingUtils.drawRuleOfThird(canvas, imageBounds); + CropDrawingUtils.drawIndicators(canvas, mCropIndicator, mIndicatorSize, imageBounds, mCropObj.isFixedAspect(), mCropObj.getSelectState()); } } @@ -193,86 +192,4 @@ public class CropView extends View { out_crop.set(inner); return true; } - - // Helper methods - - private static void drawRuleOfThird(Canvas canvas, RectF bounds) { - Paint p = new Paint(); - p.setStyle(Paint.Style.STROKE); - p.setColor(Color.argb(128, 255, 255, 255)); - p.setStrokeWidth(2); - float stepX = bounds.width() / 3.0f; - float stepY = bounds.height() / 3.0f; - float x = bounds.left + stepX; - float y = bounds.top + stepY; - for (int i = 0; i < 2; i++) { - canvas.drawLine(x, bounds.top, x, bounds.bottom, p); - x += stepX; - } - for (int j = 0; j < 2; j++) { - canvas.drawLine(bounds.left, y, bounds.right, y, p); - y += stepY; - } - } - - private static void drawCropRect(Canvas canvas, RectF bounds) { - Paint p = new Paint(); - p.setStyle(Paint.Style.STROKE); - p.setColor(Color.WHITE); - p.setStrokeWidth(3); - canvas.drawRect(bounds, p); - } - - private static void drawIndicator(Canvas canvas, Drawable indicator, int indicatorSize, - float centerX, float centerY) { - int left = (int) centerX - indicatorSize / 2; - int top = (int) centerY - indicatorSize / 2; - indicator.setBounds(left, top, left + indicatorSize, top + indicatorSize); - indicator.draw(canvas); - } - - private static void drawIndicators(Canvas canvas, Drawable cropIndicator, int indicatorSize, - RectF bounds, boolean fixedAspect, int selection) { - boolean notMoving = (selection == CropObject.MOVE_NONE); - if (fixedAspect) { - if ((selection == CropObject.TOP_LEFT) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.left, bounds.top); - } - if ((selection == CropObject.TOP_RIGHT) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.right, bounds.top); - } - if ((selection == CropObject.BOTTOM_LEFT) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.left, bounds.bottom); - } - if ((selection == CropObject.BOTTOM_RIGHT) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.right, bounds.bottom); - } - } else { - if (((selection & CropObject.MOVE_TOP) != 0) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.centerX(), bounds.top); - } - if (((selection & CropObject.MOVE_BOTTOM) != 0) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.centerX(), bounds.bottom); - } - if (((selection & CropObject.MOVE_LEFT) != 0) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.left, bounds.centerY()); - } - if (((selection & CropObject.MOVE_RIGHT) != 0) || notMoving) { - drawIndicator(canvas, cropIndicator, indicatorSize, bounds.right, bounds.centerY()); - } - } - } - - private static Matrix getBitmapToDisplayMatrix(RectF imageBounds, RectF displayBounds) { - Matrix m = new Matrix(); - setBitmapToDisplayMatrix(m, imageBounds, displayBounds); - return m; - } - - private static boolean setBitmapToDisplayMatrix(Matrix m, RectF imageBounds, - RectF displayBounds) { - m.reset(); - return m.setRectToRect(imageBounds, displayBounds, Matrix.ScaleToFit.CENTER); - } - } -- cgit v1.2.3