Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420132
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: Android平台

2015-03-18 17:22:03

ImageSwitcher和TextSwitcher是的子类,都是为了在选择视图时用Animation动画来展示。

下面给出代码:

点击(此处)折叠或打开

  1. package cn.com.xiebiao.test;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnTouchListener;
  7. import android.view.ViewGroup;
  8. import android.view.animation.AnimationUtils;
  9. import android.widget.ImageSwitcher;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.RelativeLayout.LayoutParams;
  13. import android.widget.Toast;
  14. import android.widget.ViewSwitcher.ViewFactory;

  15. public class MainActivity extends Activity implements ViewFactory, OnTouchListener{
  16.     //ImagaSwitcher 的引用
  17.     private ImageSwitcher mImageSwitcher;
  18.     //图片id数组
  19.     private int[] imgIds;
  20.     // 当前选中的图片id序号
  21.     private int currentPosition;
  22.     //按下点的X坐标
  23.     private float downX;
  24.     // 装载当前状态点的线性布局
  25.     private LinearLayout linearLayout;
  26.     // 当前状态点数组
  27.     private ImageView[] tips;

  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_main);

  32.         //图片数组,可以在SD卡中读取
  33.         imgIds = new int[]{R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4};
  34.         //实例化ImageSwitcher
  35.         mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);

  36.         /*****************设置Factory***********************/
  37.         mImageSwitcher.setFactory(this);

  38.         //设置OnTouchListener,通过Touch事件来切换图片
  39.         mImageSwitcher.setOnTouchListener(this);

  40.         //当前状态点的显示布局
  41.         linearLayout = (LinearLayout) findViewById(R.id.viewGroup);

  42.         tips = new ImageView[imgIds.length];
  43.         for(int i=0; i<imgIds.length; i++){
  44.             ImageView mImageView = new ImageView(this);
  45.             tips[i] = mImageView;
  46.             LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
  47.                     LayoutParams.WRAP_CONTENT));
  48.             layoutParams.rightMargin = 10;
  49.             layoutParams.leftMargin = 10;

  50.             mImageView.setBackgroundResource(R.drawable.tip1);
  51.             linearLayout.addView(mImageView, layoutParams);
  52.         }

  53.         //默认的图片为第一张
  54.         currentPosition =0;
  55.         mImageSwitcher.setImageResource(imgIds[currentPosition]);
  56.         setImageBackground(currentPosition);
  57.     }

  58.     /**
  59.      * 设置选中的tip的背景
  60.      * @param selectItems
  61.      * tip1为未被选中的背景,灰色圆
  62.      * tip2为被选中的背景,红色圆
  63.      */
  64.     private void setImageBackground(int selectItems){
  65.         for(int i=0; i<tips.length; i++){
  66.             if(i == selectItems){
  67.                 tips[i].setBackgroundResource(R.drawable.tip2);
  68.             }else{
  69.                 tips[i].setBackgroundResource(R.drawable.tip1);
  70.             }
  71.         }
  72.     }

  73.     /*ViewFactory接口
  74.     要将图片显示在ImageSwitcher控件中,必须为ImageSwitcher类设置一个ViewFactory,用来将显示的图片和父窗口区分开来。这可以通过如下方法来实现:
  75.     mImageSwitcher.setFactory();
  76.     此外,我们还需要实现ViewSwitcher.ViewFactory接口中的makeView()抽象方法,通过该方法可以返回一个ImageView对象,所有图片都将通过该ImageView对象来进行显示。*/
  77.     @Override
  78.     public View makeView() {
  79.         final ImageView imageView = new ImageView(MainActivity.this);
  80.         //setScaleType设置了图片的显示方式
  81.         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  82.         return imageView ;
  83.     }

  84.     @Override
  85.     public boolean onTouch(View v, MotionEvent event) {
  86.         switch (event.getAction()) {
  87.             case MotionEvent.ACTION_DOWN:{
  88.                 //手指按下的X坐标
  89.                 downX = event.getX();
  90.                 break;
  91.             }
  92.             case MotionEvent.ACTION_UP:{
  93.                 float lastX = event.getX();
  94.                 //抬起的时候的X坐标大于按下的时候就显示上一张图片
  95.                 if(lastX > downX){
  96.                     if(currentPosition > 0){
  97.                         //设置动画
  98.                         mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));
  99.                         mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_out));
  100.                         currentPosition --;
  101.                         mImageSwitcher.setImageResource(imgIds[currentPosition % imgIds.length]);
  102.                         setImageBackground(currentPosition);
  103.                     }else{
  104.                         Toast.makeText(getApplication(), "已经是第一张", Toast.LENGTH_SHORT).show();
  105.                     }
  106.                 }

  107.                 if(lastX < downX){
  108.                     if(currentPosition < imgIds.length - 1){
  109.                         mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.right_in));
  110.                         mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_out));
  111.                         currentPosition ++ ;
  112.                         mImageSwitcher.setImageResource(imgIds[currentPosition]);
  113.                         setImageBackground(currentPosition);
  114.                     }else{
  115.                         Toast.makeText(getApplication(), "已经是最后一张", Toast.LENGTH_SHORT).show();
  116.                     }
  117.                 }
  118.             }

  119.             break;
  120.         }
  121.         return true;
  122.     }

activity_main.xnl

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <FrameLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent" >
  5.     <ImageSwitcher
  6.         android:id="@+id/imageSwitcher1"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="fill_parent">
  9.     </ImageSwitcher>

  10.     <RelativeLayout
  11.         android:layout_width="fill_parent"
  12.         android:layout_height="wrap_content"
  13.         android:orientation="vertical" >
  14.         <LinearLayout
  15.             android:id="@+id/viewGroup"
  16.             android:layout_width="fill_parent"
  17.             android:layout_height="wrap_content"
  18.             android:layout_alignParentBottom="true"
  19.             android:layout_marginBottom="30dp"
  20.             android:gravity="center_horizontal"
  21.             android:orientation="horizontal" >
  22.         </LinearLayout>
  23.     </RelativeLayout>

left_in.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <set xmlns:android="">
  3.     <translate
  4.         android:fromXDelta="-100%"
  5.         android:toXDelta="0"
  6.         android:duration="500"/>
  7. </set>

tip1.xml

  1. <shape xmlns:android=""
  2.     android:shape="oval">
  3.     <solid android:color="#8d9a95"/>
  4.     <size android:width="15dp"
  5.         android:height="15dp"/>
  6. </shape>



其它几个xml差不多,就不贴出来了 
阅读(905) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~