【网学网提醒】:网学会员,鉴于大家对Android ImageSwitcher学习笔记(An-Beer工作室)十分关注,会员在此为大家搜集整理了“Android ImageSwitcher学习笔记(An-Beer工作室)”一文,供大家参考学习!
AndroidImageSwitcher学习笔记
ImageSwitcher是Android中控制
图片展示效果的一个控件,如:幻灯片效果...,颇有感觉啊。做相册一绝
源码
下载 一、重要方法setImageURI(Uriuri):设置图片地址setImageResource(intresid):设置图片资源库setImageDrawable(Drawabledrawable):绘制
图片二、实例
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"/>
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));
三、完整代码1.布局文件
android:spacing="16dp"/>
2.Java代码
packagewjq.WidgetDemo;importandroid.app.Activity;importandroid.content.Context;importandroid.os.Bundle;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.Window;importandroid.view.animation.AnimationUtils;importandroid.widget.AdapterView;importandroid.widget.BaseAdapter;importandroid.widget.Gallery;importandroid.widget.ImageSwitcher;importandroid.widget.ImageView;importandroid.widget.AdapterView.OnItemClickListener;importandroid.widget.AdapterView.OnItemSelectedListener;importandroid.widget.Gallery.LayoutParams;importandroid.widget.ViewSwitcher.ViewFactory;publicclassImageSwitcherDemoextendsActivityimplementsOnItemSelectedListener,ViewFactory{privateImageSwitcheris;privateGallerygallery;privateInteger[]mThumbIds={R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.f,R.drawable.g,};privateInteger[]mImageIds={R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.f,R.drawable.g,};/**(non-Javadoc)**@seeandroid.app.Activity#onCreate(android.os.Bundle)*/@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);requestWindo
wFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.imageswitcherpage);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(newImageAdapter(this));gallery.setOnItemSelectedListener(this);}@OverridepublicViewmakeView(){ImageViewi=newImageView(this);i.setBackgroundColor(0xFF000000);i.setScaleType(ImageView.ScaleType.FIT_CENTER);i.setLayoutParams(newImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));returni;}publicclassImageAdapterextendsBaseAdapter{publicImageAdapter(Contextc){mContext=c;}publicintgetCount(){returnmThumbIds.length;}publicObjectgetItem(intposition){returnposition;}publiclonggetItemId(intposition){returnposition;}publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ImageViewi=newImageView(mContext);i.setImageResource(mThumbIds[position]);i.setAdjustViewBounds(true);i.setLayoutParams(newGallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));i.setBackgroundResource(R.drawable.e);returni;}privateContextmContext;}
@OverridepublicvoidonItemSelected(AdapterView>parent,Viewview,intposition,longid){is.setImageResource(mImageIds[position]);}@OverridepublicvoidonNothingSelected(AdapterView>parent){//TODOAuto-generatedmethodstub}}