Today, we are implementing a simple small program for image switching.
Use the imageswitcher class and set a viewfactory to implement its makeview () method to create a view for displaying images.
Method setimageresource is used to display the specified image resource.
1. Put 8 images in the/RES/drawable directory and name them sample_0, sample_1,... sample_7, etc;
2. Define the resource ID array in the Code;
static final Integer[] imagelist = {R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,};
Activity. Java
Package COM. luoye. allview; import android. app. activity; import android. OS. bundle; 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 secondactivity extends activity implements onclicklistener, viewfactory {imageswitcher; private button button_pre; private button button_next; static final integer [] imagelist = {R. drawable. sample_0, R. drawable. sample_1, R. drawable. sample_2, R. drawable. sample_3, R. drawable. sample_4, R. drawable. sample_5, R. drawable. sample_6, R. drawable. sample_7 ,}; Private Static int Index = 0; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. secondactivity); imageswitcher = (imageswitcher) findviewbyid (R. id. imageswitch); imageswitcher. setfactory (this); imageswitcher. setimageresource (imagelist [Index]); button_pre = (button) findviewbyid (R. id. button_pre); button_next = (button) findviewbyid (R. id. button_next); button_pre.setonclicklistener (this); button_next.setonclicklistener (this) ;}@ overridepublic view makeview () {// todo auto-generated method stubreturn new imageview (this );} @ overridepublic void onclick (view v) {// implement The onclick method of the button // todo auto-generated method stubswitch (v. GETID () {case R. id. button_next: Index ++; If (Index = imagelist. length) {Index = 0;} imageswitcher. setimageresource (imagelist [Index]); break; case R. id. button_pre: Index --; If (index <0) {Index = imagelist. length-1;} imageswitcher. setimageresource (imagelist [Index]); break; default: break ;}}}
Layout file:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"> <imageswitcher Android: id = "@ + ID/imageswitch" Android: layout_width = "wrap_content" Android: layout_height = "100dp"> </imageswitcher> <button Android: id = "@ + ID/button_next" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "previous"/> <button Android: id = "@ + ID/button_pre" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Next"/> </linearlayout>
Effect:
Click the next one to implement switching.