android 控件显示和隐藏时增加动画效果

来源:互联网 发布:c int数组默认值 编辑:程序博客网 时间:2024/06/16 04:13

 在布局中提供属性,能简单的添加动画效果,如下:

<LinearLayout   ...   animateLayoutChanges="true"   ... />

当对布局中的view添加删除,隐藏或显示, 都会有一个淡入淡出,和位移动画.这是个默认动画

从网上搜到的方法基本都是这个,虽然能够实现动画效果,只是不知道该如何自定义动画效果。

后来想到在做项目时用到的viewswitcher控件能够实现子控件显示和消失的动画效果,对其进行改造发现可以实现自定义控件的显示和消失,下面将我的方案给出,希望看到此文的android高手不吝赐教,指点迷津,能够给出更好的方案。

布局文件:

android_main_test.xml

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/firsttext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:padding="10dp"
            android:text="第一个text"
            android:textSize="15sp" />


        <ViewSwitcher
            android:id="@+id/viewswitch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inAnimation="@anim/slide_in_top"   
            android:outAnimation="@anim/slide_out_top"
            android:persistentDrawingCache="animation"
            android:visibility="gone" >


            <LinearLayout
                android:id="@+id/nextlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/white"
                    android:padding="10dp"
                    android:text="第二个text"
                    android:textSize="15sp" />
            </LinearLayout>


            <View
                android:layout_width="match_parent"
                android:layout_height="0dp" />
        </ViewSwitcher>
    </LinearLayout>


</LinearLayout>

设置控件显示和消失动画的地方:

android:inAnimation="@anim/slide_in_top"   
 android:outAnimation="@anim/slide_out_top"

动画 文件:

slide_in_top.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:fromYDelta="-50%p" android:toYDelta="0"
    android:duration="300"/>

slide_out_top.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromYDelta="0" android:toYDelta="-50%p"
    android:duration="300"/>


测试代码文件:

public class MainTestActivity extends Activity{
private TextView text;
private LinearLayout layout;
private ViewSwitcher mSwitch;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
text = (TextView) findViewById(R.id.firsttext);
layout = (LinearLayout) findViewById(R.id.nextlayout);
mSwitch = (ViewSwitcher) findViewById(R.id.viewswitch);
text.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
if (mSwitch.getVisibility() == View.VISIBLE) {
mSwitch.showNext();
} else {
mSwitch.setVisibility(View.VISIBLE);
mSwitch.setDisplayedChild(0);
}
}
});
}

}

这里是把ViewSwitcher的第二个子控件设置了一个高0dp的view,这样在第一次执行showNext()时显示的是一个0dp的控件,就达到了类似控件不显示的效果。其实当viewswitch变成visible后,mSwitch.getVisibility() 一直是VISIBLE的。这时候显示和消失其实是通过showNext进行了一个布局和一个高度为0的布局间的切换。


1 0
原创粉丝点击