Gallery and imageswitcher for Android Control

Source: Internet
Author: User

Imageswitcher is a widget in Android that controls the image display effect, such as the slide effect. Album Creation

I. Important Methods

Setimageuri: sets the image address.

Setimageresource (int
Resid): sets the image repository.

Setimagedrawable (drawable): draws an image.

Ii. Instances

 

Set animation effect

Imageswitcher. setinanimation (animationutils. loadanimation (this,
Android. R. anim. fade_in ));
Imageswitcher. setoutanimation (animationutils. loadanimation (this,
Android. R. anim. fade_out ));

  

Iii. complete code

1. layout file imageswitch. XML (display the layout of the pop-up dialog box content. One gallery and one imageswitcher)

<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:id="@+id/widget34"android:layout_width="fill_parent" android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><Gallery android:id="@+id/img_gallery" android:layout_width="fill_parent"android:layout_height="110px" android:layout_marginTop="10px"android:layout_alignParentLeft="true"/><ImageSwitcher android:id="@+id/imageswitch"android:layout_width="90px" android:layout_height="90px"android:layout_alignParentTop="true" android:layout_centerHorizontal="true"android:layout_alignBottom="@+id/gallery"/></RelativeLayout>

2. Complete class adduseractivity. java. When you click the corresponding button, the Christmas box is displayed.

Public class adduseractivity extends activity {/** called when the activity is first created. */imagebutton imbutton; imageswitcher; gallery Gallery; int imageposition;/*** all image images */private int [] images = new int [] {R. drawable. icon, R. drawable. image1, R. drawable. image2, R. drawable. image3, R. drawable. image4, R. drawable. image5, R. drawable. image6, R. drawable. image7, R. drawable. image8, R. drawable. im Age9, R. drawable. image10, R. drawable. image11, R. drawable. image12, R. drawable. image13, R. drawable. image14, R. drawable. image15, R. drawable. image16, R. drawable. image17, R. drawable. image18, R. drawable. image19, R. drawable. image20, R. drawable. image21, R. drawable. image22, R. drawable. image23, R. drawable. image24, R. drawable. image25, R. drawable. image26, R. drawable. image27, R. drawable. image28, R. drawable. image29, R. drawable. imag E30}; alertdialog imagechoosedialog; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. add_new); imbutton = (imagebutton) findviewbyid (R. id. image_button); imbutton. setonclicklistener (New imagebuttonlistener ();} class imagebuttonlistener implements onclicklistener {final charsequence [] items = {"red", "green", "blue"}; @ overridepublic v Oid onclick (view v) {initimagechoosedialog (); imagechoosedialog. show () ;}// initialization dialog box; private void initimagechoosedialog () {alertdialog. builder = new Builder (this); builder. settitle ("select image "). setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {imbutton. setimageresource (images [imageposition]) ;}}). setnegativebutton ("cancel", new Dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {// todo auto-generated method stub}); layoutinflater Inflater = layoutinflater. from (this); view = Inflater. inflate (R. layout. imageswitch, null); gallery = (Gallery) view. findviewbyid (R. id. img_gallery); Gallery. setadapter (New imageadapter (this); // sets the gallery data. Gallery. setselection (images. length/2); imageswitcher = (imageswitcher) view. findviewbyid (R. id. imageswitch); imageswitcher. setfactory (New imageviewfactory (this); Gallery. setonitemselectedlistener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Parent, view, int position, long ID) {// todo auto-generated method stubimageposition = position; imageswitcher. setimageresource (images [position]) ;}@ overridepublic void onnothingselected (adapterview <?> Parent) {// todo auto-generated method stub}); builder. setview (View); imagechoosedialog = builder. create ();}/*** This adapter class represents data, adapter, and view, and adapter represents the bond between data and view * this class is run for each column of data displayed. */class imageadapter extends baseadapter {private context; Public imageadapter (context) {This. context = context;}/*** displays the number of data columns. Here is the number of available images. Indicates the number of times the image is drawn. It is run first when the class is created. * // @ Overridepublic int getcount () {// todo auto-generated method stubsystem. out. println ("baseadapter ----- getcount:" + images. length); Return images. length ;}@ overridepublic object getitem (INT position) {// todo auto-generated method stubsystem. out. println ("baseadapter ----- getitem:" + position); return NULL ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubsystem. out. println ("Baseadapter ----- getitemid:" + position); Return 0;}/*** return your own definition attempt. You can also define it in an XML file, (the data format in Gallery is defined here .) * Position indicates the position of the selected column. * Each time a column of data is displayed, it is executed once */@ overridepublic view getview (INT position, view convertview, viewgroup parent) {system. out. println ("baseadapter ----- position:" + position); imageview IV = new imageview (context); IV. setimageresource (images [position % images. length]); IV. setadjustviewbounds (true); IV. setlayoutparams (new gallery. layoutparams (80, 80); IV. setpadding (15, 10, 15, 10); Return IV ;}} class imageviewfactory implement S viewfactory {context; Public imageviewfactory (context) {This. context = context ;}@ overridepublic view makeview () {imageview IV = new imageview (context); // you can specify the display size. Layout parameters. Iv. setlayoutparams (New imageswitcher. layoutparams (90, 90); Return IV ;}}}

When the image is selected for the first operation, the log output is as follows:

08-18 11:44:57.522: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.522: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.522: INFO/System.out(400): BaseAdapter-----getItemId:008-18 11:44:57.522: INFO/System.out(400): BaseAdapter-----getItemId:008-18 11:44:57.522: INFO/System.out(400): BaseAdapter-----getItemId:1508-18 11:44:57.572: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.572: INFO/System.out(400): BaseAdapter-----position:1508-18 11:44:57.583: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.583: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.583: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.613: INFO/System.out(400): BaseAdapter-----getItemId:1508-18 11:44:57.613: INFO/System.out(400): BaseAdapter-----position:1608-18 11:44:57.622: INFO/System.out(400): BaseAdapter-----position:1708-18 11:44:57.635: INFO/System.out(400): BaseAdapter-----position:1808-18 11:44:57.635: INFO/System.out(400): BaseAdapter-----position:1408-18 11:44:57.643: INFO/System.out(400): BaseAdapter-----position:1308-18 11:44:57.643: INFO/System.out(400): BaseAdapter-----position:1208-18 11:44:57.662: INFO/System.out(400): BaseAdapter-----getItemId:1508-18 11:44:57.662: INFO/System.out(400): BaseAdapter-----getItemId:1508-18 11:44:57.673: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.673: INFO/System.out(400): BaseAdapter-----position:1508-18 11:44:57.682: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.682: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.693: INFO/System.out(400): BaseAdapter-----getCount:3108-18 11:44:57.693: INFO/System.out(400): BaseAdapter-----getItemId:1508-18 11:44:57.693: INFO/System.out(400): BaseAdapter-----getItemId:15

Running result


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.