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的布局间的切换。
- android 控件显示和隐藏时增加动画效果
- Android 动画1--View控件的显示和隐藏效果
- Android 动画1--View控件的显示和隐藏效果
- Android 动画1--View控件的显示和隐藏效果
- [Android] View控件显示隐藏动画效果
- 控件隐藏/显示时的动画效果
- JQuery 动画显示和隐藏效果
- JQuery 动画显示和隐藏效果
- Android控件显示/隐藏时添加动画:ViewSwitcher
- android---控件隐藏与显示动画
- android开发滑动srcollerview右下角按钮根据手势显示和隐藏动画简单动画效果
- View控件的显示和隐藏效果
- Android控件显示和隐藏
- Android控件显示和隐藏
- Android控件显示和隐藏
- Android 控件显示和隐藏
- Android控件显示和隐藏
- Android控件显示和隐藏
- 下载 windows系统 的网站
- Fragment详解之三——管理Fragment(1)
- eclipse Maven配置struts2详解——总结网上教程
- GoLang环境搭建
- 批量改一个文件夹下的文件格式,可以很好的防止病毒入侵,懂原理,有源码
- android 控件显示和隐藏时增加动画效果
- java性能优化读书笔记之二《设计优化===享元模式》
- 创建Tomcat web时出现错误提示:unbound classpath variable
- 地理空间数据下载 积累
- java.lang.UnsatisfiedLinkError: Couldn't load easemob_jni from loader dalvik.system.PathClassLoader
- PFX文件提取公钥私钥
- android wear-Drawing Watch Faces
- 2016.1.16 git学习 .gitignore格式规范
- Fragment详解之四——管理Fragment(2)