动画特效的应用----ViewSwitcher控件
来源:互联网 发布:sql server r2 64位 编辑:程序博客网 时间:2024/05/22 08:13
为了让活动能不断更新视图控件的内容,为每一个视图控件绑定动画特效显得非常繁琐,但是Android SDK提供的ViewSwitcher控件,能够高效简便地更新视图。
ViewSwitcher有两个子视图控件,并处理从当前子视图到下一个子视图的过渡。ViewSwitcher的子视图控件是使用ViewFactory以编程方式生成的。
ViewSwitcher有两个子类:
→ TextSwitcher:让您能够在两个TextView控件之间切换。
→ ImageSwitcher:让您能够在两个ImageView控件之间切换。
下面以ImageSwitcher为例来说一下使用的步骤:
1、 制作两个动画特效switcher_in.xml和switcher_out.xml:
switcher_in.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="1.0" android:pivotX="99%" android:pivotY="99%" android:duration="500" /> <alpha android:interpolator="@android:anim/linear_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500"/></set>
switcher_out.xml:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="1.0" android:pivotX="1%" android:pivotY="1%" android:duration="500"/> <alpha android:interpolator="@android:anim/linear_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500"/></set>
2、 在布局文件中添加ImageSwitcher,并将刚创建的动画与其绑定:
<ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inAnimation="@anim/switcher_in" android:outAnimation="@anim/switcher_out"> </ImageSwitcher>
3、 为ImageSwitcher创建布局资源image_switcher_view.xml:
<?xml version="1.0" encoding="utf-8"?><ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" ></ImageView>
4、初始化ImageSwitcher控件,实现自定义ViewFactory类:
private ImageSwitcher imageSwitcher;private float touchDownX , touchUpX;int index = 0;int[] arrayImage = {R.drawable.a7 , R.drawable.a9 , R.drawable.a11 , R.drawable.a12};@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher);imageSwitcher.setFactory(new MyImageFacotry());imageSwitcher.setImageResource(arrayImage[index]);}private class MyImageFacotry implements ViewFactory{ public View makeView() {ImageView imageView = (ImageView)LayoutInflater.from(getApplicationContext()).inflate(R.layout.image_switcher_view, imageSwitcher, false); return imageView; }}
5、 为ImageSwitcher添加手势滑动监听效果:
imageSwitcher.setOnTouchListener(new OnTouchListener(){public boolean onTouch(View arg0, MotionEvent arg1) {if (arg1.getAction() == MotionEvent.ACTION_DOWN){touchDownX = arg1.getX();return true;}else if(arg1.getAction() == MotionEvent.ACTION_UP){touchUpX = arg1.getX();if (touchDownX - touchUpX > 100)//左滑{if (index >0 ) {imageSwitcher.setInAnimation(getApplicationContext(), R.anim.switcher_in_right);imageSwitcher.setOutAnimation(getApplicationContext(), R.anim.switcher_out_right);imageSwitcher.setImageResource(arrayImage[index]);index--;}}else { if (index < 3) { imageSwitcher.setInAnimation(getApplicationContext(), R.anim.switcher_in_left); imageSwitcher.setOutAnimation(getApplicationContext(), R.anim.switcher_out_left); imageSwitcher.setImageResource(arrayImage[index]); index++; } } return true;} return false; }});
源码下载地址:动画特效的应用----ViewSwitcher控件.docx
- 动画特效的应用----ViewSwitcher控件
- [android ui]动画特效的应用----ViewSwitcher控件
- Android控件显示/隐藏时添加动画:ViewSwitcher
- View控件--ViewSwitcher
- UIView的动画特效
- css3的动画特效
- ViewSwitcher的使用方法
- ViewSwitcher的简单使用
- ViewSwitcher的简单使用
- [应用代码] Android 动画特效大全
- ViewSwitcher
- ViewSwitcher
- Android动画特效的实现
- Android 动画特效的运用
- JQuery的动画与特效
- Android的四个动画特效
- 卸载系统应用&动画气泡特效&创建守护服务&进程管理(ActivtyManager&提供批量操作)&桌面小控件AppWidget
- 9款超绚丽的HTML5/CSS3应用和动画特效
- Mac点击关闭按钮退出程序 与 程序隐藏后点击dock图标重新显示
- Swinghacks——抗锯齿字体
- Ubuntu上安装eclipse与jdk的配置
- (四)分配排序:桶排序(Bucket Sort)
- 安卓关机(3)
- 动画特效的应用----ViewSwitcher控件
- Oracle数据库安装及配置
- Ubuntu12设置可以识别GBK
- Ural-1109-Conference
- 查找重复数据sql
- 使用OLEDB可以对excel文件进行读取1
- Xcode中工程配置后台运行的项目-- Required background modes
- Oracle 忘记用户名和密码的解决办法
- Python基础学习