"Android Combat" Gallary+imageswicther picture Viewer

Source: Internet
Author: User
Tags getcolor

Follow the current major news website Picture news Browsing mode, shown above the specific picture (Imageswitch), below is a small picture can be slid (Gallery).

It is important to note that Imageswitch needs to define a component that is returned by a factory, and can animate the effect.

Gallery loads the layout with an adapter, its gallery can be set, and the distance between two pictures.

Layout:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:orientation=" vertical ">    <imageswitcher        android:id= "@+id/switcher"        android:layout_width= "fill_parent"        android:layout_height= "Fill_parent"        android:layout_gravity= "center"        android:layout_weight= "1" >    </ImageSwitcher>    < Gallery        android:id= "@+id/gallary"        android:layout_width= "fill_parent"        android:layout_height= " Fill_parent "        android:layout_margin=" 10DP "        android:layout_weight=" 3 "        android:spacing=" 20DP "/> </LinearLayout>

Java class:

Package Com.gallaryandswitch;import Android.app.activity;import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.view.view;import Android.view.viewgroup;import Android.view.viewgroup.layoutparams;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.widget.adapterview;import Android.widget.BaseAdapter;import Android.widget.framelayout;import Android.widget.gallery;import Android.widget.imageswitcher;import Android.widget.imageview;import Android.widget.imageview.scaletype;import Android.widget.linearlayout;import Android.widget.viewswitcher.viewfactory;import Com.example.wangyitest.r;public class MainAct extends Activity { Imageswitcher Imageswitcher; Gallery gallery;int[] Pics = {r.drawable.f1, r.drawable.f2, r.drawable.f3, R.drawable.f4,r.drawable.f5, R.drawable.f6, R.drawable.f7, r.drawable.flower}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (SavedinstAncestate); Setcontentview (r.layout.galleryswitch); imageswitcher = (Imageswitcher) Findviewbyid (R.id.switcher); Gallery = (gallery) Findviewbyid (r.id.gallary); Imageswitcher.setfactory (new Viewfactory () {@Overridepublic View Makeview () {//TODO auto-generated method Stubimageview ImageView = new ImageView (Getapplicationcontext ()); Imageview.setscaletype (Scaletype.fit_center); Imageview.setlayoutparams (New Framelayout.layoutparams ( framelayout.layoutparams.fill_parent,framelayout.layoutparams.fill_parent)); Imageview.setbackgroundcolor ( Getresources (). GetColor (R.COLOR.BG3)); return imageView;}); Imageswitcher.setinanimation (Animationutils.loadanimation (Getapplicationcontext (), Android. r.anim.fade_in)); Imageswitcher.setoutanimation (Animationutils.loadanimation (Getapplicationcontext (), Android. r.anim.fade_out)); Gallery.setadapter (new Myadapter ());//Gallery.setonitemclicklistener (new Adapterview.onitemclicklistener ()//{////@Override//public void Onitemclick (adapterview<?> parent, View view,//int position, long ID) {////TODO auto-generated method stub//Imageswitcher.setimageresource (pics[position]);//}// }); Gallery.setonitemselectedlistener (new Adapterview.onitemselectedlistener () {@Overridepublic void onitemselected (adapterview<?> parent, View view,int position, long ID) {//TODO auto-generated method Stubimageswitcher.setimageresource (Pics[position]);} @Overridepublic void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}}); Class Myadapter extends Baseadapter {@Overridepublic int getcount () {//TODO auto-generated method Stubreturn pics.length; } @Overridepublic Object GetItem (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubimagevie W ImageView = new ImageView (Getapplicationcontext ()); imageview.setscalEType (Scaletype.fit_center); Imageview.setlayoutparams (New Gallery.layoutparams (400,GALLERY.LAYOUTPARAMS.WRAP_ CONTENT)); Imageview.setimagebitmap (Bitmapfactory.decoderesource (Getresources (), pics[position])); Imageview.setbackgroundcolor (Getresources (). GetColor (R.COLOR.BG2)); return ImageView;}}}


"Android Combat" Gallary+imageswicther picture Viewer

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.