我正在尝试在ViewFlipper中创建一个包含3个视图的幻灯片动画,当我在它们之间切换时,我得到一个空白区域。当我尝试在Action_Move中更改它们时,是否有任何方法可以删除视图之间的空间,以便它们看起来像是粘在一起?
这是XML:
<ViewFlipper
android:id="@+id/ViewFlipper1"
android:layout_height="300dp"
android:layout_width="300dp"
android:background="#000000">
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:background="#aabbcc"/>
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:background="#cfcfcf"/>
<TextView
android:layout_width="300dp"
android:layout_height="200dp"
android:background="#adadad"/>
</ViewFlipper>
这是我想要制作的动画:
float firstTouchX = 0;
float leaveTouchX = 0;
float firstTouchX2 = 0;
float leaveTouchX2 = 0;
float oldMoveTouchX = 0;
ViewFlipper VF = null;
public boolean dispatchTouchEvent(MotionEvent event) {
VF =(ViewFlipper) findViewById(R.id.ViewFlipper1);
int eventaction=event.getAction();
final View currentView = VF.getCurrentView();
switch(eventaction) {
case MotionEvent.ACTION_DOWN:
firstTouchX = event.getX();
oldMoveTouchX = firstTouchX;
break;
case MotionEvent.ACTION_MOVE:
int deltaMovement = (int)(event.getX() - oldMoveTouchX);
oldMoveTouchX = event.getX();
if(deltaMovement < 0 )
{
currentView.layout((int)(currentView.getLeft()+deltaMovement),
currentView.getTop(), (int)(currentView.getRight()+deltaMovement),
currentView.getBottom());
}
else
{
currentView.layout((int)(currentView.getLeft()+deltaMovement),
currentView.getTop(), (int)(currentView.getRight()+deltaMovement),
currentView.getBottom());
}
break;
case MotionEvent.ACTION_UP:
leaveTouchX = event.getX();
if (firstTouchX - 50 > leaveTouchX) {
VF.setInAnimation(AnimationHelper.inFromRightAnimation());
VF.setOutAnimation(AnimationHelper.outToLeftAnimation());
VF.showNext();
}
else if (firstTouchX + 50 < leaveTouchX) {
VF.setInAnimation(AnimationHelper.inFromLeftAnimation());
VF.setOutAnimation(AnimationHelper.outToRightAnimation());
VF.showPrevious();
}
break;
}
return true;
}
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(500);
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(500);
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(500);
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(500);
return outtoRight;
}
}
答案 0 :(得分:0)
在视图鳍状肢中: -
机器人:layout_height = “300dp” 机器人:layout_width = “300dp”
而是替换为android:layout_height="wrap_content"
android:layout_width="wrap_content"