Gallery displays an image in a scroll.

Source: Internet
Author: User

Such as AD control requirements

  1. Display an image at a time

  2. Only one slide can be made at a time.

  3. Automatic playback

  • Custom ad controls inherit from gallery

/**

* The image gallery can only slide one image at a time.

* @ Author CMM

* @ Date 2012-02-02

* @ Version 1.0

*/

Public class imagegallery extends gallery {

Public imagegallery (context, attributeset paramattributeset ){

Super (context, paramattributeset );

}

@ Override

Public Boolean onfling (motionevent parammotionevent1,

Motionevent parammotionevent2, float paramfloat1, float paramfloat2 ){

Return false;

}

@ Override

Public Boolean onscroll (motionevent parammotionevent1, motionevent parammotionevent2, float paramfloat1, float paramfloat2 ){

Float F = 1.4f * paramfloat1;

Return super. onscroll (parammotionevent1, parammotionevent2, F, paramfloat2 );

} Http://www.eoeandroid.com/thread-102526-1-1.html

@ Override

Protected Boolean getchildstatictransformation (view child, transformation T ){

Return true;

}

}

  • Adapter item layout width fillparent

<? XML version = "1.0" encoding = "UTF-8"?>

<! -- Advertisement item -->

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content">

<Imageview

Android: Id = "@ + ID/qianghaoqi_ad_item_image"

Android: layout_width = "fill_parent"

Android: layout_height = "30020.dip"

Android: paddingbottom = "2020.dip"

Android: paddingleft = "2020.dip"

Android: paddingright = "2020.dip"

Android: paddingtop = "28w.dip"

Android: scaletype = "fitxy"/>

</Relativelayout>

  • Automatic playback

/**

* Auto-scrolling playback is mainly implemented by combining timer and handler. Timer is responsible for generating and transmitting scheduled update messages,

* The handler receives the processed message and resets the currently displayed image.

*/

Final handler mautogalleryhandler = new handler (){

Public void handlemessage (message ){

Super. handlemessage (Message );

Switch (message. What ){

Case handlertypeutils. handler_type_load_data_main_gallery_pos:

Mimagegallery. setselection (message. getdata (). getint (

Constantutil. gallery_pos ));

Break;

}

}

};

Timer mautotimer;


/**

* Timer is responsible for regularly updating the generation and transmission of messages. After adelay seconds, information is sent every aperiod seconds.

*

* @ Param adelay

* @ Param aperiod

*/

Protected void settimer (long adelay, long aperiod, final int APOs ){

If (mautotimer! = NULL ){

Mautotimer. Cancel ();

}

Mautotimer = new timer ();

Mautotimer. Schedule (New timertask (){

Int gallerypisition = apos;


@ Override

Public void run (){

If (gallerypisition <mcount-1 ){

Gallerypisition = gallerypisition + 1;

} Else {

Gallerypisition = 0;

}


Message MSG = new message ();

Bundle date = new bundle (); // store data

Date. putint (constantutil. gallery_pos, gallerypisition );

MSG. setdata (date );

MSG. What = handlertypeutils. handler_type_load_data_main_gallery_pos; // Message ID

Mautogalleryhandler. sendmessage (MSG );

}

}, Adelay, aperiod); // after 5 seconds, the message is sent every 5 seconds.

}

  • Layout file gallery medicines are included in framelayout to prevent Jitter

<! -- Homepage advertisement -->

<Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"

Xmlns: MySpace = "http://schemas.android.com/apk/res/com.blade.qianghaoqi"

Android: Id = "@ + ID/qianghaoqi_ad_layout"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: Orientation = "vertical">

<Com. Blade. qianghaoqi. UI. uicommon. baseview. imagegallery

Android: Id = "@ + ID/qianghaoqi_ad_gallery"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: animationduration = "100"

Android: fadingedge = "NONE"

Android: spacing = "0.0dip"/>

<! -- Add a flag -->

<Linearlayout

Android: Id = "@ + ID/qianghaoqi_ad_position"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_gravity = "bottom | Center"

Android: Orientation = "horizontal"

Android: paddingbottom = "5.0dip">

</Linearlayout>

</Framelayout>

  • Load flag points

/**

* Load a flag point

*/

Protected void loadpositionimage (){

Imageview aimageview = NULL;

Mlinearlayout. removeallviews ();

For (INT I = 0; I <mcount; I ++ ){

Aimageview = new imageview (this );

If (I = 0 ){

Aimageview

. Setimageresource (R. drawable. game_blade_qianghaoqi_alert_dialog_help_selected );

} Else {

Aimageview

. Setimageresource (R. drawable. game_blade_qianghaoqi_alert_dialog_help_unselected );

}

Mlinearlayout. addview (aimageview );

}

}

/**

* Change the flag point status

*

* @ Param APOs

*/

Public void changepositionimage (INT APOs ){

Imageview aimageview = (imageview) mlinearlayout. getchildat (APOs );

If (aimageview! = NULL ){

Aimageview

. Setimageresource (R. drawable. game_blade_qianghaoqi_alert_dialog_help_selected );

}

For (INT I = 0; I <mcount; I ++ ){

If (I! = APOs ){

Aimageview = (imageview) mlinearlayout. getchildat (I );

Aimageview

. Setimageresource (R. drawable. game_blade_qianghaoqi_alert_dialog_help_unselected );

}

}

}

@ Override

Public void onitemselected (adapterview <?> Parent, view, int position,

Long ID ){

Changepositionimage (position );

Setactionaddata (position );

}

 

 

 

From: http://mmandroid.diandian.com/post/2012-02-27/16773046

 

Niu Ren blog Space

 

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.