2010-04-08 5 views
10

좋아요. ViewFlipper에는 3 개의 LinearLayouts이 중첩되어 있습니다. 기본적으로 첫 번째 것을 보여줍니다. 이 코드 :MotionEvent.ACTION_MOVE를 사용하여 홈 화면과 같은 ViewFlipper 만들기

// Assumptions in my Activity class: 
// oldTouchValue is a float 
// vf is my view flipper 
@Override 
public boolean onTouchEvent(MotionEvent touchEvent) { 
    switch (touchEvent.getAction()) { 
    case MotionEvent.ACTION_DOWN: { 
     oldTouchValue = touchEvent.getX(); 
     break; 
    } 
    case MotionEvent.ACTION_UP: { 
     float currentX = touchEvent.getX(); 
     if (oldTouchValue < currentX) { 
     vf.setInAnimation(AnimationHelper.inFromLeftAnimation()); 
     vf.setOutAnimation(AnimationHelper.outToRightAnimation()); 
     vf.showNext(); 
     } 
     if (oldTouchValue > currentX) { 
     vf.setInAnimation(AnimationHelper.inFromRightAnimation()); 
     vf.setOutAnimation(AnimationHelper.outToLeftAnimation()); 
     vf.showPrevious(); 
     } 
     break; 
    } 
    case MotionEvent.ACTION_MOVE: { 
     // TODO: Some code to make the ViewFlipper 
     // act like the home screen. 
     break; 
    } 
    } 
    return false; 
} 

public static class AnimationHelper { 
    public static Animation inFromRightAnimation() { 
    Animation inFromRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, +1.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f); 
    inFromRight.setDuration(350); 
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
    } 

    public static Animation outToLeftAnimation() { 
    Animation outtoLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, -1.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoLeft.setDuration(350); 
    outtoLeft.setInterpolator(new AccelerateInterpolator()); 
    return outtoLeft; 
    } 

    // for the next movement 
    public static Animation inFromLeftAnimation() { 
    Animation inFromLeft = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, -1.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f); 
    inFromLeft.setDuration(350); 
    inFromLeft.setInterpolator(new AccelerateInterpolator()); 
    return inFromLeft; 
    } 

    public static Animation outToRightAnimation() { 
    Animation outtoRight = new TranslateAnimation(
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, +1.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f, 
    Animation.RELATIVE_TO_PARENT, 0.0f); 
    outtoRight.setDuration(350); 
    outtoRight.setInterpolator(new AccelerateInterpolator()); 
    return outtoRight; 
    } 
} 

...보기가 뒤집기는하지만 애니메이션은 매우 "켜기/끄기"입니다. 나는 누군가가 마지막 부분으로 나를 도울 수 있는지 궁금해. LinearLayouts에 액세스 할 수 있다고 가정하면 deltaX 및 deltaY를 기반으로 레이아웃의 위치를 ​​설정할 수있는 방법이 있습니까?

누군가 나에게 this link을 주었고이 작업을 수행하는 방법에 대한 힌트는 applyTransformation 방법을 참조해야한다고 말했지만이 같은 동작을 반복하는 방법을 모르겠습니다. 당신이 당신의 손가락이 쉽게 이루어집니다 이동하는 동안

답변

10

보기를 이동 :

case MotionEvent.ACTION_MOVE: 
    final View currentView = vf.getCurrentView(); 
    currentView.layout((int)(touchEvent.getX() - oldTouchValue), 
      currentView.getTop(), currentView.getRight(), 
      currentView.getBottom()); 
break; 

지금이은 현재보기가 아닌 이웃을 이동합니다. 아직 테스트하지는 않았지만 아마도 getChildAt에서 얻을 수 있습니다.

vf.getChildAt(vf.getDisplayedChild() - 1); // previous 
vf.getChildAt(vf.getDisplayedChild() + 1); // next 

희망이 있습니다. 나는 귀하의 요청을 이해한다면

+0

좋아, 작품을! 나는 나머지 부분을 알아낼 수있다. 고마워. – DJTripleThreat

1

,이 효과는 이제 View Pager

이 같은데 사용하여 구현해야합니다

enter image description here

+0

이것은 다른 접근 방식입니다. http://www.androiduipatterns.com/2011/06/android-ui-pattern-workspaces.html – Kurru

+0

두 번째 링크는 무시하고, 호출기는이 작업을 수행하는 올바른 방법입니다. – Kurru

관련 문제