ImageSwitcher与ViewSwitcher
来源:互联网 发布:c语言教学入门视频 编辑:程序博客网 时间:2024/05/22 05:26
ImageSwitcher
基础
参考:http://www.cnblogs.com/plokmju/p/android_ImageSwitcher.html
用于图片的切换。与ImageView类似,只不过在图片的切换过程中,可以设置动画。
设置
在布局中:android:inAnimation与android:outAnimation
在代码中:switcher.setInAnimation与switcher.setOutAnimation
使用
在使用之前必须要调用switcher.setFactroy()方法,示例:
switcher.setFactory(new ViewFactory() {public View makeView() {return new ImageView(MainActivity.this);}});只需要在makeView()方法中返回一个ImageView即可。而switcher就是负责把图片展示在返回的ImageView上。
示例
public class MainActivity extends Activity {private final int[] indexs = {R.drawable.a1,R.drawable.a6,R.drawable.arrow,R.drawable.ic_launcher};private int index = 0;private Button btn_left;private Button btn_right;private ImageSwitcher switcher;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.animation_3);switcher = (ImageSwitcher) findViewById(R.id.switcher);switcher.setFactory(new ViewFactory() {public View makeView() {return new ImageView(MainActivity.this);}});switcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));switcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));btn_left = (Button)findViewById(R.id.btn_left);btn_right = (Button)findViewById(R.id.btn_right);if(index == 0){btn_left.setClickable(false);}else if(index == indexs.length){btn_right.setClickable(false);}if(indexs.length == 1){btn_left.setClickable(false);btn_right.setClickable(false);}switcher.setImageResource(indexs[index]);}public void left(View v){index--;btn_right.setClickable(true);if(index < 0){btn_left.setClickable(false);return;}else if(index == 0){btn_left.setClickable(false);switcher.setImageResource(indexs[index]);}else{btn_left.setClickable(true);switcher.setImageResource(indexs[index]);}}public void right(View v){index++;btn_left.setClickable(true);if(index > indexs.length-1){btn_right.setClickable(false);return;}else if(index == indexs.length - 1){btn_right.setClickable(false);switcher.setImageResource(indexs[index]);}else{btn_right.setClickable(true);switcher.setImageResource(indexs[index]);}}}布局文件:
<?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" > <ImageSwitcher android:layout_weight="1" android:id="@+id/switcher" android:inAnimation="" android:outAnimation="" android:layout_width="match_parent" android:layout_height="0dp" android:background="#ffff00" /> <LinearLayout android:layout_gravity="bottom" android:weightSum="2" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_weight="1" android:id="@+id/btn_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="left" android:text="上一张" /> <Button android:layout_weight="1" android:id="@+id/btn_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="right" android:text="下一张" /> </LinearLayout></LinearLayout>
ViewSwitcher
功能
上述的ImageSwitcher只是用于图片的切换。而ViewSwitcher可以用于任何组件的切换。只要在ViewFactory类中的makeView()中返回相应的View即可。而且ImageSwitcher是ViewSwitcher的子类,类似的还有TextSwitcher。
使用
和ImageSwitcher不同的是:ImageSwitcher只要设置image即可。而ViewSwitcher返回的不一定是一个View,有可能是一个ViewGroup或其子类,因此使用过程如下:
第一步:通过getNextView()获取下一个要显示的组件,并对其中的组件进行操作。
第二步:通过showNext()显示下一张组件,或者showPrevious()显示上一个组件。
示例
private ViewSwitcher vs;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);findViewById(R.id.btn).setOnClickListener(new OnClickListener() {public void onClick(View v) {((ImageView) vs.getNextView()).setImageResource(R.drawable.bkg_img_default);vs.showNext();}});vs = (ViewSwitcher) findViewById(R.id.vs);vs.setFactory(new ViewFactory() {public View makeView() {//此时类似于ImageSwitcherreturn new ImageView(MainActivity.this);}});vs.setInAnimation(this, R.anim.test);vs.setOutAnimation(this, R.anim.test_inter);}
0 0
- ImageSwitcher与ViewSwitcher
- ViewSwitcher,ImageSwitcher,TextSwitcher,ViewAnimator
- Android ViewSwitcher、TextSwitcher、ImageSwitcher
- Android----ViewFlipper,ViewSwitcher(ImageSwitcher,TextSwitcher)
- android viewanimator viewfilter viewswitcher textswitcher imageswitcher
- Androi UI 详解之ViewAnimator(ViewSwitcher,ImageSwitcher,TextSwitcher,ViewFlipper)
- ViewSwitcher
- ViewSwitcher
- TextSwitcher与ViewSwitcher学习笔记
- ImageSwitcher与GestureDetector
- Gallery与ImageSwitcher总结
- Android ViewSwitcher 的功能与用法
- Android ViewSwitcher 的功能与用法
- Android ViewSwitcher 的功能与用法
- Android ViewSwitcher 的功能与用法
- ViewSwitcher的功能与用法View
- <ViewSwitcher>的简单功能与用法
- ViewSwitcher的功能与用法View
- androidactivity去除标题栏和状态栏
- 学车记
- HDU2028(解题报告)
- java
- YII中使用CActiveDataProvider、CDbCriteria和CGridView实现订单详情的显示
- ImageSwitcher与ViewSwitcher
- 一个快速将十六进制串转十进制数的方法
- linux内核空间和用户空间
- Spring MVC和Struts2的区别
- java
- 汇率计算器的代码
- hi3531的hifb显示1080p60Hz
- ant全自动更新部署web程序的详解
- Linux 原始套接字抓包实例