[android ui]动画特效的应用----ViewSwitcher控件
来源:互联网 发布:人工智能外文参考文献 编辑:程序博客网 时间:2024/05/21 17:54
为了让活动能不断更新视图控件的内容,为每一个视图控件绑定动画特效显得非常繁琐,但是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};
- @Override
- protected 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
0 0
- [android ui]动画特效的应用----ViewSwitcher控件
- 动画特效的应用----ViewSwitcher控件
- Android控件显示/隐藏时添加动画:ViewSwitcher
- Android的UI特效
- [应用代码] Android 动画特效大全
- Android动画特效的实现
- Android 动画特效的运用
- Android的四个动画特效
- android控件之ViewSwitcher实现图片切换
- android中ViewSwitcher的使用
- android ViewSwitcher的用法介绍
- Android中viewSwitcher的使用
- Android自定义控件之扫描动画UI
- Android ViewSwitcher
- Android中UI设计的特效
- [Android UI] 波浪特效的实现
- Android UI控件之RecyclerView的简单应用
- Android 动画特效 破碎的界面 brokview
- Ad Rotator组件使用例子
- 如何使用KeyChain保存和获取UDID
- Dicom打印工作站
- Fast Fourier Transform
- 事务隔离级别
- [android ui]动画特效的应用----ViewSwitcher控件
- 请问下mtk双卡手机如何发短信是如何选择sim卡来发(双卡都可用的情况下)?
- 常用的读写ini文件的类
- 在 Access 中使用“存储过程”
- 网站排名不稳定的原因以及解决方法。
- asp中将table表中的数据成功导成excel做法
- Android UI开发第四十一篇——墨迹天气3.0引导界面及动画实现
- SNMP简单网络管理协议
- .NET平台下WEB应用程序的部署(安装数据库和自动配置)