我正在尝试制作一组按钮滑入和滑出的动画,但我正在努力解决一个问题。当前动画正在正确移动,但正在从屏幕的最左侧滑出。用于显示此内容的按钮在哪里。所以这使得整个动画看起来很糟糕,因为这个组正在滑动。
我想要实现的是添加一些边距,或者类似的东西,这将使动画从按钮的位置开始。
我希望它看起来像这样:
这意味着这 3 个图像将从这条白线开始显示,而不是从屏幕的最左侧开始。
主要活动onCreate
,一切都在进行:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (LinearLayout)findViewById(R.id.imageLayout);
button = (Button)findViewById(R.id.buttonShow);
animationSlideInLeft = AnimationUtils.loadAnimation(this,
R.anim.push_right_in);
animationSlideOutRight = AnimationUtils.loadAnimation(this,
R.anim.push_left_out);
animationSlideInLeft.setDuration(1000);
animationSlideOutRight.setDuration(1000);
animationSlideInLeft.setAnimationListener(animationSlideInLeftListener);
animationSlideOutRight.setAnimationListener(animationSlideOutRightListener);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
if(v == button)
{
if(!on)
{
curSlidingImage = image1;
image1.startAnimation(animationSlideInLeft);
image1.setVisibility(View.VISIBLE);
on = true;
}
else
{
image1.startAnimation(animationSlideOutRight);
image1.setVisibility(View.INVISIBLE);
on = false;
}
}
}
});
}
动画文件:
push_right_in
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="-100%p"
android:toXDelta="0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
push_left_out
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/buttonShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Btn" />
<LinearLayout
android:id="@+id/imageLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" >
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon" />
</LinearLayout>
</LinearLayout>