Android basics ImageSwitcher view (11)

Source: Internet
Author: User

1. ImageSwitcher (image switcher ):

When you want to click an image, you need to use ImageSwitcher to display it slowly.

We also need to use the ViewFactory interface, which is a view converter. We need to override the makeView () method to create a new view that is added to the view converter (ViewSwitcher.

Animation effect:

Effect of sliding from left to right:

Android. R. anim. slide_in_left

Android. R. anim. slide_out_right

Fade in and out:

Android. R. anim. fade_in

Android. R. anim. fade_out

Demo instance:

Layout file:

 
     
      
      
  
 

Custom Attributes (attrs. xml ):

 
     
              
  
 

Main Code:

Public class MainActivity extends Activity implements ViewFactory {Integer [] imgId = {R. drawable. img1, R. drawable. img2, R. drawable. img3, R. drawable. img4, R. drawable. img5, R. drawable. img8, R. drawable. img9 ,}; private ImageSwitcher is; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); is = (ImageSwitcher) findViewById (R. id. I MageSwitcher1); is. setFactory (this); // sets the animation effect. Slide from left to right into is. setInAnimation (AnimationUtils. loadAnimation (this, android. r. anim. slide_in_left); is. setOutAnimation (AnimationUtils. loadAnimation (this, android. r. anim. slide_out_right); Gallery gallery = (Gallery) findViewById (R. id. gallery1); gallery. setAdapter (new ImageAdapter (this); gallery. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
 Arg0, View arg1, int arg2, long arg3) {is. setImageResource (imgId [arg2]) ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true;} // create a new View to add it to the ImageView. @ Overridepublic View makeView () {ImageView imageView = new ImageView (this); // set the background color imageView. setBackgroundColor (0xffffff00); imageView. setScaleType (ImageView. scaleType. FIT_CENTER); // fill the widget. ImageView. setLayoutParams (new ImageSwitcher. LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT); return imageView;} // Image adapter class, bind Gallery. Private class ImageAdapter extends BaseAdapter {private Context context; private int item; public ImageAdapter (Context context) {this. context = context; TypedArray ta = context. obtainStyledAttributes (R. styleable. gallery1); item = ta. getResourceId (R. styleable. gallery1_android_galleryItemBackground, 0); ta. recycle () ;}@ Overridepublic int getCount () {return imgId. length ;}@ Overridepublic Object getItem (int arg0) {// TODO Auto-generated method stubreturn arg0 ;}@ Overridepublic long getItemId (int arg0) {// TODO Auto-generated method stubreturn arg0;} @ Overridepublic View getView (int arg0, View arg1, ViewGroup arg2) {// TODO Auto-generated method stubImageView image = new ImageView (context); image. setImageResource (imgId [arg0]); image. setScaleType (ImageView. scaleType. FIT_XY); image. setLayoutParams (new Gallery. layoutParams (160,130); image. setBackgroundResource (item); return image ;}}}


Running image:

When we click the image in Chapter 2:

When you click the image below:





Zookeeper

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.