图片切换和屏幕切换

 1 package com.example.qiehuan;
 2 
 3 import com.example.qiehuan.R.id;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.support.v4.view.MotionEventCompat;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 import android.view.MotionEvent;
11 import android.view.View;
12 import android.widget.ImageSwitcher;
13 import android.widget.ImageView;
14 import android.widget.ImageView.ScaleType;
15 import android.widget.ViewSwitcher;
16 
17 public class MainActivity extends Activity {
18     private int[] image={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};
19     int index=0;
20     double startX=0.0;
21     double endX=0.0;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         final ImageSwitcher is=(ImageSwitcher) findViewById(id.imageSwitcher1);
27         is.setFactory(new ViewSwitcher.ViewFactory() {//装图片的工厂
28             @Override
29             public View makeView() {
30                 ImageView iv=new ImageView(MainActivity.this);
31                 iv.setScaleType(ScaleType.CENTER_INSIDE);
32                 iv.setImageResource(image[index]);
33                 return iv;
34             }
35         });
36         is.setOnTouchListener(new View.OnTouchListener() {//触摸事件
37             @Override
38             public boolean onTouch(View v, MotionEvent event) {
39                 
40                 if(event.getAction()==MotionEvent.ACTION_DOWN){
41                     startX=event.getX();
42                     return true;//按下去
43                 }
44                 if(event.getAction()==MotionEvent.ACTION_UP){
45                     endX=event.getX();
46                     System.out.println("start="+startX);
47                     System.out.println("endX="+endX);
48                     if(endX-startX > 20){//向右划
49                         index=index-1>0?--index:image.length-1;
50                         System.out.println("向右划");
51                         is.setInAnimation(MainActivity.this, android.R.anim.fade_in);
52                         is.setOutAnimation(MainActivity.this, android.R.anim.fade_out);
53                         is.setImageResource(image[index]);
54                     }else if(startX-endX>20){//向左划
55                         index=index+1<image.length? ++index:0;
56                         System.out.println("向左划");
57                         is.setInAnimation(MainActivity.this, android.R.anim.fade_in);
58                         is.setOutAnimation(MainActivity.this, android.R.anim.fade_out);
59                         is.setImageResource(image[index]);
60                     }
61                 }
62                 return false;
63             }
64         });
65 
66     }
67 
68 }

activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.qiehuan.MainActivity" >
10 
11     <ImageSwitcher
12         android:id="@+id/imageSwitcher1"
13         android:layout_width="match_parent"
14         android:layout_height="match_parent"
15        >
16     </ImageSwitcher>
17 
18 </RelativeLayout>

上面为图片切换,其中还一个TextSwitcher文字切换,原理和图片切换一样

 

下面为ViewFlipper 屏幕切换 能够实现多种布局之间的切换

 1 package com.example.qiehuan;
 2 
 3 import com.example.qiehuan.R.id;
 4 
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.support.v4.view.MotionEventCompat;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 import android.view.MotionEvent;
11 import android.view.View;
12 import android.widget.ImageSwitcher;
13 import android.widget.ImageView;
14 import android.widget.ImageView.ScaleType;
15 import android.widget.ViewFlipper;
16 import android.widget.ViewSwitcher;
17 
18 public class MainActivity2 extends Activity {
19     double startX=0;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.viewfilpper_layout);
24         final ViewFlipper vf=(ViewFlipper) findViewById(id.viewFlipper1);
25         vf.setOnTouchListener(new View.OnTouchListener() {
26             @Override
27             public boolean onTouch(View v, MotionEvent event) {
28                 if(event.getAction()==MotionEvent.ACTION_DOWN){
29                     startX=event.getX();
30                     return true;
31                 }else if(event.getAction()==MotionEvent.ACTION_UP){
32                     if(event.getX()>startX){//右划
33                         System.out.println("右划");
34                         vf.showPrevious();
35                     }else if(event.getX()<startX){//左划
36                         vf.showNext();
37                     }
38                 }
39                 return false;
40             }
41         });
42     }
43     
44 }

对应的 viewfilpper_layout.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <ViewFlipper
 8         android:id="@+id/viewFlipper1"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent" >
11         
12         <LinearLayout android:id="@+id/linear1"
13             android:orientation="vertical"
14             android:layout_width="match_parent"
15             android:layout_height="match_parent">
16             <ImageView android:layout_width="match_parent"
17                 android:layout_height="match_parent"
18                 android:src="@drawable/a1"
19                 android:maxWidth="100dp"
20                 android:maxHeight="500dp"
21                 android:adjustViewBounds="true"
22                 />
23         </LinearLayout>
24         
25          <LinearLayout android:id="@+id/linear2"
26             android:orientation="vertical"
27             android:layout_width="match_parent"
28             android:layout_height="match_parent">
29             <ImageView android:layout_width="match_parent"
30                 android:layout_height="match_parent"
31                 android:src="@drawable/a2"
32                 android:scaleType="centerCrop"/>
33         </LinearLayout>
34         
35           <LinearLayout android:id="@+id/linear3"
36             android:orientation="vertical"
37             android:layout_width="match_parent"
38             android:layout_height="match_parent">
39             <ImageView android:layout_width="match_parent"
40                 android:layout_height="match_parent"
41                 android:src="@drawable/a3"
42                 android:scaleType="centerCrop"/>
43         </LinearLayout>
44         
45           
46         
47     </ViewFlipper>
48 
49 </LinearLayout>

 

posted @ 2016-04-16 15:18  ts-android  阅读(386)  评论(0编辑  收藏  举报