android的ImageSwitcher和TextSwitcher

简介:

ImageSwitcher:

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     xmlns:tools= "http://schemas.android.com/tools"
     android:layout_width= "match_parent"
     android:layout_height= "match_parent"
     android:paddingBottom= "@dimen/activity_vertical_margin"
     android:paddingLeft= "@dimen/activity_horizontal_margin"
     android:paddingRight= "@dimen/activity_horizontal_margin"
     android:paddingTop= "@dimen/activity_vertical_margin"
     tools:context= "com.example.imageswitch.MainActivity"  >
 
     <ImageSwitcher
         android:id= "@+id/imageSwitcher1_1"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         android:layout_alignParentTop= "true"
        
         >
     </ImageSwitcher>
 
</RelativeLayout>
<!--    android:inAnimation= "@android:anim/slide_in_left"
         android:outAnimation= "@android:anim/slide_out_right"
          -->

MianActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package  com.example.imageswitch;
 
import  android.app.Activity;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.MenuItem;
import  android.view.MotionEvent;
import  android.view.View;
import  android.view.View.OnTouchListener;
import  android.widget.ImageSwitcher;
import  android.widget.ImageView;
import  android.widget.ViewSwitcher.ViewFactory;
 
public  class  MainActivity  extends  Activity  implements  ViewFactory ,OnTouchListener{
     private  ImageSwitcher imageSwitch;
     private  int  [] images={
             R.drawable.kobe0,
             R.drawable.kobe1,
             R.drawable.kobe3,
             R.drawable.kobe4,
             R.drawable.kobe6,
     };
     private  int  index;  //要显示的图片的下标
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         imageSwitch=(ImageSwitcher) findViewById(R.id.imageSwitcher1_1);
         //设置创建ImageView的工厂
         imageSwitch.setFactory( this );
         //设置触屏事件
         imageSwitch.setOnTouchListener( this );
     }
 
 
     
     //ViewFactory工厂接口的方法,通过这个方法,给ImageSwitch组件提供两个
     @Override
     public  View makeView() {
         // TODO Auto-generated method stub
         ImageView imageView= new  ImageView( this );
         imageView.setImageResource(images[ 0 ]);
         return  imageView;
     }
     //定义两个x坐标点
      float  startx= 0 .0F;       //开始位置
      float  endx= 0 .0F;         //结束位置
     //触屏事件监听方法
     @Override
     public  boolean  onTouch(View v, MotionEvent event) {
         if (event.getAction()==MotionEvent.ACTION_DOWN){
             startx=event.getX();
             return  true ;
         } else  if (event.getAction()==MotionEvent.ACTION_UP){
             endx=event.getX();
             //判断左滑动
             if (startx-endx> 20 ){
                 
                 index=(index+ 1 )<(images.length- 1 )?++index: 0 ;
                 imageSwitch.setImageResource(images[index]);
                 //在代码中设置动画效果
                 imageSwitch.setInAnimation( this , android.R.anim.fade_in);
                 imageSwitch.setOutAnimation( this , android.R.anim.fade_out);
             }
             
             //判断右滑动
             if (endx-startx> 20 ){
                 index=(index- 1 )> 0 ?--index:images.length- 1 ;
                 imageSwitch.setImageResource(images[index]);
                 imageSwitch.setInAnimation( this , android.R.anim.slide_in_left);
                 imageSwitch.setOutAnimation( this , android.R.anim.slide_out_right);
             }
         }
         return  true ;
     }
}

TextSwitcher与ImageSwitcher类似,只需要将ImageSwitcher换成TextSwitcher

设置图片资源修改为设置文本即可



 本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1882816

相关文章
|
Android开发 Java 数据格式
|
Android开发
Android零基础入门第55节:ImageSwitcher和TextSwitcher使用
原文:Android零基础入门第55节:ImageSwitcher和TextSwitcher使用     上一期我们了解了ViewAnimator组件和ViewSwitcher组件的使用,你都掌握了吗?本期一起来学习ViewSwitcher的两个子组件ImageSwitcher和TextSwitcher。
1596 0
|
XML Android开发 数据格式
我的Android进阶之旅------&gt;Android之Gallery和GridView两种方式与ImageSwitcher实现带预览的和幻灯片方式的两种图片浏览器
一、简介 a.GridView(网络视图)的功能和用法 b.ImageSwitcher(图形切换器)的功能和用法 c.Gallery(画廊视图)的功能和用法 二、通过一个实例来学习Gallery、GridView和I...
1233 0