Gallery Extension 2 -- animation is based on Java code -- click to zoom in and out

Source: Internet
Author: User

 

 

I. Activity Code

Package com. liudan. activity;

 

Import java. util. arraylist;
Import java. util. List;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. animation. animation;
Import Android. View. animation. animationset;
Import Android. View. animation. scaleanimation;
Import Android. widget. adapterview;
Import Android. widget. adapterview. onitemselectedlistener;
Import Android. widget. Gallery;

Import com. liudan. Adapter. galleryadapter;

Public class galleryactivity extends activity {
 
Private gallery Gallery;
Private list <integer> gallerylist;
Private animationset manimationset;


@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Initdata ();
Gallery = (Gallery) findviewbyid (R. Id. Gallery );
Galleryadapter = new galleryadapter (this, gallerylist );
Gallery. setadapter (galleryadapter );
// Set the image displayed in the middle of the screen. By default, the first image is displayed in the middle of the screen, and the left side of the screen is blank.
Gallery. setselection (gallerylist. Size ()/2 );
Gallery. setonitemselectedlistener (New galleryonitemselectedlistener ());

}
Private void initdata (){
Gallerylist = new arraylist <integer> ();
For (INT I = 0; I <8; I ++ ){
Gallerylist. Add (R. drawable. Icon );
}

}
 
Private class galleryonitemselectedlistener implements onitemselectedlistener {

@ Override
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){

Animationset = new animationset (true );
If (manimationset! = NULL & manimationset! = Animationset ){

Scaleanimation = new scaleanimation (2, 0.5f, 2, 0.5f,
Animation. relative_to_self, 0.5f, // which point is scaled to itself relative to itself.
Animation. relative_to_self, 0.5f );
Scaleanimation. setduration (1000 );
Manimationset. addanimation (scaleanimation );
Manimationset. setfillafter (true); // keep it in the animation end state.

View. startanimation (manimationset );
}

Scaleanimation = new scaleanimation (1, 2f, 1, 2f,
Animation. relative_to_self, 0.5f, // which point is scaled to itself relative to itself.
Animation. relative_to_self, 0.5f );
Scaleanimation. setduration (1000 );
Animationset. addanimation (scaleanimation );
Animationset. setfillafter (true); // keep it in the animation end state.

View. startanimation (animationset );
Manimationset = animationset;
}

@ Override
Public void onnothingselected (adapterview <?> Parent ){
// Todo auto-generated method stub

}

}
 
 
}

 

Ii. Adapter code

Package com. liudan. Adapter;

Import java. util. List;

Import Android. content. context;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. imageview;

Public class galleryadapter extends baseadapter {
Private list <integer> gallerylist;
Private context;

Public galleryadapter (context, list <integer> gallerylist ){
This. gallerylist = gallerylist;
This. Context = context;
}

@ Override
Public int getcount (){
// Todo auto-generated method stub
Return gallerylist. Size ();
}

@ Override
Public object getitem (INT position ){
// Todo auto-generated method stub
Return gallerylist. Get (position );
}

@ Override
Public long getitemid (INT position ){
// Todo auto-generated method stub
Return position;
}

@ Override
Public View getview (INT position, view convertview, viewgroup parent ){
View view = NULL;
If (convertview = NULL ){
Imageview = new imageview (context );
Imageview. setimageresource (gallerylist. Get (position ));
View = imageview;
} Else {
View = convertview;
}
// Todo auto-generated method stub
Return view;
}

}

 

Iii. XML Code

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Gallery Android: Id = "@ + ID/Gallery"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: layout_gravity = "center_horizontal"
Android: layout_centerhorizontal = "true"
Android: spacing = "10dip"/>
</Linearlayout>

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.