使用ImageSwitcher点击按钮浏览图片

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="250dp" 
        android:gravity="center">

        <ImageSwitcher
            android:id="@+id/IS01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            >
        </ImageSwitcher>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/pre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一张" />

        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一张" />
    </LinearLayout>

</LinearLayout>

package com.example.imageswitcher;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory, OnClickListener {
	
	private final static Integer[] INTEGERS = {
		R.drawable.a,
		R.drawable.d,
		R.drawable.c,
		R.drawable.d,
		R.drawable.e,
		R.drawable.f,
		R.drawable.g,
		R.drawable.h,
	};
	
	ImageSwitcher imageSwitcher;
	
	Button button,button2;
	
	private  static int index=0;
	
	private final static int DOWN_ID = 0x123456;
	private final static int UP_ID = 0x123456;
	private final static int SWICHER_ID = 0x123456;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		imageSwitcher = (ImageSwitcher)findViewById(R.id.IS01);
		button = (Button)findViewById(R.id.pre);
		button2 = (Button)findViewById(R.id.next);
		
		
		imageSwitcher.setFactory(this);
		imageSwitcher.setImageResource(INTEGERS[index]);
		
		button.setOnClickListener(this);
		button2.setOnClickListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public View makeView() {
		return new ImageView(this);
	}

	@Override
	public void onClick(View v) {
			switch (v.getId()) {
			case R.id.pre:
				index--;
				if(index<0){
					index = INTEGERS.length-1;
					
				}
				imageSwitcher.setImageResource(INTEGERS[index]);
				break;

			case R.id.next:
				index++;
				if(index>=INTEGERS.length){
					index = 0;	
				}
				imageSwitcher.setImageResource(INTEGERS[index]);
				
				break;
			}
		
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值