Imageswitcher allows you to display an image. (The image is animated when it is changed)

Source: Internet
Author: User

Both imageview and imageswitcher are used to display images. However, the latter can have an animated effect when updating the displayed image.

Note:

1. to initialize imageswitcher, You need to define a viewfactory. The object generated by this viewfactory must be imageview.

Iswitcher. setfactory (New viewfactory () {@ override public view makeview () {// create an imageview object imageview = new imageview (mainactivity. this); // set the image stretching method imageview. setscaletype (imageview. scaletype. fit_start); // sets the image Filling Method imageview. setlayoutparams (New imageswitcher. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); Return imageview ;}});

 

2. There is the same method as imageview when setting an image. Generally, setimageresource (INT resid) and setimagedrawable (drawable) are used)

Layout File

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical"><Imageswitcher Android: Id = "@ + ID/imageswitcher_id" Android: layout_width = "match_parent" Android: layout_height = "150dp" Android: layout_margin = "16dp" Android: layout_gravity = "center_horizontal" Android: inanimation = "@ Android: anim/slide_in_left" Android: outanimation = "@ Android: anim/slide_out_right"/><Button Android: Id = "@ + ID/prev_button_id" Android: layout_gravity = "center_horizontal" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "previous image"/> <button Android: Id = "@ + ID/next_button_id" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_horizontal" Android: text = "Next image"/> </linearlayout>

Mainactivity. Java

Package COM. example. imageswitcher; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. gallery. layoutparams; import android. widget. imageswitcher; import android. widget. imageview; import android. widget. viewswitcher. viewfactory; public class mainactivity extends activity {imageswitcher iswitcher; button prevbtn, nextbtn; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); viewinit (); // The first way to set the image iswitcher. setimageresource (R. drawable. ic_launcher); prevbtn. setonclicklistener (New onclicklistener () {@ override public void onclick (view arg0) {// method stub automatically generated by todo iswitcher. setimageresource (R. drawable. ic_launcher) ;}}); nextbtn. setonclicklistener (New onclicklistener () {@ override public void onclick (view arg0) {// second method of setting the image iswitcher. setimagedrawable (getresources (). getdrawable (R. drawable. kale) ;}}) ;}private void viewinit () {iswitcher = (imageswitcher) findviewbyid (R. id. imageswitcher_id );
Iswitcher. setfactory (New viewfactory () {@ override public view makeview () {// create an imageview object imageview = new imageview (mainactivity. this); // set the image stretching method imageview. setscaletype (imageview. scaletype. fit_start); // sets the image Filling Method imageview. setlayoutparams (New imageswitcher. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); ReturnImageview ;}});Prevbtn = (button) findviewbyid (R. Id. prev_button_id); nextbtn = (button) findviewbyid (R. Id. next_button_id );}}

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.