Why have you been idle for the past few days? When you are bored, you may find that many of your friends have asked questions about image display on the Internet. I just made a demo here, in this demo, I use a dialog box to display images. The main function is to display images that share the same feeling in the dialog box after clicking an image button, you can use the imageswitcher component to expand and select display alternately. Two buttons are used in the dialog box, after you select an image and click OK, bind the image to the previous button component and display it. In fact, this is just an idea, so that you can bind the image to any other component, in this way, the interactive capability of controls between components is enhanced by dynamic vision, which improves the user experience. A custom toast is also provided to achieve the Display Effect of selecting images and binding them to toast, hope to provide the best learning reference for friends who need it. Well, it's unfortunate recently. Now I have to solve the problem of my work. I really didn't expect it to happen so much. Have a good life, I will post the Code. If you want to view the instance, please download the source code in my resources:
Main XML
Imageswitch. xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Imagebutton
Android: Id = "@ + ID/Show"
Android: src = "@ drawable/Item1"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
<Gallery
Android: Id = "@ + ID/img_gallery"
Android: layout_width = "fill_parent"
Android: layout_height = "110px"
Android: layout_margintop = "10px"
Android: layout_alignparentleft = "true"
Android: spacing = "20px"
> </Gallery>
<Imageswitcher
Android: Id = "@ + ID/image_switcher"
Android: layout_width = "90px"
Android: layout_height = "90px"
Android: layout_alignparenttop = "true"
Android: layout_centerhorizontal = "true"
Android: layout_alignbottom = "@ + ID/img_gallery"
>
</Imageswitcher>
</Relativelayout>
Custom toast
Customertoast. Java
Package com. JSD;
Import Android. content. context;
Import Android. Graphics. color;
Import Android. View. gravity;
Import Android. widget. imageview;
Import Android. widget. linearlayout;
Import Android. widget. textview;
Import Android. widget. Toast;
/**
* Custom toast to help display the prompt
* @ Author jiangshide
*
*/
Public class customertoast {
/**
* Define a static method that can be called directly for other classes.
* @ Param Context
* @ Param imageresid
* @ Param content
* @ Param duration
*/
Public static void mytoastshow (context, int imagesid, int imagebg, string content, int duration ){
// You still need to initialize toast for the operation.
Toast customer = new toast (context );
Customer. setgravity (gravity. Center, 50,100 );
Linearlayout toastlayout = new linearlayout (context );
Toastlayout. setorientation (linearlayout. Horizontal );
Toastlayout. setgravity (gravity. center_vertical );
Imageview = new imageview (context );
Imageview. setimageresource (imagesid );
Toastlayout. addview (imageview );
Textview TV _content = new textview (context );
TV _content.settext (content );
TV _content.settextcolor (color. Black );
TV _content.setbackgroundresource (imagebg );
Toastlayout. addview (TV _content );
Customer. setview (toastlayout );
Customer. Show ();
}
}
Main Java classes
Jsdimages. Java:
Package com. JSD;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. context;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. widget. adapterview;
Import Android. widget. baseadapter;
Import Android. widget. Gallery;
Import Android. widget. imagebutton;
Import Android. widget. imageswitcher;
Import Android. widget. imageview;
Import Android. widget. adapterview. onitemselectedlistener;
Import Android. widget. viewswitcher. viewfactory;
/**
* Demo with minor requirements
* Mainly implements image loop browsing
* Select an image to bind it to a required component to change the image display.
* Custom toast to display
* @ Author jiangshide
*
*/
Public class jsdimages extends activity {
Private Static final string tag = "jsdimages ";
Imagebutton show;
/** Click Event on the icon */
Imagebutton btn_img;
/** Click the dialog box that appears after the event */
Alertdialog imagechoosedialog;
/** Set the image */
Private int [] images = {
R. drawable. Icon, // 0 by default
R. drawable. icon
, R. drawable. image5
, R. drawable. Item1
, R. drawable. item2
, R. drawable. item3
, R. drawable. item4,
R. drawable. item5 };
/** Set gallery as a member variable */
Gallery;
/** Set imageswitcher to a member variable */
Imageswitcher is;
/** Define a member variable to save the image position */
Int imageposition; // The default value of this member variable is 0 if no value is assigned.
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen );
Setcontentview (R. layout. imageswitch );
Getcomponents (); // initialize the Image Display Effect
}
/**
* Assemble Components
*/
Public void getcomponents (){
Show = (imagebutton) This. findviewbyid (R. Id. Show );
Show. setonclicklistener (New imagebutton. onclicklistener (){
Public void onclick (view v ){
// Use the custom toast to display the prompt
Customertoast. mytoastshow (jsdimages. This, R. drawable. Icon, R. drawable. Item1, "display images for custom toast...", 1 );
Initimagechoosedialog (); // The initialization displays the image in a dialog box.
Imagechoosedialog. Show (); // call the display method in the dialog box.
}
});
}
/**
* Create a dialog box to display the information.
*/
Private void initimagechoosedialog (){
Alertdialog. Builder = new alertdialog. Builder (this );
Builder. settitle ("select image ");
// Set the dialog box button
Builder. setpositivebutton ("OK", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
Show. setimageresource (images [imageposition % images. Length]);
Customertoast. mytoastshow (jsdimages. this, images [imageposition % images. length], images [imageposition % images. length], "changed toast Image Location:" + imageposition % images. length, 1 );
}
});
// Set the cancel button
Builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){
}
});
Layoutinflater Inflater = layoutinflater. From (this );
View view = Inflater. Inflate (R. layout. imageswitch, null );
Gallery = (Gallery) view. findviewbyid (R. Id. img_gallery );
Gallery. setadapter (New imageadapter (this ));
Gallery. setselection (images. Length/2); // the default position.
Is = (imageswitcher) view. findviewbyid (R. Id. image_switcher );
Gallery. setonitemselectedlistener (New onitemselectedlistener (){
Public void onitemselected (adapterview <?> Parent, view,
Int position, long ID ){
Imageposition = position;
Is. setimageresource (images [position % images. Length]); // solves the data subscript out of bounds to implement gallery loop display
}
Public void onnothingselected (adapterview <?> Parent ){
}
});
Is. setfactory (New myviewfactory (this ));
Builder. setview (View );
Imagechoosedialog = builder. Create ();
}
/**
*
* @ Author jiangshide
*
*/
Class imageadapter extends baseadapter {
Private context mcontext;
Public imageadapter (context c ){
This. mcontext = C;
}
/**
* Obtain the number in a loop to create a view and display it.
*/
Public int getcount (){
// Return images. length;
Return integer. max_value; // use a small trick to implement a gallery wireless Loop
}
Public object getitem (INT position ){
Return NULL;
}
Public long getitemid (INT position ){
Return 0;
}
/**
* Getview () needs to call getcount () to obtain the number of displayed items.
*/
Public View getview (INT position, view convertview, viewgroup parent ){
Imageview IV = new imageview (mcontext );
// IV. setimageresource (images [position]); // The subscript may be out of bounds: Solution
Iv. setimageresource (images [position % images. Length]); // The subscript may be out of bounds.
Iv. setadjustviewbounds (true );
Iv. setlayoutparams (new gallery. layoutparams (90,90 ));
Iv. setpadding (15, 10, 15, 10 );
Return IV;
}
}
/**
* Define the display size
* @ Author jiangshide
*
*/
Class myviewfactory implements viewfactory {
Private context mcontext;
Public myviewfactory (context c ){
This. mcontext = C;
}
Public View makeview (){
Imageview IV = new imageview (mcontext );
Iv. setlayoutparams (New imageswitcher. layoutparams (90,90 ));
Return IV;
}
}
}