Android中Gallery和ImageSwitcher的使用实例
程序员文章站
2022-07-04 20:29:13
效果如下:
布局文件activity_main.xml如下:
效果如下:
布局文件activity_main.xml如下:
<relativelayout xmlns: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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <imageswitcher android:id="@+id/imageswitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:paddingtop="30px" > </imageswitcher> <gallery android:id="@+id/gallery1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:spacing="5px" android:unselectedalpha="0.6" /> </relativelayout>
mainactivity.java代码如下:
import android.app.actionbar.layoutparams; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.animation.animationutils; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.gallery; import android.widget.imageswitcher; import android.widget.imageview; import android.widget.viewswitcher.viewfactory; public class mainactivity extends activity { private int imageid[] = new int[] { r.drawable.a, r.drawable.b, r.drawable.c, r.drawable.d, r.drawable.e, r.drawable.f, r.drawable.g, r.drawable.h, r.drawable.i, r.drawable.j, r.drawable.k }; private imageswitcher imageswitcher; private gallery gallery; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imageswitcher = (imageswitcher) this.findviewbyid(r.id.imageswitcher); gallery = (gallery) this.findviewbyid(r.id.gallery1); // 设置动画效果 imageswitcher.setinanimation(animationutils.loadanimation(this, android.r.anim.fade_in)); imageswitcher.setoutanimation(animationutils.loadanimation(this, android.r.anim.fade_out)); imageswitcher.setfactory(new viewfactory() { @override public view makeview() { imageview imageview = new imageview(mainactivity.this); imageview.setscaletype(imageview.scaletype.fit_center); // 设置保持纵横比居中 imageview.setlayoutparams(new imageswitcher.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content)); return imageview; } }); galleryadapter adapter = new galleryadapter(mainactivity.this,imageid); gallery.setadapter(adapter); gallery.setselection(imageid.length / 2); gallery.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view,int position, long id) { imageswitcher.setimageresource(imageid[position]); } @override public void onnothingselected(adapterview<?> arg0) { } }); } }
其中需要的一个适配器:
import android.content.context; import android.content.res.typedarray; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gallery; import android.widget.imageview; public class galleryadapter extends baseadapter { private int[] imageid; private context mcontext; /** * 穿入上下文和图片资源数组 * @param mcontext * @param imageid */ public galleryadapter(context mcontext, int[] imageid) { this.mcontext = mcontext; this.imageid = imageid; } @override public int getcount() { return imageid.length; } @override public object getitem(int position) { return imageid[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview1; if (convertview == null) { imageview1 = new imageview(mcontext); imageview1.setscaletype(imageview.scaletype.fit_xy); imageview1.setlayoutparams(new gallery.layoutparams(180, 135)); typedarray typedarray = mcontext .obtainstyledattributes(r.styleable.gallery); imageview1.setbackgroundresource(typedarray.getresourceid( r.styleable.gallery_android_galleryitembackground, 0)); imageview1.setpadding(5, 0, 5, 0); // 设置imageview的内边距 } else { imageview1 = (imageview) convertview; } imageview1.setimageresource(imageid[position]); // 为imageview设置要显示的图片 return imageview1; // 返回imageview } }
到此 ok!
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
推荐阅读
-
Android中ViewFlipper的使用及设置动画效果实例详解
-
Android学习之Intent中显示意图和隐式意图的用法实例分析
-
Android MotionEvent中getX()和getRawX()的区别实例详解
-
Android 中Lambda表达式的使用实例详解
-
python中的hashlib和base64加密模块使用实例
-
Python中的startswith和endswith函数使用实例
-
怎样删除android的gallery中的图片实例说明
-
Android 中HttpURLConnection与HttpClient使用的简单实例
-
Android中AnimationDrawable使用的简单实例
-
Android学习之Intent中显示意图和隐式意图的用法实例分析