Android uses Horizontalscrollview for gallery effects

Source: Internet
Author: User

  Gallery (gallery) is a lock center entry and has a horizontal scrolling list of views, generally used to browse the picture, and can respond to event display information,Gallery can also be used in conjunction with the Imageswitcher component to achieve a thumbnail view of the effect of the picture;

But gallery was discarded by Google, Google recommended the use of Viewpager and Horizontalscrollview to achieve gallery effect;

See an example: Using Gallery to achieve:

Code:

Scrollview1.java

 PackageCom.xiaozhang.dialog;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.Gallery;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classScrollView1extendsActivity {PrivateGallery Gallery; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.view); Gallery=(Gallery) Findviewbyid (r.id.gallery); //setting up a picture adapterGallery.setadapter (NewImageadapter ( This)); //Setting up ListenersGallery.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?>Parent, View V,intPositionLongID) {toast.maketext (ScrollView1. This,                        "Clicked" + (position + 1) + "picture", Toast.length_long). Show ();    }        }); }}classImageadapterextendsBaseadapter {//declaring context    Privatecontext Context; //Image source Array    Privateinteger[] Imageinteger ={r.drawable.pic1, R.drawable.pic2, R.DRAWABLE.PIC3, R.drawable.pic4, R.drawable.pic5, R.DRAWABLE.PIC6,    R.DRAWABLE.PIC7}; //Statement Imageadapter     PublicImageadapter (context C) {context=C; } @Override//get the number of pictures     Public intGetCount () {returnimageinteger.length; } @Override//get the position of the picture in the library     PublicObject GetItem (intposition) {        returnposition; } @Override//get the position of the picture in the library     Public LongGetitemid (intposition) {        returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {ImageView ImageView=NewImageView (context); //setting up resources for ImageViewImageview.setimageresource (imageinteger[position]); //Set Scale typeImageview.setscaletype (ImageView.ScaleType.CENTER); Imageview.setlayoutparams (NewGallery.layoutparams (350, 350)); returnImageView; }}

View.xml

<?XML version= "1.0" encoding= "Utf-8"?><Galleryxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/gallery"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "? Android:galleryitembackground"android:spacing= "10DP"    />

Use Horizontalscrollview to achieve:

Mainactivity.java

 Packagecom.xiaozhang.horizontal;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.widget.ImageView;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateLinearLayout MGallery; Private int[] imgid; PrivateLayoutinflater Minflater; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_main); Minflater= Layoutinflater.from ( This); Imgid=New int[] {r.drawable.pic1, r.drawable.pic2, R.DRAWABLE.PIC3, R.drawable.pic4, R.DRAWABLE.PIC5, R.drawable.pi        C6, R.drawable.pic7, R.drawable.pic8, r.drawable.pic9};    Initview (); }    Private voidInitview () {mgallery=(LinearLayout) Findviewbyid (r.id.gallery_id);  for(inti = 0; i < imgid.length; i++) {View View= Minflater.inflate (R.layout.gallery, MGallery,false); ImageView img=(ImageView) View.findviewbyid (r.id.gallery_image);            Img.setimageresource (Imgid[i]); TextView txt=(TextView) View.findviewbyid (R.id.gallery_text); Txt.settext ("Some info");        Mgallery.addview (view); }    }}

Activity_main.xml

<LinearLayoutxmlns: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" >    <HorizontalscrollviewAndroid:layout_width= "Wrap_content"Android:layout_height= "260DP"android:layout_gravity= "Center_vertical"Android:background= "#AA4444"Android:scrollbars= "None" >        <LinearLayoutAndroid:id= "@+id/gallery_id"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"android:orientation= "Horizontal" >        </LinearLayout>    </Horizontalscrollview></LinearLayout>

Gallery.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "240DP"Android:layout_height= "240DP"Android:background= "@android: Color/white" >    <ImageViewAndroid:id= "@+id/gallery_image"Android:layout_width= "180DP"Android:layout_height= "180DP"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"Android:layout_margin= "5DP"Android:scaletype= "Centercrop" />    <TextViewAndroid:id= "@+id/gallery_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/gallery_image"Android:layout_centerhorizontal= "true"Android:layout_marginbottom= "5DP"Android:layout_margintop= "5DP"Android:textcolor= "#ff0000"android:textsize= "25SP" /></Relativelayout>

Android uses Horizontalscrollview for gallery effects

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.