检查翻译视图是否已达到父级边缘?

时间:2014-08-02 00:10:15

标签: android eclipse animation

我有一个被拖到屏幕上的视图。在应用下一次转换之前,如何检查视图是否已到达其父级的边缘?

全班https://github.com/thuytrinh/android-collage-views/blob/master/libraries/collage-views/src/main/java/com/thuytrinh/android/collageviews/MultiTouchListener.java

这是我目前的代码:

 case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position.
            int pointerIndex = event.findPointerIndex(mActivePointerId);
            if (pointerIndex != -1) {
                float currX = event.getX(pointerIndex);
                float currY = event.getY(pointerIndex);

                // Only move if the ScaleGestureDetector isn't processing a
                // gesture.
                if (!mScaleGestureDetector.isInProgress()) {
                    adjustTranslation(view, currX - mPrevX, currY - mPrevY);
                }
            }

            break;
        }

private static void adjustTranslation(View view, float deltaX, float deltaY) {
    float[] deltaVector = {deltaX, deltaY};
    view.getMatrix().mapVectors(deltaVector);

    view.setTranslationX(view.getTranslationX() + deltaVector[0]);
    view.setTranslationY(view.getTranslationY() + deltaVector[1]);
}

xml:拖动的视图是拼贴视图(ID为“CollageView1”

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:padding="5dp"
    android:background="#545454">

    <FrameLayout
        android:layout_width="295dp"
        android:layout_height="170dp"
        android:splitMotionEvents="true" >

        <ImageView
            android:id="@+id/collageBgView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="#868686" />

        <com.thuytrinh.android.collageviews.CollageView
            android:id="@+id/collageView1"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/stamp_003" />
    </FrameLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

必须检查X ad Y翻译,以下是代码:

用于X翻译:

   private static boolean hasReachedEdgeX(View v, float newTransX){
    if(canCrossParentBoundries){
        return false;
    }
    Boolean reachedEdge = true;
    int adjustedWidth = (int) ((v.getWidth() * v.getScaleX() - v.getWidth()) / 2.f);
    int viewLeft = v.getLeft() - adjustedWidth;
    int viewRight = v.getRight() + adjustedWidth;
    View p = (View) v.getParent();
    ViewGroup.MarginLayoutParams pLayoutParams = (ViewGroup.MarginLayoutParams) p.getLayoutParams();
    int pLeft = p.getLeft() - pLayoutParams.leftMargin;
    int pRight = p.getRight() - pLayoutParams.leftMargin - pLayoutParams.rightMargin;
    float newViewRight = viewRight + newTransX;
    float newViewLeft = viewLeft + newTransX;
    //checks if the view has reached the boundaries of its parent
    if((newViewLeft > pLeft) && (newViewRight < pRight)){
        reachedEdge = false;
    }
    return reachedEdge;
}

对于Y翻译:

    private static boolean hasReachedEdgeY(View v, float newTransY){
    if(canCrossParentBoundries){
        return false;
    }
    Boolean reachedEdge = true;
    int adjustedHeight = (int) ((v.getHeight() * v.getScaleY() - v.getHeight()) / 2.f);
    int viewTop = v.getTop() - adjustedHeight;
    int viewBottom = v.getBottom() + adjustedHeight;
    View p = (View) v.getParent();
    ViewGroup.MarginLayoutParams pLayoutParams = (ViewGroup.MarginLayoutParams) p.getLayoutParams();
    int parentTop = p.getTop() - pLayoutParams.topMargin;
    int parentBottom = p.getBottom() - pLayoutParams.topMargin - pLayoutParams.bottomMargin;
    float newViewTop = viewTop + newTransY;
    float newViewBottom = viewBottom + newTransY;
    //checks if the view has reached the boundaries of its parent
    if((newViewBottom < parentBottom) && (newViewTop > parentTop)){
        reachedEdge = false;
    }
    return reachedEdge;
}

我希望能帮助谁解决这个问题。要获得完整代码,请访问github ripo: https://github.com/kcochibili/android-collage-views/blob/master/libraries/collage-views/src/main/java/com/thuytrinh/android/collageviews/MultiTouchListener.java