Use Imageswitcher to change images for gradient effects
<LinearLayoutxmlns: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"android:gravity= "Center_horizontal"> <GridViewAndroid:id= "@+id/gridview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:horizontalspacing= "2pt"android:verticalspacing= "2pt"android:gravity= "Center"Android:numcolumns= "4" > </GridView> <ImageswitcherAndroid:id= "@+id/imageswitcher1"Android:layout_width= "300DP"Android:layout_height= "300DP"android:layout_gravity= "Center_horizontal"android:inanimation= "@android: anim/fade_in"android:outanimation= "@android: Anim/fade_out" > </Imageswitcher> </LinearLayout>
PackageCom.example.imageswitcher;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.AdapterView.OnItemSelectedListener;ImportAndroid.widget.GridView;ImportAndroid.widget.ImageSwitcher;ImportAndroid.widget.ImageView;ImportAndroid.widget.SimpleAdapter;Importandroid.widget.ViewSwitcher.ViewFactory; Public classMainactivityextendsActivity {int[] imageids=New int[]{r.drawable.bomb5,r.drawable.bomb6,r.drawable.bomb7,r.drawable.bomb8, R.DRAWABLE.BOMB9,R.DRAWABLE.BOMB10, R.DRAWABLE.BOMB11,R.DRAWABLE.BOMB12, R.DRAWABLE.BOMB13,R.DRAWABLE.BOMB14,R.DRAWABLE.BOMB15,R.DRAWABLE.BOMB16}; Imageswitcher switcher; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); List<Map<String,Object>> listitems=NewArraylist<map<string,object>>(); for(inti=0;i<imageids.length;i++) {Map<String,Object> listitem=NewHashmap<string,object>(); Listitem.put ("Image", Imageids[i]); Listitems.add (ListItem); } Switcher=(Imageswitcher) Findviewbyid (r.id.imageswitcher1); Switcher.setfactory (Newviewfactory () {@Override PublicView Makeview () {ImageView ImageView=NewImageView (mainactivity. This); Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER); Imageview.setlayoutparams (NewImageswitcher.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content)); returnImageView; } }); Simpleadapter Simpleadapter=NewSimpleadapter ( This, ListItems, R.layout.cell,Newstring[]{"Image"},New int[]{r.id.image1}]; GridView Grid=(GridView) Findviewbyid (R.id.gridview); Grid.setadapter (Simpleadapter); Grid.setonitemselectedlistener (NewOnitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?>Parent, view view,intPositionLongID) {switcher.setimageresource (imageids[position]); } @Override Public voidOnnothingselected (adapterview<?>parent) { } }); Grid.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> Parent, view view,intposition,LongID) {switcher.setimageresource (imageids[position]); }}); }}
Android 25th Lesson--imageswitcher