Points:
(View API Summary)
1:imageswitcher inherit Viewswitcher, (Viewswitcher has inheritance framelayout)
2: To implement the cut graph must implement the Makeview () in the Viewswitcher.viewfactory interface
Easy Error Point:
1:
Mimageswitcher.setfactory (this);
Mimageswitcher.setimageresource (Mimages[0]);
Setimageresource must be called after setfactory. Reason: According to the source code analysis call Setfactory get interface, can
Gets the view returned by Makeview () otherwise throws a null pointer exception
2: Set layout parameters in Makeview with framelayout layout parameters
Package Com.imageswitcher;import Android.app.activity;import Android.os.bundle;import android.util.Log;import Android.view.view;import Android.view.viewgroup;import Android.view.animation.animationutils;import Android.widget.framelayout;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.toast;import Android.widget.viewswitcher;public Class Mainactivity extends Activity implements Viewswitcher.viewfactory {private LinearLayout mlinearlayout; Private Imageswitcher Mimageswitcher; Private integer[] Mimages = {r.drawable.android01, r.drawable.android02, R.DRAWABLE.ANDROID03, R.DRAWABLE.ANDROID04, R.DRAWABLE.ANDROID05, R.drawable.android06, r.drawable.android07, r.drawable.android08, R.drawable.android09}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); MimageswiTcher.setfactory (this); Mimageswitcher.setimageresource (Mimages[0]); Mimageswitcher.setinanimation (Animationutils.loadanimation (this, Android. r.anim.fade_in)); Mimageswitcher.setoutanimation (Animationutils.loadanimation (this, Android. R.anim.fade_out)); for (int i = 0; i < mimages.length; i++) {Mlinearlayout.addview (Getimageview (i)); }} private ImageView Getimageview (final int i) {ImageView ImageView = new ImageView (this); Imageview.setimageresource (Mimages[i]); Imageview.setid (i); Imageview.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Mimageswitcher.setimageresource (Mimages[i]); Toast.maketext (V.getcontext (), "selected" + (V.getid () + 1) + "map", Toast.length_short). Show (); } }); return imageView; } private void Initview () {mlinearlayout = (linearlayout) Findviewbyid (R.id.linEarlayout); Mimageswitcher = (imageswitcher) Findviewbyid (R.id.imageswitcher); } @Override Public View Makeview () {ImageView ImageView = new ImageView (this); LOG.I (MainActivity.class.getSimpleName (), "First Call"); Imageview.setlayoutparams (New Framelayout.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, viewgroup.layou Tparams.match_parent)); return imageView; }}
Xml
<Relativelayoutxmlns: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"Tools:context=". Mainactivity "> <ImageswitcherAndroid:id= "@+id/imageswitcher"Android:layout_width= "Match_parent"Android:layout_height= "200DP"> </Imageswitcher> <HorizontalscrollviewAndroid:layout_below= "@id/imageswitcher"Android:layout_width= "Match_parent"Android:layout_height= "100DP" > <LinearLayoutAndroid:id= "@+id/linearlayout"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"> </LinearLayout> </Horizontalscrollview></Relativelayout>
Android's Imageswitcher