Android programming sliding effect gallery+gridview to achieve the image Preview function (with demo source download) _android

Source: Internet
Author: User

This article illustrates the Gallery+gridview of the Android programming sliding effect to realize the image preview function. Share to everyone for your reference, specific as follows:

The Android system has a GridView and gallery two controls, the GridView grid display, gallery single browsing, the combination of the two can really achieve gallery browsing image effects.

This example simulates the image browsing effect of a complete gallery image set through the GridView and gallery two controls. The effect chart is as follows:

1. The GridView

First, customize a Gridimageadapter picture adapter to populate a picture of the GridView control

public class Gridimageadapter extends Baseadapter {private context mcontext;
  Drawable btndrawable;
    Public Gridimageadapter {mcontext = context;
    Resources of resources = context.getresources ();
  btndrawable = resources.getdrawable (r.drawable.bg);
  @Override public int GetCount () {return ImageSource.mThumbIds.length;
  @Override public Object getitem (int position) {return position;
  @Override public long getitemid (int position) {return position;
    @Override public View getview (int position, View Convertview, ViewGroup parent) {Imageviewext ImageView;
    int space;
      if (Convertview = = null) {ImageView = new Imageviewext (mcontext);
        if (Imagecol = = 5) {space = dm.heightpixels/imagecol-6;
      Imageview.setlayoutparams (New Gridview.layoutparams (space, spaces));
        else {space = dm.widthpixels/imagecol-6; Imageview.setlayoutparams (New Gridview.layoutparams ( Space, space));
      } imageview.setadjustviewbounds (True);  Imageview.setscaletype (ImageView.ScaleType.CENTER_CROP);
    Scale the picture to make it long and wide as imageview.setpadding (3, 3, 3, 3);
    else {ImageView = (imageviewext) Convertview;
    } imageview.setimageresource (Imagesource.mthumbids[position]);
  return ImageView;

 }
}

Then, fill the GridView with Gridimageadapter

GridView = (GridView) Findviewbyid (R.id.mygrid);
Gridimageadapter = new Gridimageadapter (this);
Gridview.setadapter (gridimageadapter);
Gridview.setonitemclicklistener (listener); Set up a click on a listener event

Finally, set the click-Listening event for the GridView control

Adapterview.onitemclicklistener listener = new Adapterview.onitemclicklistener () {
  @Override public
  Void Onitemclick (adapterview<?> arg0, View arg1, int position, long id) {
    Intent Intent = new Intent ();
    Intent.setclass (Gridviewactivity.this, galleryactivity.class);
    Intent.putextra ("position", position);
    StartActivity (intent);
  }
;

2, Gallery

When you have finished displaying the images of the GridView, listening to the events, and now clicking on the image, you will start an activity to display the currently clicked picture, and the control that displays the picture is gallery

First, like the GridView, a Imageadapter picture adapter is customized to populate the gallery

public class Imageadapter extends Baseadapter {private context mcontext;
  private int MPOs;
  Public Imageadapter {mcontext = context;
  The public void setownposition (int ownposition) {this.mpos = ownposition;
  public int getownposition () {return mpos;
  @Override public int GetCount () {return ImageSource.mThumbIds.length;
    @Override public Object getitem (int position) {mpos=position;
  return position;
    @Override public long getitemid (int position) {mpos=position;
  return position;
    @Override public View getview (int position, View Convertview, ViewGroup parent) {mpos=position;
    ImageView ImageView = new ImageView (mcontext);
    Imageview.setbackgroundcolor (0xff000000);
    Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER);
    Imageview.setlayoutparams (New Mygallery.layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); Imageview.setimageresource (Imagesource.mthumbiDs[position]);
  return ImageView;

 }
}

Then, fill the gallery with Imageadapter

Mygallery galllery = (mygallery) Findviewbyid (r.id.mygallery);
Intent Intent = Getintent ();
Position = Intent.getintextra ("position", 0); Get gridviewactivity the picture location position
imageadapter imgadapter=new imageadapter (this);
Galllery.setadapter (Imgadapter); Set Picture Imageadapter
galllery.setselection (position);//Set current display picture
  Animation an= animationutils.loadanimation (This,r.anim.scale); Gallery Animation
  Galllery.setanimation (an);

At this point, if careful to note that our gallery is also our own definition of mygallery, specifically defined as follows:

public class Mygallery extends Gallery {Boolean isfirst = false;
  Boolean islast = false;
  Public Mygallery {Super (context);
  Public Mygallery (context, AttributeSet Paramattributeset) {Super (context, paramattributeset); /** slide to the left (true-slide to the left; false-right)/private Boolean Isscrollingleft (Motionevent E1, motionevent E2) {Retur
  n E2.getx () > E1.getx (); @Override public boolean onfling (Motionevent E1, motionevent E2, float Distancex, float distancey) {imageadapte
    R ia = (imageadapter) this.getadapter (); int p = ia.getownposition (); Gets the position int count = Ia.getcount () of the current picture;
    Gets the total count int kevent for all pictures; if (Isscrollingleft (E1, E2)) {if (P = = 0 && isfirst) {Toast.maketext (This.getcontext (), "is first page", T Oast.
      Length_short). Show ();
      else if (p = = 0) {Isfirst = true;
      else {islast = false;
    } kevent = Keyevent.keycode_dpad_left; }else {if (p = = count-1 && islast) {Toast.maketext (This.getcontext (), "to last page", Toast.length_shor
      T). Show ();
      else if (p = = count-1) {Islast = true;
      else {Isfirst = false;
    } kevent = Keyevent.keycode_dpad_right;
    } onKeyDown (kevent, NULL);
  return true;

 }
}

Galleryactivity Layout File Gallery.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: gravity= "center"
  android:orientation= "horizontal"
  android:padding= "10dip" >
  <relativelayout
    android:layout_width= "wrap_content"
    android:layout_height= "wrap_content"
    android:background= "# 000000 "
    android:padding=" 2dip ">
    <com.homer.gridgallery.mygallery
      android:id=" @+id/ Mygallery "
      android:layout_width=" fill_parent "
      android:layout_height=" Fill_parent "
      android: spacing= "16DP"/>
  </RelativeLayout>
</LinearLayout>

Full instance code click here to download the site.

More interested readers of Android-related content can view this site: "Android Development Animation Tips", "Android Development introduction and Advanced Course" and "Android Control usage summary."

I hope this article will help you with the Android program.

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.