1

我正在尝试制作一组​​按钮滑入和滑出的动画,但我正在努力解决一个问题。当前动画正在正确移动,但正在从屏幕的最左侧滑出。用于显示此内容的按钮在哪里。所以这使得整个动画看起来很糟糕,因为这个组正在滑动。

我想要实现的是添加一些边距,或者类似的东西,这将使动画从按钮的位置开始。

我希望它看起来像这样:

利润

这意味着这 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>
4

2 回答 2

2

试试这个代码:

TranslateAnimation anim = new TranslateAnimation(-50, 0, 0, 0);
anim.setStartOffset(0);
anim.setDuration(3000);
imageView.startAnimation(anim);

希望能帮助到你!!

于 2013-08-01T10:38:54.083 回答
0

在这种情况下,您可以将 Padding 添加到父相对布局中,动画从这样的填充开始

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="64dp">
于 2015-12-08T13:09:53.030 回答