Android基础总结五:ImageSwitcher的使用
来源:互联网 发布:sqlserver 字段默认值 编辑:程序博客网 时间:2024/06/01 08:01
ImageSwitcher是图片切换器,常用于应用的引导界面的设计,下面来总结下它的简单用法:
布局中定义ImageSwitcher
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageSwitcher android:layout_width="match_parent" android:layout_height="match_parent" android:id = "@+id/id_imageswitcher"></ImageSwitcher></RelativeLayout>
代码中实现
public class testActivity extends FragmentActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener{ ImageSwitcher imageSwitcher; /** * 当前选中的图片id序号 */ private int currentPosition; /** * 按下点的X坐标 */ private float downX; private int[] imgIds = new int[]{R.drawable.new1,R.drawable.new2,R.drawable.new3}; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testactivity); imageSwitcher = (ImageSwitcher)findViewById(R.id.id_imageswitcher); imageSwitcher.setFactory(this); imageSwitcher.setOnTouchListener(this); } @Override public View makeView() { final ImageView i = new ImageView(this); i.setBackgroundColor(0xff000000); i.setScaleType(ImageView.ScaleType.CENTER_CROP); i.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); return i ; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN:{ //手指按下的X坐标 downX = event.getX(); break; } case MotionEvent.ACTION_UP:{ float lastX = event.getX(); //抬起的时候的X坐标大于按下的时候就显示上一张图片 if(lastX > downX){ if(currentPosition > 0){ //设置动画,这里的动画比较简单,不明白的去网上看看相关内容 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out)); currentPosition --; imageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]); // setImageBackground(currentPosition); }else{ Toast.makeText(getApplication(), "已经是第一张", Toast.LENGTH_SHORT).show(); } } if(lastX < downX){ if(currentPosition < imgIds.length - 1){ imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_out)); currentPosition ++ ; imageSwitcher.setImageResource(imgIds[currentPosition]); // setImageBackground(currentPosition); }else{ Toast.makeText(getApplication(), "到了最后一张", Toast.LENGTH_SHORT).show(); } } } break; } return true; }}
主要是继承了ViewSwitcher.ViewFactory方法,重写makeView()
阅读全文
0 0
- Android基础总结五:ImageSwitcher的使用
- android ImageSwitcher的使用
- android ImageSwitcher的使用
- Android中Gallery和ImageSwitcher的使用
- Android中Gallery和ImageSwitcher的使用
- Android 中textSwitcher与imageSwitcher的使用
- Android的ImageSwitcher效果
- android ImageSwitcher的用法
- Android ImageSwitcher的用法!
- ImageSwitcher的使用
- ImageSwitcher的使用
- ImageSwitcher的使用
- ImageSwitcher的使用
- ImageSwitcher的使用
- ImageSwitcher的使用
- imageSwitcher的使用
- ImageSwitcher的使用
- Android 基础控件之ImageSwitcher、Gallery功能的实现
- 自定义异常
- git的webhook应用
- Eclipse中 sysout 按alt+/为什么不出System.out.println();
- Linux 如何改变输出的内容
- AndroidStudio中debug快捷键
- Android基础总结五:ImageSwitcher的使用
- nyojzb的生日
- Jenkins入门系列之——01第一章 Jenkins是什么?
- laravel的orderBy问题
- stm32 RS485 SP3485
- 什么是Punycode-
- find与grep兄弟两的基情
- mysql5.5 mysql5.6 mysql5.7官方手册
- Web后台管理系统框架模板