Preface
This chapter is about android. widget. ImageSwitcher. The version is Android 2.2 r1 and translated from "wallace2010". You are welcome to visit his blog: http://blog.csdn.net/springiscoming. Thank you again "wallace2010 "! Welcome to join in Android API Chinese translation, contact me over140@gmail.com.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog Garden: http://www.cnblogs.com/
Android Chinese translation group: http://www.cnblogs.com/over140/
Body
I. Structure
Public class ImageSwitcher extends ViewSwitcher
Java. lang. Object
Android. view. View
Android. view. ViewGroup
Android. widget. FrameLayout
Android. widget. ViewAnimator
Android. widget. ViewSwitcher
Android. widget. ImageSwitcher
Ii. Overview
(Note: ImageSwitcher is a widget in Android that controls the image display effect, such as the slide effect .)
Iii. Public Methods
Public void setImageDrawable (Drawable drawable)
Draw Images
Public void setImageResource (int resid)
Set image resource library
Public void setImageURI (Uri uri)
Set image address
Iv. Supplement
4.1 article link
Android ImageSwitcher
Image Switcher View | Android Developer Tutorial
4.2 Sample Code (this code is reposted here)
Java files
Public class mainactivity extends Activity implements
OnItemSelectedListener, ViewFactory {
Private ImageSwitcher is;
Private Gallery gallery;
Private Integer [] mThumbIds = {R. drawable. B, R. drawable. c,
R. drawable. d, R. drawable. f, R. drawable. g,
};
Private Integer [] mImageIds = {R. drawable. B, R. drawable. c,
R. drawable. d, R. drawable. f, R. drawable. g ,};
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. main );
Is = (ImageSwitcher) findViewById (R. id. switcher );
Is. setFactory (this );
Is. setInAnimation (AnimationUtils. loadAnimation (this,
Android. R. anim. fade_in ));
Is. setOutAnimation (AnimationUtils. loadAnimation (this,
Android. R. anim. fade_out ));
Gallery = (Gallery) findViewById (R. id. gallery );
Gallery. setAdapter (new ImageAdapter (this ));
Gallery. setOnItemSelectedListener (this );
}
@ Override
Public View makeView (){
ImageView I = new ImageView (this );
I. setBackgroundColor (0xFF000000 );
I. setScaleType (ImageView. ScaleType. FIT_CENTER );
I. setLayoutParams (new ImageSwitcher. LayoutParams (
LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT ));
Return I;
}
Public class ImageAdapter extends BaseAdapter {
Public ImageAdapter (Context c ){
MContext = c;
}
Public int getCount (){
Return mThumbIds. length;
}
Public Object getItem (int position ){
Return position;
}
Public long getItemId (int position ){
Return position;
}
Public View getView (int position, View convertView, ViewGroup parent ){
ImageView I = new ImageView (mContext );
I. setImageResource (mThumbIds [position]);
I. setAdjustViewBounds (true );
I. setLayoutParams (new Gallery. LayoutParams (
LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT ));
I. setBackgroundResource (R. drawable. e );
Return I;
}
Private Context mContext;
}
@ Override
Public void onItemSelected (AdapterView <?> Parent, View view, int position,
Long id ){
Is. setImageResource (mImageIds [position]);
}
@ Override
Public void onNothingSelected (AdapterView <?> Parent ){
}
}
Xml file
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<ImageSwitcher android: id = "@ + id/switcher"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: layout_alignParentTop = "true"
Android: layout_alignParentLeft = "true"
/>
<Gallery android: id = "@ + id/gallery"
Android: background = "#55000000"
Android: layout_width = "match_parent"
Android: layout_height = "60dp"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentLeft = "true"
Android: gravity = "center_vertical"
Android: spacing = "16dp"
/>
</RelativeLayout>
End
Wallace2010 made such a nice image--# attracting Emperor ......