事件监听---图片浏览器

事件监听---图片浏览器

code:

1.布局

 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=".CopyImageshowActivity" >
10 
11     <ImageView
12         android:id="@+id/iv_show"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:layout_alignParentTop="true"
16         android:layout_centerHorizontal="true"
17         android:layout_marginTop="74dp"
18         android:src="@drawable/t301_chicken" />
19 
20     <Button
21         android:id="@+id/btn_up"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_alignParentLeft="true"
25         android:layout_below="@+id/iv_show"
26         android:layout_marginLeft="37dp"
27         android:layout_marginTop="28dp"
28         android:text="上一页" />
29 
30     <Button
31         android:id="@+id/btn_down"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:layout_alignBottom="@+id/btn_up"
35         android:layout_alignParentRight="true"
36         android:layout_marginRight="43dp"
37         android:text="下一页" />
38   
39 </RelativeLayout>

 

2.Java代码

public class CopyImageshowActivity extends Activity {
    
    
    ImageView iv_show;
    Button btn_up;
    Button btn_down;
    
    int current;//当前页
    int[] imgSrc = {R.drawable.t301_chicken,R.drawable.t301_cow,R.drawable.t301_dog,R.drawable.t301_horse,R.drawable.t301_mokey,R.drawable.t301_rat};//图片的集合
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_copy_imageshow);
        
        iv_show = (ImageView) findViewById(R.id.iv_show);
        btn_up = (Button) findViewById(R.id.btn_up);
        btn_down = (Button) findViewById(R.id.btn_down);
        
        current = 0;//为当前页面设置初始值
        iv_show.setImageResource(imgSrc[current]);//设置当前图片
        
        
        btn_up.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                
                if (current == 0) {//判断是否是第一页,如果是那么返回到最后一页
                    current =  imgSrc.length -1;
                    iv_show.setImageResource(imgSrc[current]);
                }else {
                    current --;
                    iv_show.setImageResource(imgSrc[current]);
                }
                
            }
        });
        
        
        btn_down.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (current == imgSrc.length-1) {
                    current =0;
                    iv_show.setImageResource(imgSrc[current]);
                }else {
                    current++;
                    iv_show.setImageResource(imgSrc[current]);
                    
                }
            }
        });
        
    }

    
}

 

posted @ 2016-06-22 09:53  悟知清风  阅读(356)  评论(0编辑  收藏  举报