Android advanced components ----- image switch, Android ----- Switch
Android image switch <ImageSwitch> is a component that allows you to play images in sequence. It is similar to "windows photo viewer". You can click the left and right buttons to view photos in sequence. ImageSwitch actually inherits ViewSwitch and overwrites the showNext () showprevious () method of ViewSwitch, which makes it very easy to view a previous image.
ImageSwitch provides a ViewFactory interface. The View component generated by ViewFactory must be ImageView. When switching images, you only need to call the setImageResource (int resid) method to change the image.
Implementation of the image switch:
1. Create a project and put the ImageSwitch component and two buttons in the layout.
<LinearLayout android: layout_width = "match_parent" android: layout_height = "match_parent" android: gravity = "center" android: orientation = "vertical"> <ImageSwitcher android: id = "@ + id/imageSwitcher1" android: layout_weight = "1" android: layout_gravity = "center" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </ImageSwitcher> <LinearLayout android: layout_weight = "1" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: gravity = "center_horizontal" android: orientation = "horizontal"> <Button android: id = "@ + id/button1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "previous"/> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Next"/> </LinearLayout>
2. Create an image id array and a button for the image switch object in the main activity.
Private int [] image = new int [] {R. drawable. photo1, R. drawable. photo2, R. drawable. photo3, R. drawable. photo4}; // Image array private int index = 0; // subscript private ImageSwitcher is; // Switch
Private Button up, down;
3. Get component instantiation and set ImageSwitch. setFactory ()
is = (ImageSwitcher)findViewById(R.id.imageSwitcher1); up = (Button)findViewById(R.id.button1); down = (Button)findViewById(R.id.button2); up.setOnClickListener(this); down.setOnClickListener(this); is.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; } }); is.setImageResource(image[index]); }
4. Modify the button and add a listener. The Listener interface is implemented in Activity. Note that after adding the listener event, the View component id is obtained. The switch is used to determine whether the clicked button is the previous or next button.
In fact, the listener interface uses the View listener interface. The returned object is View, which is obtained through View. getId ().
up.setOnClickListener(this); down.setOnClickListener(this); public void onClick(View v) { switch(v.getId()){ case R.id.button1: if(index > 0){ index --; }else { index = image.length - 1; } is.setImageResource(image[index]);break; case R.id.button2: if(index == image.length - 1){ index = 0; }else { index ++; } is.setImageResource(image[index]);break; } }
Running effect:
Conclusion: The View interface implemented by the main Activity is public void onClick (View v) {}. in this method, we team v to judge, the android component inherits from the View class.