7
我在ViewFlipper上使用GestureDetector來實現showPrevious和showNext。 一切都很好。不過,我想要讓ViewFlipper動畫馬上跟隨手指姿勢,而不是在完成姿勢之後。 有何建議?立即跟隨手勢的Android ViewFlipper
我在ViewFlipper上使用GestureDetector來實現showPrevious和showNext。 一切都很好。不過,我想要讓ViewFlipper動畫馬上跟隨手指姿勢,而不是在完成姿勢之後。 有何建議?立即跟隨手勢的Android ViewFlipper
據我所知,你必須手動完成。下面的代碼應該指出你正確的方向。您必須添加動態動畫,以便視圖在翻轉過程中不閃爍。您需要做更多的事情,具體取決於viewflipper中的內容,但應該讓您開始。
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
//Gets the startpoint for you finger
pressDownPoint = event.getX();
//Gets how many view there is in the viewflipper
flipperViewCount = flipper.getChildCount();
//Checks if there is a view to the left of the current view
//if there is, it positions it to the left of the current view
if (flipper.getDisplayedChild() > 0)
{
View leftChild = flipper.getChildAt(flipper.getDisplayedChild() - 1);
//You must set the left view to invisible or visible
//or it will not move to the position you tell it
leftChild.setVisibility(View.INVISIBLE);
leftChild.layout(-screenWidth,
leftChild.getTop(), 0,
leftChild.getBottom());
}
//Same as above but for the view to the right
if (flipper.getDisplayedChild() < flipperViewCount - 1)
{
View rightChild = flipper.getChildAt(flipper.getDisplayedChild() + 1);
rightChild.setVisibility(View.INVISIBLE);
rightChild.layout(screenWidth,
rightChild.getTop(), screenWidth * 2,
rightChild.getBottom());
}
break;
}
case MotionEvent.ACTION_UP:
{
//Gets the absolute position on the screen
float releasePoint = event.getRawX();
//Calculates if the fling is to the right or left
//The screenDensity variable is simply the density of the device
//Have in mind that this will not flipp the viewflipper if you drag
//your finger less than about 0.5cm (depeding on the device)
//In that case you need to make an animation that takes the view back
//to its original position. Else it will just get stuck where you
//let go with your finger.
if (Math.abs(pressDownPoint - releasePoint)/screenDensity > 30)
{
if (pressDownPoint > releasePoint)
{
myAnimLeft(); //Method with your animation
flipper.showNext();
}
else
{
myAnimRight();
flipper.showPrevious();
}
}
break;
}
case MotionEvent.ACTION_MOVE:
{
View currentView = flipper.getCurrentView();
//Moves the current view
//screenWidth is based on the current devices screen width
currentView.layout((int)(event.getRawX() - pressDownPoint),
currentView.getTop(), (int)(event.getRawX() - pressDownPoint) + screenWidth,
currentView.getBottom());
//Moves the view to the left if there is one
if (flipper.getDisplayedChild() > 0)
{
View leftChild = flipper.getChildAt(flipper.getDisplayedChild() - 1);
leftChild.layout((int)(event.getRawX() - pressDownPoint - screenWidth),
leftChild.getTop(), (int)(event.getRawX() - pressDownPoint),
leftChild.getBottom());
//Sets the left view to visible so it shows
if (leftChild.getVisibility() == View.INVISIBLE)
{
leftChild.setVisibility(View.VISIBLE);
}
}
//Same as above but for the view to the right
if (flipper.getDisplayedChild() < flipperViewCount - 1)
{
View rightChild = flipper.getChildAt(flipper.getDisplayedChild() + 1);
rightChild.layout((int)(event.getRawX() - pressDownPoint + screenWidth),
rightChild.getTop(), (int)(event.getRawX() - pressDownPoint + (screenWidth * 2)),
rightChild.getBottom());
if (rightChild.getVisibility() == View.INVISIBLE)
{
rightChild.setVisibility(View.VISIBLE);
}
}
}
}
return true;
}