diff options
15 files changed, 561 insertions, 43 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 623cf4c..7a48a6c 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -26,6 +26,8 @@ <uses-feature android:name="android.software.live_wallpaper" android:required="true" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> + <uses-permission android:name="android.permission.VIBRATE"/> + <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" diff --git a/res/drawable-hdpi/resize_frame_holo.9.png b/res/drawable-hdpi/resize_frame.9.png Binary files differindex 059a9d6..059a9d6 100644 --- a/res/drawable-hdpi/resize_frame_holo.9.png +++ b/res/drawable-hdpi/resize_frame.9.png diff --git a/res/drawable-mdpi/resize_frame_holo.9.png b/res/drawable-mdpi/resize_frame.9.png Binary files differindex 435f3aa..435f3aa 100644 --- a/res/drawable-mdpi/resize_frame_holo.9.png +++ b/res/drawable-mdpi/resize_frame.9.png diff --git a/res/drawable-xhdpi/resize_frame_holo.9.png b/res/drawable-xhdpi/resize_frame.9.png Binary files differindex 270c9b7..270c9b7 100644 --- a/res/drawable-xhdpi/resize_frame_holo.9.png +++ b/res/drawable-xhdpi/resize_frame.9.png diff --git a/res/layout/choose_disposition_fragment.xml b/res/layout/choose_disposition_fragment.xml index 166f715..1569ecd 100644 --- a/res/layout/choose_disposition_fragment.xml +++ b/res/layout/choose_disposition_fragment.xml @@ -14,22 +14,22 @@ limitations under the License. --> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_marginTop="@dimen/default_margin" - android:layout_marginBottom="@dimen/default_margin" + android:layout_marginTop="@dimen/small_margin" + android:layout_marginBottom="@dimen/small_margin" android:orientation="vertical"> - <TextView android:id="@+id/description" + <TextView android:id="@+id/advise" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_blue_light" - android:layout_marginEnd="8dip" - android:layout_marginStart="8dip" - android:padding="4dip" + android:layout_marginStart="@dimen/disposition_frame_margin" + android:layout_marginEnd="@dimen/disposition_frame_margin" + android:padding="@dimen/disposition_advise_frame_padding" android:gravity="center_horizontal" - android:textColor="#FFFFFF" + android:textColor="@color/notification_text_color" android:textAppearance="?android:attr/textAppearanceSmall" android:text="@string/pref_disposition_description" /> @@ -37,6 +37,24 @@ android:id="@+id/disposition_view" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_marginTop="@dimen/default_margin" - android:layout_marginBottom="@dimen/default_margin" /> -</LinearLayout> + android:padding="@dimen/disposition_frame_margin" + android:layout_below="@id/advise" + android:layout_alignParentLeft="true" + android:layout_alignParentRight="true" + android:layout_alignParentBottom="true" /> + + <FrameLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_alignLeft="@id/disposition_view" + android:layout_alignRight="@id/disposition_view" + android:layout_alignTop="@id/disposition_view" + android:layout_alignBottom="@id/disposition_view"> + <org.cyanogenmod.wallpapers.photophase.widgets.ResizeFrame + android:id="@+id/resize_frame" + android:layout_width="1dp" + android:layout_height="1dp"> + </org.cyanogenmod.wallpapers.photophase.widgets.ResizeFrame> + </FrameLayout> + +</RelativeLayout> diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 3fc07e1..92e4643 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -45,5 +45,10 @@ <dimen name="small_margin">8dp</dimen> <dimen name="small_padding">5dp</dimen> + <dimen name="disposition_frame_margin">12dp</dimen> <dimen name="disposition_frame_padding">1dp</dimen> + <dimen name="disposition_advise_frame_padding">4dp</dimen> + <dimen name="resize_frame_padding">12dp</dimen> + <dimen name="resize_frame_extra_handling_space">16dp</dimen> + </resources> diff --git a/res/values/strings.xml b/res/values/strings.xml index eec1105..93b8824 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -72,8 +72,7 @@ <string name="pref_disposition_portrait_summary">Select how pictures are disposed on a portrait screen</string> <string name="pref_disposition_landscape">Landscape disposition</string> <string name="pref_disposition_landscape_summary">Select how pictures are disposed on a landscape screen</string> - <string name="pref_disposition_description">Tap a frame to select it. Then drag \u0026 drop the borders of the frame to resize it. - Tap the split buttons to split the frame horizontally or vertically.</string> + <string name="pref_disposition_description">Long tap a frame to select it. Then drag \u0026 drop the borders of the frame to resize it.</string> <string name="pref_about">About</string> <string name="pref_about_summary">PhotoPhase v<xliff:g id="version">%1$s</xliff:g>\nCopyright \u00A9 2013 The CyanogenMod Project</string> diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java index 7c502ae..0d98a49 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaperWorld.java @@ -309,30 +309,26 @@ public class PhotoPhaseWallpaperWorld { */ private static float[] getVerticesFromDisposition( Disposition disposition, boolean portrait, float cellw, float cellh) { - int x = portrait ? disposition.x : disposition.y; - int y = portrait ? disposition.y : disposition.x; - int w = portrait ? disposition.w : disposition.h; - int h = portrait ? disposition.h : disposition.w; return new float[] { // top left - -1.0f + (x * cellw), - 1.0f - (y * cellh), + -1.0f + (disposition.x * cellw), + 1.0f - (disposition.y * cellh), 0.0f, // bottom left - -1.0f + (x * cellw), - 1.0f - ((y * cellh) + (h * cellh)), + -1.0f + (disposition.x * cellw), + 1.0f - ((disposition.y * cellh) + (disposition.h * cellh)), 0.0f, // bottom right - -1.0f + ((x * cellw) + (w * cellw)), - 1.0f - ((y * cellh) + (h * cellh)), + -1.0f + ((disposition.x * cellw) + (disposition.w * cellw)), + 1.0f - ((disposition.y * cellh) + (disposition.h * cellh)), 0.0f, // top right - -1.0f + ((x * cellw) + (w * cellw)), - 1.0f - (y * cellh), + -1.0f + ((disposition.x * cellw) + (disposition.w * cellw)), + 1.0f - (disposition.y * cellh), 0.0f }; } diff --git a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java index ae7b228..de8261b 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java +++ b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java @@ -40,7 +40,7 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { private static final String TAG = "TextureManager"; - private static final int QUEUE_SIZE = 1; + private static final int QUEUE_SIZE = 3; static final List<Bitmap> sRecycledBitmaps = new ArrayList<Bitmap>(); diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/DispositionFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/DispositionFragment.java index 780bf4e..5bfd3dd 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/DispositionFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/DispositionFragment.java @@ -29,11 +29,12 @@ import android.view.ViewGroup; import org.cyanogenmod.wallpapers.photophase.R; import org.cyanogenmod.wallpapers.photophase.model.Disposition; import org.cyanogenmod.wallpapers.photophase.widgets.DispositionView; +import org.cyanogenmod.wallpapers.photophase.widgets.ResizeFrame; import java.util.List; /** - * An abstract fragment class that allow to choose the layout disposition of the wallpaper + * An abstract fragment class that allow to choose the layout disposition of the wallpaper. */ public abstract class DispositionFragment extends PreferenceFragment { @@ -63,14 +64,14 @@ public abstract class DispositionFragment extends PreferenceFragment { /** * Method that returns the number of rows to use - * + * * @return int The number of rows */ public abstract int getRows(); /** * Method that returns the number of cols to use - * + * * @return int The number of cols */ public abstract int getCols(); @@ -97,6 +98,7 @@ public abstract class DispositionFragment extends PreferenceFragment { // Inflate the layout for this fragment ViewGroup v = (ViewGroup)inflater.inflate(R.layout.choose_disposition_fragment, container, false); mDispositionView = (DispositionView)v.findViewById(R.id.disposition_view); + mDispositionView.setResizeFrame((ResizeFrame)v.findViewById(R.id.resize_frame)); mDispositionView.post(mRedraw); return v; } diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/LandscapeDispositionFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/LandscapeDispositionFragment.java index f53925d..1461df4 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/LandscapeDispositionFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/LandscapeDispositionFragment.java @@ -25,7 +25,7 @@ import java.util.List; /** * A fragment class that allow to choose the layout disposition of the wallpaper for landscape - * screen. + * screen. */ public class LandscapeDispositionFragment extends DispositionFragment { diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/PortraitDispositionFragment.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/PortraitDispositionFragment.java index 49fc4c4..a91fb96 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/PortraitDispositionFragment.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/PortraitDispositionFragment.java @@ -25,7 +25,7 @@ import java.util.List; /** * A fragment class that allow to choose the layout disposition of the wallpaper for portrait - * screen. + * screen. */ public class PortraitDispositionFragment extends DispositionFragment { diff --git a/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java b/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java index 0ad3ace..115e80e 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java +++ b/src/org/cyanogenmod/wallpapers/photophase/preferences/PreferencesProvider.java @@ -291,10 +291,10 @@ public final class PreferencesProvider { */ public static class Layout { - private static final int DEFAULT_COLS = 8; - private static final int DEFAULT_ROWS = 14; - private static final String DEFAULT_PORTRAIT_DISPOSITION = "0x0:5x4|5x0:3x2|5x2:3x2|0x4:4x4|4x4:4x4|0x8:8x6"; - private static final String DEFAULT_LANDSCAPE_DISPOSITION = "0x0:5x4|5x0:3x2|5x2:3x2|0x4:4x4|4x4:4x4|0x8:8x6"; + private static final int DEFAULT_COLS = 4; + private static final int DEFAULT_ROWS = 7; + private static final String DEFAULT_PORTRAIT_DISPOSITION = "0x0:2x1|3x0:3x0|3x1:3x1|0x2:1x3|2x2:3x3|0x4:3x6"; + private static final String DEFAULT_LANDSCAPE_DISPOSITION = "0x0:2x3|3x0:5x1|6x0:6x0|6x1:6x1|3x2:4x2|3x3:4x3|5x2:6x3"; /** * Method that returns the rows of the wallpaper. @@ -352,8 +352,8 @@ public final class PreferencesProvider { Disposition disposition = new Disposition(); disposition.x = Integer.parseInt(s2[0]); disposition.y = Integer.parseInt(s2[1]); - disposition.w = Integer.parseInt(s3[0]); - disposition.h = Integer.parseInt(s3[1]); + disposition.w = Integer.parseInt(s3[0]) - disposition.x + 1; + disposition.h = Integer.parseInt(s3[1]) - disposition.y + 1; dispositions.add(disposition); } return dispositions; diff --git a/src/org/cyanogenmod/wallpapers/photophase/widgets/DispositionView.java b/src/org/cyanogenmod/wallpapers/photophase/widgets/DispositionView.java index 93b4b76..8ee1973 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/widgets/DispositionView.java +++ b/src/org/cyanogenmod/wallpapers/photophase/widgets/DispositionView.java @@ -18,25 +18,38 @@ package org.cyanogenmod.wallpapers.photophase.widgets; import android.content.Context; import android.graphics.Rect; +import android.os.Vibrator; import android.util.AttributeSet; +import android.view.Gravity; +import android.view.View; +import android.view.View.OnLongClickListener; +import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.ImageView.ScaleType; import org.cyanogenmod.wallpapers.photophase.R; import org.cyanogenmod.wallpapers.photophase.model.Disposition; +import org.cyanogenmod.wallpapers.photophase.widgets.ResizeFrame.OnResizeListener; import java.util.List; /** - * A class that allow to select the frames disposition visually + * A class that allow to select the frames disposition visually */ -public class DispositionView extends RelativeLayout { +public class DispositionView extends RelativeLayout implements OnLongClickListener, OnResizeListener { private List<Disposition> mDispositions; private int mCols; private int mRows; + private View mTarget; + private ResizeFrame mResizeFrame; + private int mInternalPadding; + private Rect mOldResizeFrameLocation; + + private Vibrator mVibrator; + /** * Constructor of <code>DispositionView</code>. * @@ -44,6 +57,7 @@ public class DispositionView extends RelativeLayout { */ public DispositionView(Context context) { super(context); + init(); } /** @@ -54,6 +68,7 @@ public class DispositionView extends RelativeLayout { */ public DispositionView(Context context, AttributeSet attrs) { super(context, attrs); + init(); } /** @@ -68,6 +83,15 @@ public class DispositionView extends RelativeLayout { */ public DispositionView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); + init(); + } + + /** + * Initialize the view + */ + private void init() { + mVibrator = (Vibrator)getContext().getSystemService(Context.VIBRATOR_SERVICE); + mInternalPadding = (int)getResources().getDimension(R.dimen.disposition_frame_padding); } /** @@ -88,6 +112,16 @@ public class DispositionView extends RelativeLayout { } /** + * Method that sets the resize frame view + * + * @param resizeFrame The resize frame view + */ + public void setResizeFrame(ResizeFrame resizeFrame) { + mResizeFrame = resizeFrame; + mResizeFrame.setOnResizeListener(this); + } + + /** * Method that create a new frame to be drawn in the specified location * * @param r The location relative to the parent layout @@ -102,8 +136,7 @@ public class DispositionView extends RelativeLayout { new RelativeLayout.LayoutParams(r.width() - padding, r.height() - padding); params.leftMargin = r.left + padding; params.topMargin = r.top + padding; - v.setFocusable(true); - v.setFocusableInTouchMode(true); + v.setOnLongClickListener(this); addView(v, params); } @@ -114,8 +147,8 @@ public class DispositionView extends RelativeLayout { * @return Rect The location on parent view */ private Rect getLocationFromDisposition(Disposition disposition) { - int w = getMeasuredWidth(); - int h = getMeasuredHeight(); + int w = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight()); + int h = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom()); int cw = w / mCols; int ch = h / mRows; @@ -126,4 +159,166 @@ public class DispositionView extends RelativeLayout { location.bottom = location.top + disposition.h * ch; return location; } + + /** + * {@inheritDoc} + */ + @Override + public boolean onLongClick(View v) { + // Do not do long click if we do not have a target +// if (mTarget != null) return false; + + // Show the resize frame view just in place of the current clicked view + mResizeFrame.hide(); + RelativeLayout.LayoutParams viewParams = + (RelativeLayout.LayoutParams)v.getLayoutParams(); + FrameLayout.LayoutParams frameParams = + (FrameLayout.LayoutParams)mResizeFrame.getLayoutParams(); + int padding = mInternalPadding + mResizeFrame.getNeededPadding(); + frameParams.width = viewParams.width + (padding * 2); + frameParams.height = viewParams.height + (padding * 2); + mResizeFrame.setX(v.getLeft() - padding); + mResizeFrame.setY(v.getTop() - padding); + mVibrator.vibrate(300); + mResizeFrame.show(); + + // Save the new view + mTarget = v; + + return true; + } + + @Override + public void onStartResize(int mode) { + mOldResizeFrameLocation = new Rect( + mResizeFrame.getLeft(), + mResizeFrame.getTop(), + mResizeFrame.getRight(), + mResizeFrame.getBottom()); + } + + @Override + public void onResize(int mode, int delta) { + if (mTarget == null) return; + + int w = getMeasuredWidth() - (getPaddingLeft() + getPaddingRight()); + int h = getMeasuredHeight() - (getPaddingTop() + getPaddingBottom()); + int minWidth = (w / mCols) + (w / mCols) / 2; + int minHeight = (h / mRows) + (h / mRows) / 2; + + FrameLayout.LayoutParams params = + (FrameLayout.LayoutParams)mResizeFrame.getLayoutParams(); + switch (mode) { + case Gravity.LEFT: + float newpos = mResizeFrame.getX() + delta; + if ((delta < 0 && newpos < (getPaddingLeft() * -1)) || + (delta > 0 && newpos > (mResizeFrame.getX() + params.width - minWidth))) { + return; + } + mResizeFrame.setX(newpos); + params.width -= delta; + break; + case Gravity.RIGHT: + if ((delta < 0 && ((params.width + delta) < minWidth)) || + (delta > 0 && (mResizeFrame.getX() + delta + params.width) > (getPaddingLeft() + getMeasuredWidth()))) { + return; + } + params.width += delta; + break; + case Gravity.TOP: + newpos = mResizeFrame.getY() + delta; + if ((delta < 0 && newpos < (getPaddingTop() * -1)) || + (delta > 0 && newpos > (mResizeFrame.getY() + params.height - minHeight))) { + return; + } + mResizeFrame.setY(newpos); + params.height -= delta; + break; + case Gravity.BOTTOM: + if ((delta < 0 && ((params.height + delta) < minHeight)) || + (delta > 0 && (mResizeFrame.getY() + delta + params.height) > (getPaddingTop() + getMeasuredHeight()))) { + return; + } + params.height += delta; + break; + + default: + break; + } + mResizeFrame.setLayoutParams(params); + } + + @Override + public void onEndResize(int mode) { + try { +// int w = getMeasuredWidth(); +// int h = getMeasuredHeight(); +// int cw = w / mCols; +// int ch = h / mRows; +// +// // Retrieve the new layout params +// int neededPadding = mResizeFrame.getNeededPadding(); +// int padding = (int)getResources().getDimension(R.dimen.disposition_frame_padding) +// + neededPadding; +// FrameLayout.LayoutParams params = +// (FrameLayout.LayoutParams)mResizeFrame.getLayoutParams(); +// switch (mode) { +// case Gravity.LEFT: +// int left = params.leftMargin + padding; +// if (left % cw != 0) { +// params.leftMargin = ((left / cw) * cw) - padding; +//// params.width += ((left / cw) * cw) - left + (padding * 2); +// } +// break; +// case Gravity.RIGHT: +// int right = params.rightMargin + padding; +// if (right % cw != 0) { +// params.rightMargin = ((right / cw) * cw) - padding; +//// params.width += ((right / cw) * cw) - right + (padding * 2); +// } +// break; +// case Gravity.TOP: +// int top = params.topMargin + padding; +// if (top % ch != 0) { +// params.topMargin = ((top / ch) * ch) - padding; +//// params.height += ((top / cw) * cw) - top + (padding * 2); +// } +// break; +// case Gravity.BOTTOM: +// int bottom = params.bottomMargin + padding; +// if (bottom % ch != 0) { +// params.bottomMargin = ((bottom / ch) * ch) - padding; +//// params.height += ((bottom / cw) * cw) - bottom + (padding * 2); +// } +// break; +// +// default: +// break; +// } +// mResizeFrame.setLayoutParams(params); +// mResizeFrame.invalidate(); + + // Recalculate all the dispositions in base to the new positions + + } finally { + // Reset vars +// mOldResizeFrameLocation = null; +// mTarget = null; + } + } + + /** + * {@inheritDoc} + */ + @Override + public void onCancel() { + if (mOldResizeFrameLocation != null) { + mTarget.setLeft(mOldResizeFrameLocation.left); + mTarget.setRight(mOldResizeFrameLocation.right); + mTarget.setTop(mOldResizeFrameLocation.top); + mTarget.setBottom(mOldResizeFrameLocation.bottom); + } +// mOldResizeFrameLocation = null; +// mTarget = null; + } } diff --git a/src/org/cyanogenmod/wallpapers/photophase/widgets/ResizeFrame.java b/src/org/cyanogenmod/wallpapers/photophase/widgets/ResizeFrame.java new file mode 100644 index 0000000..218a4e4 --- /dev/null +++ b/src/org/cyanogenmod/wallpapers/photophase/widgets/ResizeFrame.java @@ -0,0 +1,301 @@ +/* + * Copyright (C) 2013 The CyanogenMod 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 org.cyanogenmod.wallpapers.photophase.widgets; + +import android.content.Context; +import android.graphics.BitmapFactory; +import android.util.AttributeSet; +import android.util.DisplayMetrics; +import android.view.Gravity; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.RelativeLayout; + +import org.cyanogenmod.wallpapers.photophase.R; + +/** + * The hold view to resize a frame. A square with 4 handles in every border + * to drag and resize a view + */ +public class ResizeFrame extends RelativeLayout { + + /** + * An interface to communicate resize event states + */ + public interface OnResizeListener { + /** + * Called when the resize is going to start + * + * @param mode The resize mode (left, right, top, bottom) + * @see Gravity + */ + void onStartResize(int mode); + /** + * Called when the resize is going to start + * + * @param mode The resize mode (left, right, top, bottom) + * @param delta The delta motion + * @see Gravity + */ + void onResize(int mode, int delta); + /** + * Called when the resize was ended + * + * @param mode The resize mode (left, right, top, bottom) + * @see Gravity + */ + void onEndResize(int mode); + /** + * Called when the resize was cancelled + * + * @param mode The resize mode (left, right, top, bottom) + * @see Gravity + */ + void onCancel(); + } + + private int mNeededPadding; + + private ImageView mLeftHandle; + private ImageView mRightHandle; + private ImageView mTopHandle; + private ImageView mBottomHandle; + + private float mExtraHandlingSpace; + + private View mHandle; + + private float mLastTouchX; + private float mLastTouchY; + + private OnResizeListener mOnResizeListener; + + /** + * Constructor of <code>ResizeFrame</code>. + * + * @param context The current context + */ + public ResizeFrame(Context context) { + super(context); + init(); + } + + /** + * Constructor of <code>ResizeFrame</code>. + * + * @param context The current context + * @param attrs The attributes of the XML tag that is inflating the view. + */ + public ResizeFrame(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + /** + * Constructor of <code>ResizeFrame</code>. + * + * @param context The current context + * @param attrs The attributes of the XML tag that is inflating the view. + * @param defStyle The default style to apply to this view. If 0, no style + * will be applied (beyond what is included in the theme). This may + * either be an attribute resource, whose value will be retrieved + * from the current theme, or an explicit style resource. + */ + public ResizeFrame(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + /** + * Method that initializes the view + */ + @SuppressWarnings("boxing") + private void init() { + setBackgroundResource(R.drawable.resize_frame); + setPadding(0, 0, 0, 0); + + BitmapFactory.Options o = new BitmapFactory.Options(); + o.inJustDecodeBounds = true; + o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT; + BitmapFactory.decodeResource(getContext().getResources(), R.drawable.resize_handle_left, o); + mNeededPadding = (int)(o.outWidth / 1.5f); + + LayoutParams lp; + mLeftHandle = new ImageView(getContext()); + mLeftHandle.setImageResource(R.drawable.resize_handle_left); + mLeftHandle.setTag(Gravity.LEFT); + lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); + lp.addRule(RelativeLayout.CENTER_VERTICAL); + addView(mLeftHandle, lp); + + mRightHandle = new ImageView(getContext()); + mRightHandle.setImageResource(R.drawable.resize_handle_right); + mRightHandle.setTag(Gravity.RIGHT); + lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); + lp.addRule(RelativeLayout.CENTER_VERTICAL); + addView(mRightHandle, lp); + + mTopHandle = new ImageView(getContext()); + mTopHandle.setImageResource(R.drawable.resize_handle_top); + mTopHandle.setTag(Gravity.TOP); + lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); + lp.addRule(RelativeLayout.CENTER_HORIZONTAL); + addView(mTopHandle, lp); + + mBottomHandle = new ImageView(getContext()); + mBottomHandle.setImageResource(R.drawable.resize_handle_bottom); + mBottomHandle.setTag(Gravity.BOTTOM); + lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); + lp.addRule(RelativeLayout.CENTER_HORIZONTAL); + addView(mBottomHandle, lp); + + mExtraHandlingSpace = getResources().getDimension(R.dimen.resize_frame_extra_handling_space); + } + + /** + * Method that set the callback for resize events + * + * @param onResizeListener The callback + */ + public void setOnResizeListener(OnResizeListener onResizeListener) { + mOnResizeListener = onResizeListener; + } + + /** + * Method that hides the view + */ + public void hide() { + setVisibility(View.GONE); + } + + /** + * Method that shows the view + */ + public void show() { + setVisibility(View.VISIBLE); + } + + /** + * Method that returns the extra padding to draw the handlers + * + * @return The extra padding space + */ + public int getNeededPadding() { + return mNeededPadding; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean onTouchEvent(MotionEvent ev) { + final int action = ev.getAction(); + final float x = ev.getX(); + final float y = ev.getY(); + + switch (action) { + case MotionEvent.ACTION_DOWN: + mHandle = getHandleFromCoordinates(x, y); + if (mHandle != null) { + // Start moving the resize frame + mLastTouchX = x; + mLastTouchY = y; + + // Start motion + if (mOnResizeListener != null) { + mOnResizeListener.onStartResize(((Integer)mHandle.getTag()).intValue()); + } + return true; + } + break; + + case MotionEvent.ACTION_MOVE: + if (mHandle != null) { + // Resize + if (mOnResizeListener != null) { + int handle = ((Integer)mHandle.getTag()).intValue(); + int delta = + handle == Gravity.LEFT || handle == Gravity.RIGHT + ? Math.round(x - mLastTouchX) + : Math.round(y - mLastTouchY); + mOnResizeListener.onResize(handle, delta); + invalidate(); + } + mLastTouchX = x; + mLastTouchY = y; + return true; + } + break; + + case MotionEvent.ACTION_UP: + if (mHandle != null) { + if (mOnResizeListener != null) { + mOnResizeListener.onEndResize(((Integer)mHandle.getTag()).intValue()); + return true; + } + cancelMotion(); + break; + } + + //$FALL-THROUGH$ + case MotionEvent.ACTION_CANCEL: + cancelMotion(); + break; + + default: + break; + } + + return false; + } + + /** + * Cancel motions + */ + private void cancelMotion() { + mHandle = null; + mLastTouchX = 0; + mLastTouchY = 0; + if (mOnResizeListener != null) { + mOnResizeListener.onCancel(); + } + } + + /** + * Method that returns the resize handle touch from the the screen coordinates + * + * @param x The x coordinate + * @param y The y coordinate + * @return View The handle view or null if no handle touched + */ + private View getHandleFromCoordinates(float x, float y) { + final View[] handles = {mLeftHandle, mRightHandle, mTopHandle, mBottomHandle}; + for (View v : handles) { + if ((v.getLeft() - mExtraHandlingSpace) < x && (v.getRight() + mExtraHandlingSpace) > x && + (v.getTop() - mExtraHandlingSpace) < y && (v.getBottom() + mExtraHandlingSpace) > y) { + return v; + } + } + return null; + } +} |