Android动画-如何添加开始边距?
我试图制作一组按钮滑入滑出的动画,但我遇到了一个问题。 当前动画正在正确移动,但正在从屏幕的最左侧滑出。显示这个的按钮在哪里。这使得整个动画看起来很糟糕,因为这组人在这上面滑动 我想要实现的是添加一些边距,或者类似的东西,使动画从按钮所在的位置开始 我希望它看起来像这样: 这意味着这3幅图像将从这条白线开始显示,而不是从屏幕的最左侧开始显示 主要活动Android动画-如何添加开始边距?,android,android-animation,Android,Android Animation,我试图制作一组按钮滑入滑出的动画,但我遇到了一个问题。 当前动画正在正确移动,但正在从屏幕的最左侧滑出。显示这个的按钮在哪里。这使得整个动画看起来很糟糕,因为这组人在这上面滑动 我想要实现的是添加一些边距,或者类似的东西,使动画从按钮所在的位置开始 我希望它看起来像这样: 这意味着这3幅图像将从这条白线开始显示,而不是从屏幕的最左侧开始显示 主要活动onCreate,一切都在进行: @Override public void onCreate(Bundle savedInstance
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;
}
}
}
});
}
动画文件:
向右推压
<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>
向左推出
<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>
main.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>
尝试以下代码:
TranslateAnimation anim = new TranslateAnimation(-50, 0, 0, 0);
anim.setStartOffset(0);
anim.setDuration(3000);
imageView.startAnimation(anim);
希望能有帮助 您可以将填充添加到父相对布局中。在这种情况下,动画从填充开始,如下所示
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="64dp">
百分比在Android中是无用的。@sebap123尝试此编辑的代码,并根据您的要求减少xdelta值。@Armanstranger您可以在代码中显示它,因为动画是使用AnimationUtils添加的。loadAnimation
使用我在代码中向您展示的方法。为任何视图创建动画和开始动画。