动画从顶部到按钮

时间:2014-05-09 16:35:23

标签: android android-layout view android-animation

我正在尝试从顶部到按钮制作动画。

如果我点击一个按钮,他应该从顶部显示一个视图。如果我再次点击,他应该将其动画回到顶部。这就是我所拥有的:

MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button btFilterDown = (Button) findViewById(R.id.btFilterDown);
    Button btFilterUp = (Button) findViewById(R.id.btFilterUp);
    final View layout = findViewById(R.id.slide);
    layout.setVisibility(View.GONE);

    btFilterUp.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        hideView(layout);
        }
    });

    btFilterDown.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        showView(layout);
        }
    });
}

private void hideView(final View view){
    Animation animation = AnimationUtils.loadAnimation(this, R.layout.slide_out_up);
    animation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            view.setVisibility(View.GONE);
        }
    });

    view.startAnimation(animation);
}

private void showView(final View view){
    Animation animation = AnimationUtils.loadAnimation(this, R.layout.slide_in_down);
    animation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            view.setVisibility(View.VISIBLE);
        }
    });

    view.startAnimation(animation);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Slide_in_down.xml:

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="-1000"
android:duration="@android:integer/config_longAnimTime" />

Slide_out_up.xml:

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:toXDelta="0"
android:toYDelta="-1000"
android:duration="@android:integer/config_longAnimTime" />

我的main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@android:color/darker_gray" >
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@android:color/white" >

    <Button 
        android:id="@+id/btFilterDown"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="FILTER!!!"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/slide"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#99009900" 
    android:orientation="vertical">

    <Button 
        android:id="@+id/btFilterUp"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text=" NO FILTER!!!"/>

</LinearLayout>

</LinearLayout>

所以现在我有两个问题:

第一: 如果第二个布局在其上,则我的第一个布局的按钮不再可见。我想制作一个动画的视图,透明度。但它看起来非常糟糕,如果第一个视图中的对象消失了。 如何在第一个布局上放置动画布局,以便第一个布局上的所有对象都可见?

第二: 动画应从动作栏的底部开始。我的意思是,如果布局进入,他开始在底部边缘而不是在屏幕顶部。 如何设置动画的起点?

非常感谢:)

2 个答案:

答案 0 :(得分:0)

你的动画必须有4 xml,如slide_down_in,slide_down_out,slide_up_in,slide_up_out,以及使用百分比,如

slide_down_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="-100%"
        android:toYDelta="0%"
        android:duration="500" />
</set>

slide_down_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="500" />
</set>

slide_up_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%"
        android:toYDelta="0%"
        android:duration="500" />
</set>

slide_up_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="-100%"
        android:duration="500" />
</set>

答案 1 :(得分:0)

正在运作

Intent i = new Intent(getActivity(), FeedbackSuccessActivity.class);
    i.putExtra("title", "Success!");
    i.putExtra("message", "\n Profile Update Successfully \n ");
    startActivity(i);
    overridePendingTransition(R.anim.slide_down_in, R.anim.slide_down_out);

<强> slide_down_in

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="-100%"
        android:toYDelta="0%"
        android:duration="500" />
</set>

<强> slide_down_out

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="500" />
</set>