第五篇 ImageSwitcher篇

直奔主题~!

结构如图:

第五篇 ImageSwitcher篇

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

	android:orientation="vertical" android:layout_width="fill_parent"

	android:layout_height="fill_parent" android:gravity="center">

	<ImageSwitcher android:id="@+id/is"

		android:layout_width="fill_parent" android:layout_height="200dip"></ImageSwitcher>

	<LinearLayout android:layout_width="fill_parent"

		android:id="@+id/linearLayout1" android:layout_height="wrap_content"

		android:gravity="center">

		<Button android:text="上一页" android:id="@+id/up_btn"

			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

		<Button android:text="下一页" android:id="@+id/next_btn"

			android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

	</LinearLayout>



</LinearLayout>

Control_ImageSwitcherActivity.java代码:

public class Control_ImageSwitcherActivity extends Activity {

	

	ImageSwitcher is;

	Button next_btn;

	Button up_btn;

	int[] imggroup;

	int index=0;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        findAll();

        bind();

    }

    

    public void findAll()

    {

    	is=(ImageSwitcher) this.findViewById(R.id.is);

    	next_btn=(Button) this.findViewById(R.id.next_btn);

    	up_btn=(Button) this.findViewById(R.id.up_btn);

    }

    

    public void bind()

    {

    	imggroup=new int[]{R.raw.jwc1,R.raw.jwc2,R.raw.jwc3,R.raw.jwc4,R.raw.jwc5};

    	next_btn.setOnClickListener(mylistener);

    	up_btn.setOnClickListener(mylistener);

    	is.setFactory(new ViewFactory() {

			

    		   		

			public View makeView() {

				// TODO Auto-generated method stub

			//	return null;

				ImageView iv=new ImageView(Control_ImageSwitcherActivity.this);

				iv.setLayoutParams(new  ImageSwitcher.LayoutParams(100, 100));

				return iv;

			}

		});

    	is.setBackgroundResource(imggroup[index]);

    }

    

    

    

    private View.OnClickListener mylistener =new OnClickListener() {

		

		public void onClick(View v) {

			// TODO Auto-generated method stub

			switch(v.getId())

			{

			case R.id.up_btn:

				if(index>=1)

				{

				   index--;

				   is.setBackgroundResource(imggroup[index]);

				}

				else

				{

				  Toast.makeText(Control_ImageSwitcherActivity.this, "已经到了第一页", Toast.LENGTH_LONG).show();

				}

				break;

			case R.id.next_btn:

				if(index<(imggroup.length-1))

				{

				   index++;

				   is.setBackgroundResource(imggroup[index]);

				}

				else

				{

				  Toast.makeText(Control_ImageSwitcherActivity.this, "已经到了最后一页", Toast.LENGTH_LONG).show();

				}

				break;

				default:

					break;

			}

		}

	};

}

你可能感兴趣的:(ImageSwitcher)