Skip to content

Commit

Permalink
corrected animation for diagonal dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
askerov committed Jul 6, 2014
1 parent 464c9b7 commit 4c13c13
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
4 changes: 4 additions & 0 deletions dynamicgrid/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dgv_overlap_if_switch_straight_line">16dp</dimen>
</resources>
2 changes: 1 addition & 1 deletion dynamicgrid/res/values/id.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="dynamic_grid_wobble_tag"/>
<item type="id" name="dgv_wobble_tag"/>
</resources>
55 changes: 31 additions & 24 deletions dynamicgrid/src/org/askerov/dynamicgrid/DynamicGridView.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
import android.widget.GridView;
import android.widget.ListAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.*;

/**
* Author: alex askerov
Expand All @@ -50,6 +46,9 @@ public class DynamicGridView extends GridView {
private int mLastEventY = -1;
private int mLastEventX = -1;

//used to distinguish straight line and diagonal switching
private int mOverlapIfSwitchStraightLine;

private List<Long> idList = new ArrayList<Long>();

private long mMobileItemId = INVALID_ID;
Expand Down Expand Up @@ -84,7 +83,7 @@ public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
mMobileItemId = getAdapter().getItemId(position);

if (mSelectedItemBitmapCreationListener != null) {
mSelectedItemBitmapCreationListener.OnPreSelectedItemBitmapCreation(selectedView, position, mMobileItemId);
mSelectedItemBitmapCreationListener.onPreSelectedItemBitmapCreation(selectedView, position, mMobileItemId);
}

mHoverCell = getAndAddHoverView(selectedView);
Expand All @@ -94,7 +93,7 @@ public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id)
mCellIsMobile = true;

if (mSelectedItemBitmapCreationListener != null) {
mSelectedItemBitmapCreationListener.OnPostSelectedItemBitmapCreation(selectedView, position, mMobileItemId);
mSelectedItemBitmapCreationListener.onPostSelectedItemBitmapCreation(selectedView, position, mMobileItemId);
}

updateNeighborViewsForId(mMobileItemId);
Expand Down Expand Up @@ -248,12 +247,12 @@ private void undoModification(DynamicGridModification modification) {
private void startWobbleAnimation() {
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
if (v != null && Boolean.TRUE != v.getTag(R.id.dynamic_grid_wobble_tag)) {
if (v != null && Boolean.TRUE != v.getTag(R.id.dgv_wobble_tag)) {
if (i % 2 == 0)
animateWobble(v);
else
animateWobbleInverse(v);
v.setTag(R.id.dynamic_grid_wobble_tag, true);
v.setTag(R.id.dgv_wobble_tag, true);
}
}
}
Expand All @@ -268,7 +267,7 @@ private void stopWobble(boolean resetRotation) {
View v = getChildAt(i);
if (v != null) {
if (resetRotation) v.setRotation(0);
v.setTag(R.id.dynamic_grid_wobble_tag, false);
v.setTag(R.id.dgv_wobble_tag, false);
}
}
}
Expand All @@ -283,6 +282,7 @@ public void init(Context context) {
setOnScrollListener(mScrollListener);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
mSmoothScrollAmountAtEdge = (int) (SMOOTH_SCROLL_AMOUNT_AT_EDGE * metrics.density + 0.5f);
mOverlapIfSwitchStraightLine = getResources().getDimensionPixelSize(R.dimen.dgv_overlap_if_switch_straight_line);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
Expand Down Expand Up @@ -657,10 +657,14 @@ private void handleCellSwitch() {
&& deltaYTotal > view.getTop() && deltaXTotal > view.getLeft()
|| belowLeft(targetColumnRowPair, mobileColumnRowPair)
&& deltaYTotal > view.getTop() && deltaXTotal < view.getRight()
|| above(targetColumnRowPair, mobileColumnRowPair) && deltaYTotal < view.getBottom())
|| below(targetColumnRowPair, mobileColumnRowPair) && deltaYTotal > view.getTop()
|| right(targetColumnRowPair, mobileColumnRowPair) && deltaXTotal > view.getLeft()
|| left(targetColumnRowPair, mobileColumnRowPair) && deltaXTotal < view.getRight()) {
|| above(targetColumnRowPair, mobileColumnRowPair)
&& deltaYTotal < view.getBottom() - mOverlapIfSwitchStraightLine
|| below(targetColumnRowPair, mobileColumnRowPair)
&& deltaYTotal > view.getTop() + mOverlapIfSwitchStraightLine
|| right(targetColumnRowPair, mobileColumnRowPair)
&& deltaXTotal > view.getLeft() + mOverlapIfSwitchStraightLine
|| left(targetColumnRowPair, mobileColumnRowPair)
&& deltaXTotal < view.getRight() - mOverlapIfSwitchStraightLine)) {
float xDiff = Math.abs(DynamicGridUtils.getViewX(view) - DynamicGridUtils.getViewX(mobileView));
float yDiff = Math.abs(DynamicGridUtils.getViewY(view) - DynamicGridUtils.getViewY(mobileView));
if (xDiff >= vX && yDiff >= vY) {
Expand Down Expand Up @@ -764,7 +768,8 @@ private void animateReorder(final int oldPosition, final int newPosition) {
for (int pos = Math.min(oldPosition, newPosition); pos < Math.max(oldPosition, newPosition); pos++) {
View view = getViewForId(getId(pos));
if ((pos + 1) % getColumnCount() == 0) {
resultList.add(createTranslationAnimations(view, -view.getWidth() * (getColumnCount() - 1), 0, view.getHeight(), 0));
resultList.add(createTranslationAnimations(view, -view.getWidth() * (getColumnCount() - 1), 0,
view.getHeight(), 0));
} else {
resultList.add(createTranslationAnimations(view, view.getWidth(), 0, 0, 0));
}
Expand All @@ -773,7 +778,8 @@ private void animateReorder(final int oldPosition, final int newPosition) {
for (int pos = Math.max(oldPosition, newPosition); pos > Math.min(oldPosition, newPosition); pos--) {
View view = getViewForId(getId(pos));
if ((pos + getColumnCount()) % getColumnCount() == 0) {
resultList.add(createTranslationAnimations(view, view.getWidth() * (getColumnCount() - 1), 0, -view.getHeight(), 0));
resultList.add(createTranslationAnimations(view, view.getWidth() * (getColumnCount() - 1), 0,
-view.getHeight(), 0));
} else {
resultList.add(createTranslationAnimations(view, -view.getWidth(), 0, 0, 0));
}
Expand Down Expand Up @@ -871,15 +877,15 @@ private void updateWobbleState(int visibleItemCount) {
View child = getChildAt(i);

if (child != null) {
if (mMobileItemId != INVALID_ID && Boolean.TRUE != child.getTag(R.id.dynamic_grid_wobble_tag)) {
if (mMobileItemId != INVALID_ID && Boolean.TRUE != child.getTag(R.id.dgv_wobble_tag)) {
if (i % 2 == 0)
animateWobble(child);
else
animateWobbleInverse(child);
child.setTag(R.id.dynamic_grid_wobble_tag, true);
child.setTag(R.id.dgv_wobble_tag, true);
} else if (mMobileItemId == INVALID_ID && child.getRotation() != 0) {
child.setRotation(0);
child.setTag(R.id.dynamic_grid_wobble_tag, false);
child.setTag(R.id.dgv_wobble_tag, false);
}
}

Expand Down Expand Up @@ -941,18 +947,19 @@ public void checkAndHandleLastVisibleCellChange() {
};

public interface OnSelectedItemBitmapCreationListener {
public void OnPreSelectedItemBitmapCreation(View selectedView, int position, long itemId);
public void OnPostSelectedItemBitmapCreation(View selectedView, int position, long itemId);
public void onPreSelectedItemBitmapCreation(View selectedView, int position, long itemId);

public void onPostSelectedItemBitmapCreation(View selectedView, int position, long itemId);
}
}

class DynamicGridModification {

private List<Pair<Integer,Integer>> transitions;
private List<Pair<Integer, Integer>> transitions;

DynamicGridModification() {
super();
this.transitions = new Stack<Pair<Integer,Integer>>();
this.transitions = new Stack<Pair<Integer, Integer>>();
}

public boolean hasTransitions() {
Expand All @@ -963,7 +970,7 @@ public void addTransition(int oldPosition, int newPosition) {
transitions.add(new Pair<Integer, Integer>(oldPosition, newPosition));
}

public List<Pair<Integer,Integer>> getTransitions() {
public List<Pair<Integer, Integer>> getTransitions() {
Collections.reverse(transitions);
return transitions;
}
Expand Down

0 comments on commit 4c13c13

Please sign in to comment.