Android Siege Lion Gallery and imageswitcher make picture browser

Source: Internet
Author: User

  using gallery and Imageswitcher Create a Picture browser gallery we sometimes see dynamic images on a phone or PC, move it with a mouse or finger touch, create a dynamic picture scrolling effect, and trigger other event responses based on your click or touch. Similarly, this implementation is also available on Android, which is implemented by gallery the thumbnail browser on the UI. -------------------------Google has listed gallery as an outdated control, it is recommended to use Horizonscrollview or Viewpager. -------------------------to see the source code is not difficult to find Arrayadapter and Simpleadapter have inherited baseadapter. So, we can also define an adapter by ourselves! The benefits of customization are greater freedom and flexibility. Important methods in Baseadapter (1) public int getcount ()--Returns the total number of data sources defined (2) public Object GetItem (int. position) public long getitemid (i NT position)--tells the adapter to get the data ID and object in the current container (3) public View getView (int position,view convertview,viewgroupparent)-- Gets the image view that is currently being displayed, passing in the array ID value so that it is read and imaged. -----------------------------------the steps to use Gallery to browse Pictures: 1. Add the gallery label to the Main.xml. 2. Create a data source in Mainactivity, where the int array holds the image ID. 3. Customize the adapter. Creates a new class Imageadapter, inheriting Baseadapter. Override GetView (int position, view view, ViewGroup ViewGroup) method. 4. Load the adapter in mainactivity.  

  Step 1 to customize the adapter. Create a new class Iamgeadapter, inherit Baseadapter. 2. In order to receive the parameters, we need to construct the method of constructing the parameter: private int[]res; Used to receive the data source private context context;//contextual information, the initialization of the ImageView will be used//constructs a method public imageadapter (Int[]res,context Contex        T) {this.res=res;    This.context=context; }3. Rewrite GetView (), convert the image ID (here is res[position]) to ImageView, and return to Imageview:public view GetView (int position, view view, ViewGroup    ViewGroup) {ImageView image = new ImageView (context);    Image.setbackgroundresource (Res[position]); Sets the size of the thumbnail image (ImageView) in Gallery image.setlayoutparams (new Gallery.layoutparams (400,300));//width is 400, height is 300// Set the scaling (scale) to take the type Image.setscaletype (ImageView.ScaleType.FIT_XY);//the transverse x-y axis of the stretch return image;} Other methods of rewriting, fixed: public int getcount () {return res.length;} Public Object getItem (int position) {return res[position];} public long getitemid (int position) {return position;}  




Unlimited Carousel Image implementation: 1. Overwrite the Oncount () method of the Imageadapter adapter so that it returns the integer maximum value integer.max_value. 2. Overwrite GetView (): Image.setbackgroundresource (Res[position%res.length]); Because the upper limit of the array res subscript is integer.max_value, so position The result of the%res.length operation is an array of loops. This enables the loop to play the picture. ---------------------------------------------Imageswitcher to achieve a larger view of the steps: 1. Add the Imageswitcher label to the Main.xml. 2. Let mainactivity inherit the interface viewfactory. Override Method Makeview (): Public    View Makeview () {        ImageView image = new ImageView (this);//Initialize ImageView object        Image.setscaletype (ImageView.ScaleType.FIT_CENTER);//maintain a high ratio and center display        return image;    }    Rewrite onitemselected (), write only one sentence: Imageswitcher.setbackgroundresource (res[position%res.length]);//imageswitch load the picture resource. 3. Create and initialize the Imageswitcher object in Mainactivity. After loading factory: Imageswitcher.setfactory (this);   Then it is the loading animation effect, for example: Imageswitcher.setinanimation (Animationutils.loadanimation (this,android. r.anim.fade_in));//Fade in effect
Imageswitcher Large Map Browse Imageswitcher and ImageView functions are a bit similar, they all can be used to display pictures, the difference is that the effect of imageswitcher is more dazzling, it can specify the picture when the animation effect of the switch. In order for Imageswitcher to load the image, the mainactivity must be implemented viewfactory the interface. ---------------------------------------viewfactory Use Imageswitcher rough understanding is ImageView selector, it needs to set viewfactory. In general, we let the Viewfactory Makeview () method return ImageView. -----------------------------------Add animation imageswitcher.setinanimation to Imageswitcher (animationutils.loadanimation (This,android. r.anim.fade_in));//Fade in effect Imageswitcher.setoutanimation (Animationutils.loadanimation (this,android. r.anim.fade_out));//fade-out effect
























Android Siege Lion Gallery and imageswitcher make picture browser

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.